Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,47 @@ const _ = require('lodash');
const mp = require('./markdown_parser');
const logger = require('./log');

const annotations_exporter = function (pl) {
const annotationExporter = function (pl) {
const paths = pl.config.paths;
let oldAnnotations;

/**
* Parses JS annotations.
* @returns array of comments that used to be wrapped in raw JS
*/
function parseAnnotationsJS() {
function parseAnnotationsJSON() {
const jsonPath = path.resolve(paths.source.annotations, 'annotations.json');
let annotations;

//attempt to read the file
try {
oldAnnotations = fs.readFileSync(
path.resolve(paths.source.annotations, 'annotations.js'),
'utf8'
);
if (fs.pathExistsSync(jsonPath)) {
//read the new file
annotations = fs.readFileSync(jsonPath, 'utf8');
} else {
//read the old file
const jsPath = path.resolve(paths.source.annotations, 'annotations.js');

annotations = fs
.readFileSync(jsPath, 'utf8')
.replace(/^\s*var comments ?= ?/, '')
.replace(/};\s*$/, '}');

logger.info(
`Please convert ${jsPath} to JSON and rename it annotations.json.`
);
Comment on lines +34 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit on the fence with that. We should let the user decide if he wants to use JS or JSON, even in the future.
Furthermore, we could check if it is possible to require that .js or .json file so that we do not have to strip comments and anything else in that file. Maybe we can change the way it needs to be written to a module.exports format.

Copy link
Contributor Author

@mfranzke mfranzke Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JosefBredereck, actually I've realized that we're stripping that var comments part first of all, to add it again on exporting the data:

'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';

So another solution might be to skip that whole transition to a pure JSON file and only provide the possibility with using JS (the existing format), but remove the JSON file possibility, as we're even already providing another format with the markdown notation as well.
So another task for this ticket could be still to correct the documentation part anyhow: https://patternlab.io/docs/adding-annotations/#heading-json-example

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No way 😄. In that case, throw away those nasty JSON files 🗑️

Copy link
Contributor Author

@mfranzke mfranzke Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, sounds reasonable to me. I've set this ticket on draft again and will proceed by removing these parts as well as the nasty existing transitions in the codebase, as well as correct the documentation page.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole file you mentioned is a solution, but if we would rework the codebase with today's standards, we would maybe not do it that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so does it mean that you want to come up with (or at least describe) a solution ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for now; it would require a more significant rewrite.
This line shows that we already work with modules somewhere.

'module.exports = { config, ishControls, navItems, patternPaths, viewAllPaths, plugins, defaultShowPatternInfo, defaultPattern };';

But then we also need to adjust the place where we load those annotations again.

What makes me curious. Annotations always have a comments property and no other. Is it possible that they have different properties? If not, this would be another change I would like to implement somehow.

'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';

Copy link
Contributor Author

@mfranzke mfranzke Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No way 😄. In that case, throw away those nasty JSON files 🗑️

@JosefBredereck, on the other hand, besides the strange way to include the JSON data into the website after a transformation, the positive way of using JSON (even only) would be that this adapts the way other data is being maintained in the pattern lab system (like pattern data).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. Nodejs is so fast in transforming the data that we could use the approach that is already in this PR. In the end, it makes no difference.

}
} catch (ex) {
logger.debug(
`annotations.js file missing from ${paths.source.annotations}. This may be expected if you do not use annotations or are using markdown.`
`annotations.json file missing from ${paths.source.annotations}. This may be expected if you do not use annotations or are using markdown.`
);
return [];
}

//parse as JSON by removing the old wrapping js syntax. comments and the trailing semi-colon
oldAnnotations = oldAnnotations.replace('var comments = ', '');
oldAnnotations = oldAnnotations.replace('};', '}');

try {
const oldAnnotationsJSON = JSON.parse(oldAnnotations);
return oldAnnotationsJSON.comments;
const annotationsJSON = JSON.parse(annotations);
return annotationsJSON.comments;
} catch (ex) {
logger.error(
`There was an error parsing JSON for ${paths.source.annotations}annotations.js`
);
logger.error(`There was an error parsing JSON for ${jsonPath}`);
return [];
}
}
Expand Down Expand Up @@ -104,7 +112,7 @@ const annotations_exporter = function (pl) {
* @returns array of annotations
*/
function gatherAnnotations() {
const annotationsJS = parseAnnotationsJS();
const annotationsJS = parseAnnotationsJSON();
const annotationsMD = parseAnnotationsMD();
return _.unionBy(annotationsJS, annotationsMD, 'el');
}
Expand All @@ -113,13 +121,13 @@ const annotations_exporter = function (pl) {
gather: function () {
return gatherAnnotations();
},
gatherJS: function () {
return parseAnnotationsJS();
gatherJSON: function () {
return parseAnnotationsJSON();
},
gatherMD: function () {
return parseAnnotationsMD();
},
};
};

module.exports = annotations_exporter;
module.exports = annotationExporter;
6 changes: 3 additions & 3 deletions packages/core/src/lib/exportData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('path');
const eol = require('os').EOL;

const ae = require('./annotation_exporter');
const ae = require('./annotationExporter');

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

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

const paths = patternlab.config.paths;

Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = function (patternlab, uikit) {
eol;

//annotations
const annotationsJSON = annotation_exporter.gather();
const annotationsJSON = annotationExporter.gather();
const annotations =
'var comments = { "comments" : ' + JSON.stringify(annotationsJSON) + '};';
fs.outputFileSync(
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/annotation_exporter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function createFakePatternLab(anPath, customProps) {
}

var patternlab = createFakePatternLab(anPath);
var ae = require('../src/lib/annotation_exporter')(patternlab);
var ae = require('../src/lib/annotationExporter')(patternlab);

tap.test('converts old JS annotations into new format', function (test) {
//arrange
//act
var annotations = ae.gatherJS();
var annotations = ae.gatherJSON();

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

var annotations = ae2.gather();
test.equal(annotations.length, 0);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"comments": []
}
6 changes: 4 additions & 2 deletions packages/docs/src/docs/pattern-adding-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ eleventyNavigation:
sitemapPriority: '0.8'
---

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.
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.

## The Elements of an Annotation

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

## JSON Example

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

```javascript
{
Expand All @@ -32,6 +32,8 @@ This is an example of an annotation saved as part of `annotations.js` that will
}
```

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.

## Markdown Example

This is an example of an annotation saved as part of `annotations.md` that will be added to an element with the class `logo`:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var comments = {
{
"comments" : [
{
"el": ".c-header",
Expand All @@ -11,4 +11,4 @@ var comments = {
"comment": "The logo isn't an image but regular text, which ensures that the logo displays crisply even on high resolution displays."
}
]
};
}