Skip to content

Commit d2c4311

Browse files
teviostevemartin
authored andcommitted
refactor markdown parser code
* extract functions * add test case for zero files
1 parent b448484 commit d2c4311

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

core/lib/annotation_exporter.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,42 @@ var annotations_exporter = function (pl) {
3737
return oldAnnotationsJSON.comments;
3838
}
3939

40-
/*
41-
Converts the annotations.md file yaml list into an array of annotations
42-
*/
43-
function parseAnnotationsMD() {
44-
var markdown_parser = new mp();
45-
var annotations = [];
46-
var mdFiles = readDir.readSync(paths.source.annotations, ['*.md'])
40+
function buildAnnotationMD(annotationsYAML, markdown_parser) {
41+
var annotation = {};
42+
var markdownObj = markdown_parser.parse(annotationsYAML);
43+
44+
annotation.el = markdownObj.el || markdownObj.selector;
45+
annotation.title = markdownObj.title;
46+
annotation.comment = markdownObj.markdown;
47+
return annotation;
48+
}
4749

48-
mdFiles.forEach(function (file) {
49-
var annotationsMD = '';
50-
try {
51-
annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), '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-
}
50+
function parseMDFile(annotations, parser) {
51+
var annotations = annotations;
52+
var markdown_parser = parser;
53+
54+
return function (file) {
55+
var annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), 'utf8');
5856

5957
//take the annotation snippets and split them on our custom delimiter
6058
var annotationsYAML = annotationsMD.split('~*~');
61-
6259
for (var i = 0; i < annotationsYAML.length; i++) {
63-
var annotation = {};
64-
65-
var markdownObj = markdown_parser.parse(annotationsYAML[i]);
66-
67-
annotation.el = markdownObj.el || markdownObj.selector;
68-
annotation.title = markdownObj.title;
69-
annotation.comment = markdownObj.markdown;
70-
60+
var annotation = buildAnnotationMD(annotationsYAML[i], markdown_parser)
7161
annotations.push(annotation);
7262
}
7363
return false;
74-
})
64+
}
65+
}
66+
67+
/*
68+
Converts the *.md file yaml list into an array of annotations
69+
*/
70+
function parseAnnotationsMD() {
71+
var markdown_parser = new mp();
72+
var annotations = [];
73+
var mdFiles = readDir.readSync(paths.source.annotations, ['*.md'])
74+
75+
mdFiles.forEach(parseMDFile(annotations, markdown_parser));
7576
return annotations;
7677
}
7778

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/empty/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)