-
-
Notifications
You must be signed in to change notification settings - Fork 123
Is it possible to create standalone comments? #159
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
Comments
Standalone comments do not currently exist in I think the easiest way to get what you want is to extend the Pair class and override its import { Pair } from 'yaml/types'
class CommentPair extends Pair {
toString(ctx, onComment, onChompKeep) {
const str = super.toString(ctx, onComment, onChompKeep)
return str.replace(new RegExp(`^(${ctx.indent})?`, 'gm'), '$&# ')
}
} In case it's not clear, that should add an appropriate-indented |
I'll give that a try! Thanks! |
Yes, I can confirm, that works well, thank you! One note for any TypeScript users looking at this, here is the typed implementation: class CommentPair extends Pair {
toString(ctx: Schema.StringifyContext, onComment?: () => void, onChompKeep?: () => void) {
// @ts-ignore the Pair (super) type doesn't declare the 3-arg toString overload
const str = super.toString(ctx, onComment, onChompKeep);
return str.replace(new RegExp(`^(${ctx.indent})?`, 'gm'), '$&# ');
}
} @eemeli - if you do any future work on types, and if this sort of use is expected to be common, you may want to consider putting the 3-arg |
Closing, as the original issue is hopefully solved and the missing method typings will be included in the next release. |
Thanks again, @eemeli! |
This is partially related to the above, so I hope you don't mind me asking here, but is there a way to create a standalone comment without attaching it to a specific sequence, map, or pair in the doc?
My use case is that I am generating a YAML file one field at a time based a different configuration data source. For each field, I want to either add it as a pair to the document or add a commented out pair to the document.
E.g., a very simplified example:
resulting in YAML like so (if the legacy config has only name and url):
I guess I can do that today by keeping a reference to the last added pair and appending the new comment to the last added pair's
comment
value, but something like above would be much cleaner.BONUS:
I'm really asking to see if anything like this exists now, but if it's something that needs to be developed, then it would be really cool if I could do something like this:
This would make adding commented out sections of more complex YAML even easier!
The text was updated successfully, but these errors were encountered: