Skip to content

Commit 05e642d

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

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

core/lib/annotation_exporter.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,45 @@ 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, annotations, markdown_parser) {
41+
var annotationsYAML = annotationsYAML;
42+
var annotations = annotations;
4743

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-
}
44+
for (var i = 0; i < annotationsYAML.length; i++) {
45+
var annotation = {};
5846

59-
//take the annotation snippets and split them on our custom delimiter
60-
var annotationsYAML = annotationsMD.split('~*~');
47+
var markdownObj = markdown_parser.parse(annotationsYAML[i]);
6148

62-
for (var i = 0; i < annotationsYAML.length; i++) {
63-
var annotation = {};
49+
annotation.el = markdownObj.el || markdownObj.selector;
50+
annotation.title = markdownObj.title;
51+
annotation.comment = markdownObj.markdown;
6452

65-
var markdownObj = markdown_parser.parse(annotationsYAML[i]);
53+
annotations.push(annotation);
54+
}
55+
}
6656

67-
annotation.el = markdownObj.el || markdownObj.selector;
68-
annotation.title = markdownObj.title;
69-
annotation.comment = markdownObj.markdown;
57+
function parseMDFile(annotations, parser) {
58+
var annotations = annotations;
59+
var markdown_parser = parser;
60+
return function (file) {
61+
var annotationsMD = fs.readFileSync(path.resolve(paths.source.annotations, file), 'utf8');
7062

71-
annotations.push(annotation);
72-
}
63+
//take the annotation snippets and split them on our custom delimiter
64+
var annotationsYAML = annotationsMD.split('~*~');
65+
buildAnnotationMD(annotationsYAML, annotations, markdown_parser);
7366
return false;
74-
})
67+
}
68+
}
69+
70+
/*
71+
Converts the *.md file yaml list into an array of annotations
72+
*/
73+
function parseAnnotationsMD() {
74+
var markdown_parser = new mp();
75+
var annotations = [];
76+
var mdFiles = readDir.readSync(paths.source.annotations, ['*.md'])
77+
78+
mdFiles.forEach(parseMDFile(annotations, markdown_parser));
7579
return annotations;
7680
}
7781

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)