@@ -6,43 +6,49 @@ const _ = require('lodash');
66const mp = require ( './markdown_parser' ) ;
77const logger = require ( './log' ) ;
88
9- const annotations_exporter = function ( pl ) {
9+ const annotationExporter = function ( pl ) {
1010 const paths = pl . config . paths ;
11- let oldAnnotations ;
1211
1312 /**
1413 * Parses JS annotations.
1514 * @returns array of comments that used to be wrapped in raw JS
1615 */
17- function parseAnnotationsJS ( ) {
16+ function parseAnnotationsJSON ( ) {
17+ const jsonPath = path . resolve ( paths . source . annotations , 'annotations.json' ) ;
18+ let annotations ;
19+
1820 //attempt to read the file
1921 try {
20- oldAnnotations = fs . readFileSync (
21- path . resolve ( paths . source . annotations , 'annotations.js' ) ,
22- 'utf8'
23- ) ;
22+ if ( fs . pathExistsSync ( jsonPath ) ) {
23+ //read the new file
24+ annotations = fs . readFileSync ( jsonPath , 'utf8' ) ;
25+ } else {
26+ //read the old file
27+ const jsPath = path . resolve ( paths . source . annotations , 'annotations.js' ) ;
28+
29+ annotations = fs
30+ . readFileSync ( jsPath , 'utf8' )
31+ . replace ( / ^ \s * v a r c o m m e n t s ? = ? / , '' )
32+ . replace ( / } ; \s * $ / , '}' ) ;
33+
34+ logger . info (
35+ `Please convert ${ jsPath } to JSON and rename it annotations.json.`
36+ ) ;
37+ }
2438 } catch ( ex ) {
2539 logger . debug (
26- `annotations.js file missing from ${
40+ `annotations.json file missing from ${
2741 paths . source . annotations
2842 } . This may be expected if you do not use annotations or are using markdown.`
2943 ) ;
3044 return [ ] ;
3145 }
3246
33- //parse as JSON by removing the old wrapping js syntax. comments and the trailing semi-colon
34- oldAnnotations = oldAnnotations . replace ( 'var comments = ' , '' ) ;
35- oldAnnotations = oldAnnotations . replace ( '};' , '}' ) ;
36-
3747 try {
38- const oldAnnotationsJSON = JSON . parse ( oldAnnotations ) ;
39- return oldAnnotationsJSON . comments ;
48+ const annotationsJSON = JSON . parse ( annotations ) ;
49+ return annotationsJSON . comments ;
4050 } catch ( ex ) {
41- logger . error (
42- `There was an error parsing JSON for ${
43- paths . source . annotations
44- } annotations.js`
45- ) ;
51+ logger . error ( `There was an error parsing JSON for ${ jsonPath } ` ) ;
4652 return [ ] ;
4753 }
4854 }
@@ -108,7 +114,7 @@ const annotations_exporter = function(pl) {
108114 * @returns array of annotations
109115 */
110116 function gatherAnnotations ( ) {
111- const annotationsJS = parseAnnotationsJS ( ) ;
117+ const annotationsJS = parseAnnotationsJSON ( ) ;
112118 const annotationsMD = parseAnnotationsMD ( ) ;
113119 return _ . unionBy ( annotationsJS , annotationsMD , 'el' ) ;
114120 }
@@ -117,13 +123,13 @@ const annotations_exporter = function(pl) {
117123 gather : function ( ) {
118124 return gatherAnnotations ( ) ;
119125 } ,
120- gatherJS : function ( ) {
121- return parseAnnotationsJS ( ) ;
126+ gatherJSON : function ( ) {
127+ return parseAnnotationsJSON ( ) ;
122128 } ,
123129 gatherMD : function ( ) {
124130 return parseAnnotationsMD ( ) ;
125131 } ,
126132 } ;
127133} ;
128134
129- module . exports = annotations_exporter ;
135+ module . exports = annotationExporter ;
0 commit comments