-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: Keep comment #242
Comments
Thanks @mjy9088 this is indeed a known issue. Unfortunatly, this is related to the YAML parser. The sample you provided is still going to give the same issue. The YAML parser will keep only the comments that were added before the front matter section. I have just tested this by writing a parser based on the sample you gave me. My front matter looks as follows: The parser returns:
Went a bet further, and checked what it parsed, and unfortunately, it also removes the comments:
If you know a way to handle this, feel free to suggest or contribute. I'm sure this issue can help others as well. |
https://codesandbox.io/s/yaml-comment-test-jl08o import YAML from "yaml";
const original = `
# This is YAML
---
title: YAML Test
slug: yaml-test
date: '2022-02-08T17:34:18.844Z'
lastmod: '2022-02-09T10:08:20.682Z'
tags:
- 'lang:XML'
# TODO: add more languages
# this comment is ok also
- Test
`;
const parsed = YAML.parseDocument(original);
const serialized = parsed.toString();
console.log(serialized); output:
The parser looks fine. Since I had previously used the comment-json module (almost same to using global ...but now that I see it, using this module is not that simple 馃槩 it seems like a lot of changes are needed to use this module. |
This works, as you just parse it and go straight to a Internally we use the |
how about using const fs = require("fs");
const YAWN = require("yawn-yaml/cjs");
const yaml = require("js-yaml");
const originalText = fs.readFileSync("./original.yml").toString();
const yawn = new YAWN(originalText);
const content = yaml.load(originalText);
const test = "[TEST] THIS CONTENT IS DYNAMICALLY ADDED! - "
const changedContent = {
...content,
tags: [
test + "add",
...content.tags.map((a, i) => i ? [test + "more", a] : a).flat(1),
test + "tags"
]
};
yawn.json = changedContent;
console.log(yawn.yaml); input: # This is YAML
---
title: YAML Test
slug: yaml-test
date: '2022-02-08T17:34:18.844Z'
lastmod: '2022-02-09T10:08:20.682Z'
tags:
- 'lang:XML'
# TODO: Check this comment remains
- Test output: # This is YAML
---
title: YAML Test
slug: yaml-test
date: '2022-02-08T17:34:18.844Z'
lastmod: '2022-02-09T10:08:20.682Z'
tags:
- '[TEST] THIS CONTENT IS DYNAMICALLY ADDED! - add'
# TODO: Check this comment remains
- 'lang:XML'
- '[TEST] THIS CONTENT IS DYNAMICALLY ADDED! - more'
- Test
- '[TEST] THIS CONTENT IS DYNAMICALLY ADDED! - tags' |
comment-json stores comments to symbol properties, so I thought yaml works similar way too... This is what I thought wrong. sorry + yawn-yaml doesn't works same way as comment-json, sometimes buggy. but it seems OK to add/modify existing value only |
@mjy9088 I've been testing the dependency you mentioned. It turns out that when updating arrays or objects it still removes comments. Screen.Recording.2022-02-16.at.17.32.40.mov |
Is your feature request related to a problem? Please describe.
I put comments for readability in the front matter. modified by this extension, all comments gone! 馃帀
Describe the solution you'd like
use yaml or similar package to keep comments
The text was updated successfully, but these errors were encountered: