Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Earnshaw committed Jul 1, 2015
0 parents commit c7d2b8c
Show file tree
Hide file tree
Showing 5 changed files with 10,020 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Andy Earnshaw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# MDN Links
__MDN Links__ is a utility package for generating links to Mozilla Developer Network documentation pages. It's primary use is for generating links for YUIDoc to use for native classes and methods. Currently, only links to the Web API Reference and JavaScript Global Objects pages are supported.

## Usage

Install:

npm install mdn-linker

Require:

const mdn = require('mdn-links');

### Retrieve a link
Simply pass the name of the global object and, optionally, one of its members to the `getLink()` method:

mdn.getLink('Array');
//-> 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array'

mdn.getLink('Blob', 'slice');
//-> 'https://developer.mozilla.org/en-US/docs/Web/API/Blob/slice'

### Update cache
A cached set of links is included in the package, but if these become stale they can be updated with the `update()` method:

mdn.update(function () {
console.log(mdn.getLink('Array', 'isArray'));
console.log(mdn.getLink('Array', 'prototype.forEach'));
});

## License

Copyright (c) 2015 Andy Earnshaw

This software is licensed under the MIT license. See the `LICENSE.txt` file
accompanying this software for terms of use.

109 changes: 109 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*eslint-env node*/
'use strict';
var data = {},
get = require('https').get,
fs = require('fs'),
path = require('path');

if (require.main === module) {
console.log('Updating cache');
getData(function () {
console.log('Done');
});
}

module.exports = {
/**
* Fetches the latest data from MDN and caches it locally.
*
* @method update
* @param {function} callback function to be called when download completes
*/
update: getData,

/**
* Returns a link to the MDN documentation for the supplied native global (e.g. class or
* function) or, if provided, a property belonging to that global object.
*
* @param {string} global name of the global object to obtain a url for
* @param {string} [property] name of the property belonging to the class
* @returns
*/
getLink: function (global, method) {
var root, base, url = '';

if (!Object.keys(data).length) {
data = require('./mdn-cache');
}

for (root in data) {
base = data[root][global];
if (base) {
url = root + base._slug;

// Add the method as a subpage, or fragment to try and get them close if there's no
// matching subpage
if (method) {
url += (base[global + '.' + method] || base[global + '.prototype.' + method] || {})._slug || '#' + method;
}

break;
}
}

return url;
}
};

function getData (callback) {
// MDN base URLS
var 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 = ';

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 getSlugs (d, c) {
// Only get classes for now, though methods/functions might be useful for
// crosslinking
if (!~c.title.indexOf(' ')) {
c.title = c.title.replace('()', '');
d[c.title] = {
_slug: c.slug.slice(c.slug.lastIndexOf('/'))
};

// Repeat for subpages
c.subpages.reduce(getSlugs, d[c.title]);
}
return d;
}, {});

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

function write () {
fs.writeFileSync(
path.join(__dirname, 'mdn-cache.js'),
output + JSON.stringify(data, null, 4) + ';\n'
);

callback(data);
}
}
Loading

0 comments on commit c7d2b8c

Please sign in to comment.