Skip to content

Commit

Permalink
Use mdn-links package for cross-linking MDN
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Earnshaw committed Jul 6, 2015
1 parent b9004dd commit 634c65f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 60 deletions.
67 changes: 7 additions & 60 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

var MarkdownIt = require('markdown-it');
var fs = require('graceful-fs');
var mdn = require('mdn-links');
var noop = function () {};
var path = require('path');
var TEMPLATE;
Expand Down Expand Up @@ -251,13 +252,11 @@ YUI.add('doc-builder', function (Y) {
}
}
if (!link && self.options.linkNatives) {
if (self.NATIVES && self.NATIVES[item]) {
href = self.NATIVES_LINKER(item);
if (href) {
className += ' external';
newWin = true;
link = true;
}
href = mdn.getLink.apply(mdn, item.split('/'));
if (href) {
className += ' external';
newWin = true;
link = true;
}
}
if (link) {
Expand All @@ -271,59 +270,7 @@ YUI.add('doc-builder', function (Y) {
}
return (raw) ? href : item;
},
/**
* List of native types to cross link to MDN
* @property NATIVES
* @type Object
*/
NATIVES: {
'Array': 1,
'Boolean': 1,
'Date': 1,
'decodeURI': 1,
'decodeURIComponent': 1,
'encodeURI': 1,
'encodeURIComponent': 1,
'eval': 1,
'Error': 1,
'EvalError': 1,
'Function': 1,
'Infinity': 1,
'isFinite': 1,
'isNaN': 1,
'Math': 1,
'NaN': 1,
'Number': 1,
'Object': 1,
'parseFloat': 1,
'parseInt': 1,
'RangeError': 1,
'ReferenceError': 1,
'RegExp': 1,
'String': 1,
'SyntaxError': 1,
'TypeError': 1,
'undefined': 1,
'URIError': 1,
'HTMLElement': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/',
'HTMLCollection': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/',
'DocumentFragment': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/',
'HTMLDocument': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/'
},
/**
* Function to link an external type uses `NATIVES` object
* @method NATIVES_LINKER
* @private
* @param {String} name The name of the type to link
* @return {String} The combined URL
*/
NATIVES_LINKER: function (name) {
var url = 'https:/' + '/developer.mozilla.org/en/JavaScript/Reference/Global_Objects/';
if (this.NATIVES[name] !== 1) {
url = this.NATIVES[name];
}
return url + name;
},

/**
* Mixes the various external data soures together into the local data, augmenting
* it with flags.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"express": "^4.13.0",
"graceful-fs": "^4.1.2",
"markdown-it": "^4.2.2",
"mdn-links": "^0.1.0",
"minimatch": "^2.0.8",
"rimraf": "2.x",
"yui": "^3.18.1"
Expand Down
52 changes: 52 additions & 0 deletions scripts/mdn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node
'use strict';

var get = require('https').get,
fs = require('fs'),
path = require('path'),

// MDN base URLS
baseURLs = [
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects',
'https://developer.mozilla.org/en-US/docs/Web/API'
],

done = 0,
output = 'module.exports = ',

// Final data object
data = {};

baseURLs.forEach(function (u) {
get(u + '$children', function (res) {
var text = '';

res.on('data', function(d) {
text += d;
});

res.on('end', function () {
var vars = JSON.parse(text);

data[u] = vars.subpages.reduce(function (d, c) {
// Only get classes for now, though methods/functions might be useful for
// crosslinking
if (!~c.title.indexOf('()') && !~c.title.indexOf(' ')) {
d[c.title] = c.slug.slice(c.slug.lastIndexOf('/'));
}
return d;
}, {});

if (++done === baseURLs.length) {
write();
}
});
});
});

function write () {
fs.writeFileSync(
path.join(__dirname, '../lib/mdn.js'),
output + JSON.stringify(data, null, 4) + ';'
);
}

0 comments on commit 634c65f

Please sign in to comment.