Skip to content

Commit 6e47793

Browse files
author
Brian Muenzenmeyer
authored
Merge pull request #387 from stevemartin/dev
fix for issue #385 - support foo.md inside _annotations
2 parents 1d651c2 + b5dd1d0 commit 6e47793

File tree

6 files changed

+52
-36
lines changed

6 files changed

+52
-36
lines changed

core/lib/annotation_exporter.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
var path = require('path'),
4+
glob = require('glob'),
45
fs = require('fs-extra'),
56
JSON5 = require('json5'),
67
_ = require('lodash'),
@@ -38,38 +39,42 @@ var annotations_exporter = function (pl) {
3839
return oldAnnotationsJSON.comments;
3940
}
4041

41-
/*
42-
Converts the annotations.md file yaml list into an array of annotations
43-
*/
44-
function parseAnnotationsMD() {
45-
var markdown_parser = new mp();
46-
var annotations = [];
42+
function buildAnnotationMD(annotationsYAML, markdown_parser) {
43+
var annotation = {};
44+
var markdownObj = markdown_parser.parse(annotationsYAML);
4745

48-
//attempt to read the file
49-
var annotationsMD = '';
50-
try {
51-
annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, 'annotations.md'), 'utf8');
52-
} catch (ex) {
53-
if (pl.config.debug) {
54-
console.log('annotations.md file missing from ' + paths.source.annotations + '. This may be expected.');
55-
}
56-
return [];
57-
}
46+
annotation.el = markdownObj.el || markdownObj.selector;
47+
annotation.title = markdownObj.title;
48+
annotation.comment = markdownObj.markdown;
49+
return annotation;
50+
}
5851

59-
//take the annotation snippets and split them on our custom delimiter
60-
var annotationsYAML = annotationsMD.split('~*~');
52+
function parseMDFile(annotations, parser) {
53+
var annotations = annotations;
54+
var markdown_parser = parser;
6155

62-
for (var i = 0; i < annotationsYAML.length; i++) {
63-
var annotation = {};
56+
return function (filePath) {
57+
var annotationsMD = fs.readFileSync(path.resolve(filePath), 'utf8');
6458

65-
var markdownObj = markdown_parser.parse(annotationsYAML[i]);
59+
//take the annotation snippets and split them on our custom delimiter
60+
var annotationsYAML = annotationsMD.split('~*~');
61+
for (var i = 0; i < annotationsYAML.length; i++) {
62+
var annotation = buildAnnotationMD(annotationsYAML[i], markdown_parser)
63+
annotations.push(annotation);
64+
}
65+
return false;
66+
}
67+
}
6668

67-
annotation.el = markdownObj.el || markdownObj.selector;
68-
annotation.title = markdownObj.title;
69-
annotation.comment = markdownObj.markdown;
69+
/*
70+
Converts the *.md file yaml list into an array of annotations
71+
*/
72+
function parseAnnotationsMD() {
73+
var markdown_parser = new mp();
74+
var annotations = [];
75+
var mdFiles = glob.sync(paths.source.annotations + '/*.md')
7076

71-
annotations.push(annotation);
72-
}
77+
mdFiles.forEach(parseMDFile(annotations, markdown_parser));
7378
return annotations;
7479
}
7580

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"contributors": [
3838
{
39-
"name" : "Geoff Pursell"
39+
"name": "Geoff Pursell"
4040
}
4141
],
4242
"license": "MIT",

test/annotation_exporter_tests.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
var eol = require('os').EOL;
44
var Pattern = require('../core/lib/object_factory').Pattern;
55
var extend = require('util')._extend;
6+
var anPath = './test/files/';
67

7-
function createFakePatternLab(customProps) {
8+
function createFakePatternLab(anPath, customProps) {
89
var pl = {
910
"config": {
1011
"paths": {
1112
"source": {
12-
"annotations": './test/files/'
13+
"annotations": anPath
1314
}
1415
}
1516
}
@@ -18,7 +19,7 @@ function createFakePatternLab(customProps) {
1819
return extend(pl, customProps);
1920
}
2021

21-
var patternlab = createFakePatternLab();
22+
var patternlab = createFakePatternLab(anPath);
2223
var ae = require('../core/lib/annotation_exporter')(patternlab);
2324

2425
exports['annotaton_exporter'] = {
@@ -65,5 +66,15 @@ exports['annotaton_exporter'] = {
6566

6667
test.done();
6768

69+
},
70+
71+
'when there are 0 annotation files' : function (test) {
72+
var emptyAnPath = './test/files/empty/';
73+
var patternlab2 = createFakePatternLab(emptyAnPath);
74+
var ae2 = require('../core/lib/annotation_exporter')(patternlab2);
75+
76+
var annotations = ae2.gather();
77+
test.equals(annotations.length, 0);
78+
test.done();
6879
}
6980
};

test/files/annotations.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@ selector: .logo
1010
title: Logo
1111
---
1212
The _logo image_ is an SVG file.
13-
~*~
14-
---
15-
el: #nav
16-
title : Navigation
17-
---
18-
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.

test/files/empty/.gitkeep

Whitespace-only changes.

test/files/nav.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
el: #nav
3+
title : Navigation
4+
---
5+
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.
6+

0 commit comments

Comments
 (0)