Simplify plugin
The augmentExamples just adds metadata to the example markup files. If we also want to support example markdown files, we can use the metalsmith-markdown plugin before this one.
This commit is contained in:
+3
-1
@@ -10,10 +10,12 @@
|
|||||||
"trailing": true,
|
"trailing": true,
|
||||||
"maxlen": 80,
|
"maxlen": 80,
|
||||||
"globals": {
|
"globals": {
|
||||||
|
"Buffer": false,
|
||||||
"__dirname": false,
|
"__dirname": false,
|
||||||
"exports": true,
|
"exports": true,
|
||||||
"module": false,
|
"module": false,
|
||||||
"process": false,
|
"process": false,
|
||||||
"require": false
|
"require": false,
|
||||||
|
"setImmediate": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-14
@@ -1,4 +1,3 @@
|
|||||||
/*global Buffer */
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var Metalsmith = require('metalsmith');
|
var Metalsmith = require('metalsmith');
|
||||||
@@ -31,27 +30,23 @@ var templatesDir = path.join(__dirname, '..', 'config', 'examples');
|
|||||||
* @param {function(Error)} done Called when done (with any error).
|
* @param {function(Error)} done Called when done (with any error).
|
||||||
*/
|
*/
|
||||||
function augmentExamples(files, metalsmith, done) {
|
function augmentExamples(files, metalsmith, done) {
|
||||||
|
setImmediate(done); // all remaining code is synchronous
|
||||||
for (var filename in files) {
|
for (var filename in files) {
|
||||||
var file = files[filename];
|
var file = files[filename];
|
||||||
var match = filename.match(markupRegEx);
|
var match = filename.match(markupRegEx);
|
||||||
if (match && filename !== 'index.html') {
|
if (match && filename !== 'index.html') {
|
||||||
if (!file.template) {
|
if (!file.template) {
|
||||||
done(new Error('Missing template in YAML front-matter:' + filename));
|
throw new Error(filename + ': Missing template in YAML front-matter');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
var id = match[1];
|
var id = match[1];
|
||||||
if (file.docs) {
|
if (file.docs) {
|
||||||
file.docs = marked(file.docs);
|
file.docs = marked(file.docs);
|
||||||
}
|
}
|
||||||
if (file.contents) {
|
|
||||||
file.contents = new Buffer(marked(file.contents.toString()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// add js tag and source
|
// add js tag and source
|
||||||
var jsFilename = id + '.js';
|
var jsFilename = id + '.js';
|
||||||
if (!(jsFilename in files)) {
|
if (!(jsFilename in files)) {
|
||||||
done(new Error('No .js file found for ' + filename));
|
throw new Error('No .js file found for ' + filename);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
file.js = {
|
file.js = {
|
||||||
tag: '<script src="loader.js?id=' + id + '"></script>',
|
tag: '<script src="loader.js?id=' + id + '"></script>',
|
||||||
@@ -76,19 +71,16 @@ function augmentExamples(files, metalsmith, done) {
|
|||||||
if (isJsRegEx.test(resource)) {
|
if (isJsRegEx.test(resource)) {
|
||||||
resources[i] = '<script src="' + resource + '"></script>';
|
resources[i] = '<script src="' + resource + '"></script>';
|
||||||
} else if (isCssRegEx.test(resource)) {
|
} else if (isCssRegEx.test(resource)) {
|
||||||
resources[i] = '<link rel="stylesheet" href="' + resource +
|
resources[i] = '<link rel="stylesheet" href="' + resource + '">';
|
||||||
'">';
|
|
||||||
} else {
|
} else {
|
||||||
done(new Error('Invalid value for resource: ' +
|
throw new Error('Invalid value for resource: ' +
|
||||||
resource + ' is not .js or .css: ' + filename));
|
resource + ' is not .js or .css: ' + filename);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file.extra_head = resources.join('\n');
|
file.extra_head = resources.join('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function main(callback) {
|
function main(callback) {
|
||||||
|
|||||||
Reference in New Issue
Block a user