-
-
Notifications
You must be signed in to change notification settings - Fork 771
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you generate a YAML file with comments? #642
Comments
I need this function too |
This parser implementation does NOT have AST and can't manage metadata like comments and so on. This question was already asked and answered in previous issues. |
@puzrin You don't have any interest in fixing that? For example maybe dump() could accept a map or callback that associates comment strings with JSON nodes. The problem does not seem insurmountable. |
@octogonz i guess, you don't understand the cost of such "fix". That's really full rewrite. So, no, i'm not interested to spend a lot of time for that. Because, i have other projects to do. But, if you know how to solve problem better than me, you can suggest PR. |
I am an author of a YAML library and I know a list of YAML libraries. Very few support creating comments. It's hard, I concur with @puzrin . |
@perlpunk Thank you! We'll check it out. |
@perlpunk that works great, thank you. |
I implemented the following utility which allows using yaml-js to generate output with comments in it: const YAML_COMMENT_PREFIX = '___YAML_COMMENT_';
const YamlCommentRegex = /( *)___YAML_COMMENT_\d+: *([^ |].*)/g;
const MultilineYamlCommentRegex = /( *)___YAML_COMMENT_\d+: *\|-\n((\1 +)[^ ].*(?:\n\3[^ ].+)*)/g;
let yamlCommentIndex = 0;
/**
* Creates a key to be used as a YAML comment
*/
export function yamlComment() {
return YAML_COMMENT_PREFIX + yamlCommentIndex++;
}
/**
* Replaces YAML comment keys created with {@link yamlComment} with proper YAML comment syntax
*/
export function processYamlComments(baseYaml: string) {
return baseYaml
.replace(YamlCommentRegex, (_match, leadingSpace: string, comment: string) => leadingSpace + '# ' + comment.trim())
.replace(
MultilineYamlCommentRegex,
(_match, leadingSpace: string, comment: string) => comment
.split('\n')
.map(x => `${leadingSpace}# ${x.trim()}`)
.join('\n')
);
} Example: const sampleData = {
[yamlComment()]: 'Comment on root key / start of file',
ROOT: {
[yamlComment()]: 'Comment on child key',
CHILD_1: 'test',
[yamlComment()]: 'Multi-line comment\non child key',
CHILD_2: "'this' is quoted",
[yamlComment()]: "This comment's text has special characters!",
CHILD_3: 'key 3',
},
};
test('test', () => {
const yaml = processYamlComments(dump(sampleData));
expect(yaml).toMatchInlineSnapshot(`
"# Comment on root key / start of file
ROOT:
# Comment on child key
CHILD_1: test
# Multi-line comment
# on child key
CHILD_2: '''this'' is quoted'
# This comment's text has special characters!
CHILD_3: key 3
"
`);
}); Relates to #680 |
We have a tool that generates YAML config files for CI pipelines.
The files contain informative code comments. Here's a representative example (with a bunch of fields deleted for brevity):
As you can see, the comments aren't merely pasted at the top of the file, they need to appear inline in the file as well.
We started using a simple string template, but it is tricky to escape parameters correctly. The js-yaml project seems like a better solution.
But does the
yaml.dump()
API provide some way to inject comment strings?Comments are a core feature of YAML, so it seems that there should be some way to do that. Thanks!
CC @hbo-iecheruo
The text was updated successfully, but these errors were encountered: