diff --git a/core/lib/annotation_exporter.js b/core/lib/annotation_exporter.js index e51ff1106..dc13698dd 100644 --- a/core/lib/annotation_exporter.js +++ b/core/lib/annotation_exporter.js @@ -1,6 +1,7 @@ "use strict"; var path = require('path'), + glob = require('glob'), fs = require('fs-extra'), JSON5 = require('json5'), _ = require('lodash'), @@ -36,38 +37,42 @@ var annotations_exporter = function (pl) { return oldAnnotationsJSON.comments; } - /* - Converts the annotations.md file yaml list into an array of annotations - */ - function parseAnnotationsMD() { - var markdown_parser = new mp(); - var annotations = []; + function buildAnnotationMD(annotationsYAML, markdown_parser) { + var annotation = {}; + var markdownObj = markdown_parser.parse(annotationsYAML); - //attempt to read the file - var annotationsMD = ''; - try { - annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.md'), 'utf8'); - } catch (ex) { - if (pl.config.debug) { - console.log('annotations.md file missing from ' + paths.source.annotations + '. This may be expected.'); - } - return []; - } + annotation.el = markdownObj.el || markdownObj.selector; + annotation.title = markdownObj.title; + annotation.comment = markdownObj.markdown; + return annotation; + } - //take the annotation snippets and split them on our custom delimiter - var annotationsYAML = annotationsMD.split('~*~'); + function parseMDFile(annotations, parser) { + var annotations = annotations; + var markdown_parser = parser; - for (var i = 0; i < annotationsYAML.length; i++) { - var annotation = {}; + return function (filePath) { + var annotationsMD = fs.readFileSync(path.resolve(filePath), 'utf8'); - var markdownObj = markdown_parser.parse(annotationsYAML[i]); + //take the annotation snippets and split them on our custom delimiter + var annotationsYAML = annotationsMD.split('~*~'); + for (var i = 0; i < annotationsYAML.length; i++) { + var annotation = buildAnnotationMD(annotationsYAML[i], markdown_parser) + annotations.push(annotation); + } + return false; + } + } - annotation.el = markdownObj.el || markdownObj.selector; - annotation.title = markdownObj.title; - annotation.comment = markdownObj.markdown; + /* + Converts the *.md file yaml list into an array of annotations + */ + function parseAnnotationsMD() { + var markdown_parser = new mp(); + var annotations = []; + var mdFiles = glob.sync(paths.source.annotations + '/*.md') - annotations.push(annotation); - } + mdFiles.forEach(parseMDFile(annotations, markdown_parser)); return annotations; } diff --git a/package.json b/package.json index 53992f55d..8148364b7 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ }, "contributors": [ { - "name" : "Geoff Pursell" + "name": "Geoff Pursell" } ], "license": "MIT", diff --git a/test/annotation_exporter_tests.js b/test/annotation_exporter_tests.js index 70bbfec06..49ae049c8 100644 --- a/test/annotation_exporter_tests.js +++ b/test/annotation_exporter_tests.js @@ -3,13 +3,14 @@ var eol = require('os').EOL; var Pattern = require('../core/lib/object_factory').Pattern; var extend = require('util')._extend; +var anPath = './test/files/'; -function createFakePatternLab(customProps) { +function createFakePatternLab(anPath, customProps) { var pl = { "config": { "paths": { "source": { - "annotations": './test/files/' + "annotations": anPath } } } @@ -18,7 +19,7 @@ function createFakePatternLab(customProps) { return extend(pl, customProps); } -var patternlab = createFakePatternLab(); +var patternlab = createFakePatternLab(anPath); var ae = require('../core/lib/annotation_exporter')(patternlab); exports['annotaton_exporter'] = { @@ -65,5 +66,15 @@ exports['annotaton_exporter'] = { test.done(); + }, + + 'when there are 0 annotation files' : function (test) { + var emptyAnPath = './test/files/empty/'; + var patternlab2 = createFakePatternLab(emptyAnPath); + var ae2 = require('../core/lib/annotation_exporter')(patternlab2); + + var annotations = ae2.gather(); + test.equals(annotations.length, 0); + test.done(); } }; diff --git a/test/files/annotations.md b/test/files/annotations.md index 7e2ad972a..cf895f081 100644 --- a/test/files/annotations.md +++ b/test/files/annotations.md @@ -10,9 +10,3 @@ selector: .logo title: Logo --- The _logo image_ is an SVG file. -~*~ ---- -el: #nav -title : Navigation ---- -Navigation for adaptive web experiences can be tricky. Refer to [these repsonsive patterns](https://bradfrost.github.io/this-is-responsive/patterns.html#navigation) when evaluating solutions. diff --git a/test/files/empty/.gitkeep b/test/files/empty/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/test/files/nav.md b/test/files/nav.md new file mode 100644 index 000000000..00de05203 --- /dev/null +++ b/test/files/nav.md @@ -0,0 +1,6 @@ +--- +el: #nav +title : Navigation +--- +Navigation for adaptive web experiences can be tricky. Refer to [these repsonsive patterns](https://bradfrost.github.io/this-is-responsive/patterns.html#navigation) when evaluating solutions. +