Skip to content

Commit d303f4d

Browse files
refactor: allow .json extension on annotations file #836 (#1402)
* refactor: corrected annotations.js to annotations.json * refactor(annotations): allow .json extension on annotations file #836 * refactor: corrected annotations.js to annotations.json * feat(annotations): added at least annotation to the sample content * chore: added another annotations example * refactor(docs): corrected the filetype in the docs as well * docs(annotations): added further details * refactor: removed this file again for the moment * refactor: renamed this file (after the latest merge) * refactor: migrated that file as well Co-authored-by: Josef Bredreck <13408112+JosefBredereck@users.noreply.github.com>
1 parent b6f18e0 commit d303f4d

File tree

9 files changed

+44
-34
lines changed

9 files changed

+44
-34
lines changed

packages/core/src/lib/annotation_exporter.js renamed to packages/core/src/lib/annotationExporter.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,47 @@ const _ = require('lodash');
66
const mp = require('./markdown_parser');
77
const 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*var comments ?= ?/, '')
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 ${paths.source.annotations}. This may be expected if you do not use annotations or are using markdown.`
40+
`annotations.json file missing from ${paths.source.annotations}. This may be expected if you do not use annotations or are using markdown.`
2741
);
2842
return [];
2943
}
3044

31-
//parse as JSON by removing the old wrapping js syntax. comments and the trailing semi-colon
32-
oldAnnotations = oldAnnotations.replace('var comments = ', '');
33-
oldAnnotations = oldAnnotations.replace('};', '}');
34-
3545
try {
36-
const oldAnnotationsJSON = JSON.parse(oldAnnotations);
37-
return oldAnnotationsJSON.comments;
46+
const annotationsJSON = JSON.parse(annotations);
47+
return annotationsJSON.comments;
3848
} catch (ex) {
39-
logger.error(
40-
`There was an error parsing JSON for ${paths.source.annotations}annotations.js`
41-
);
49+
logger.error(`There was an error parsing JSON for ${jsonPath}`);
4250
return [];
4351
}
4452
}
@@ -104,7 +112,7 @@ const annotations_exporter = function (pl) {
104112
* @returns array of annotations
105113
*/
106114
function gatherAnnotations() {
107-
const annotationsJS = parseAnnotationsJS();
115+
const annotationsJS = parseAnnotationsJSON();
108116
const annotationsMD = parseAnnotationsMD();
109117
return _.unionBy(annotationsJS, annotationsMD, 'el');
110118
}
@@ -113,13 +121,13 @@ const annotations_exporter = function (pl) {
113121
gather: function () {
114122
return gatherAnnotations();
115123
},
116-
gatherJS: function () {
117-
return parseAnnotationsJS();
124+
gatherJSON: function () {
125+
return parseAnnotationsJSON();
118126
},
119127
gatherMD: function () {
120128
return parseAnnotationsMD();
121129
},
122130
};
123131
};
124132

125-
module.exports = annotations_exporter;
133+
module.exports = annotationExporter;

packages/core/src/lib/exportData.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const path = require('path');
44
const eol = require('os').EOL;
55

6-
const ae = require('./annotation_exporter');
6+
const ae = require('./annotationExporter');
77

88
let fs = require('fs-extra'); //eslint-disable-line prefer-const
99

@@ -12,7 +12,7 @@ let fs = require('fs-extra'); //eslint-disable-line prefer-const
1212
* @param patternlab - global data store
1313
*/
1414
module.exports = function (patternlab, uikit) {
15-
const annotation_exporter = new ae(patternlab);
15+
const annotationExporter = new ae(patternlab);
1616

1717
const paths = patternlab.config.paths;
1818

@@ -67,7 +67,7 @@ module.exports = function (patternlab, uikit) {
6767
eol;
6868

6969
//annotations
70-
const annotationsJSON = annotation_exporter.gather();
70+
const annotationsJSON = annotationExporter.gather();
7171
const annotations =
7272
'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
7373
fs.outputFileSync(

packages/core/test/annotation_exporter_tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ function createFakePatternLab(anPath, customProps) {
2020
}
2121

2222
var patternlab = createFakePatternLab(anPath);
23-
var ae = require('../src/lib/annotation_exporter')(patternlab);
23+
var ae = require('../src/lib/annotationExporter')(patternlab);
2424

2525
tap.test('converts old JS annotations into new format', function (test) {
2626
//arrange
2727
//act
28-
var annotations = ae.gatherJS();
28+
var annotations = ae.gatherJSON();
2929

3030
//assert
3131
test.equal(annotations.length, 2);
@@ -77,7 +77,7 @@ tap.test('merges both annotation methods into one array', function (test) {
7777
tap.test('when there are 0 annotation files', function (test) {
7878
var emptyAnPath = './test/files/empty/';
7979
var patternlab2 = createFakePatternLab(emptyAnPath);
80-
var ae2 = require('../src/lib/annotation_exporter')(patternlab2);
80+
var ae2 = require('../src/lib/annotationExporter')(patternlab2);
8181

8282
var annotations = ae2.gather();
8383
test.equal(annotations.length, 0);

packages/development-edition-engine-handlebars/source/_annotations/annotations.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"comments": []
3+
}

packages/docs/src/docs/pattern-adding-annotations.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ eleventyNavigation:
1010
sitemapPriority: '0.8'
1111
---
1212

13-
Annotations provide an easy way to add notes to elements that may appear inside patterns. Annotations can be saved as a single JSON file at `./source/_annotations/annotations.js` or as multiple Markdown files in `./source/_annotations/`. They're _not_ tied to any specific patterns. When annotations are active they are compared against every pattern using a CSS selector syntax.
13+
Annotations provide an easy way to add notes to elements that may appear inside patterns. Annotations can be saved as a single JSON file at `./source/_annotations/annotations.json` or as multiple Markdown files in `./source/_annotations/`. They're _not_ tied to any specific patterns. When annotations are active they are compared against every pattern using a CSS selector syntax.
1414

1515
## The Elements of an Annotation
1616

@@ -22,7 +22,7 @@ The elements of an annotation are:
2222

2323
## JSON Example
2424

25-
This is an example of an annotation saved as part of `annotations.js` that will be added to an element with the class `logo`:
25+
This is an example of an annotation saved as part of `annotations.json` that will be added to an element with the class `logo`:
2626

2727
```javascript
2828
{
@@ -32,6 +32,8 @@ This is an example of an annotation saved as part of `annotations.js` that will
3232
}
3333
```
3434

35+
Compare to e.g. [`handlebars` annotations](https://github.com/pattern-lab/patternlab-node/blob/dev/packages/starterkit-handlebars-demo/dist/_annotations/annotations.json) or [`twig` annotations](https://github.com/pattern-lab/patternlab-node/blob/dev/packages/starterkit-twig-demo/dist/_annotations/annotations.json) editions demo content as well.
36+
3537
## Markdown Example
3638

3739
This is an example of an annotation saved as part of `annotations.md` that will be added to an element with the class `logo`:

packages/starterkit-handlebars-demo/dist/_annotations/annotations.js renamed to packages/starterkit-handlebars-demo/dist/_annotations/annotations.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var comments = {
1+
{
22
"comments" : [
33
{
44
"el": ".c-header",
@@ -11,4 +11,4 @@ var comments = {
1111
"comment": "The logo isn't an image but regular text, which ensures that the logo displays crisply even on high resolution displays."
1212
}
1313
]
14-
};
14+
}

0 commit comments

Comments
 (0)