-
I'm trying to work with some yaml that has ruby object tags, specifically the !ruby/object:SomeRule tag for each list item. I'd like to use the custom api tag but I'm struggling to determine how to take the value that is matching and returning what is needed in the stringify method. import YAML, { ScalarTag } from 'yaml'
import { stringifyString } from 'yaml/util'
const original = `---
:some_rules: !seq:SomeRules
- !ruby/object:SomeRule
id: 1
- !ruby/object:SomeRule
id: 2
- !ruby/object:SomeRule
id: 3
`;
// @ts-ignore
const someRuleRubyTag = {
identify: value => typeof value === "object" && Object.keys(value!).includes("id"),
tag: '!ruby/object:SomeRule',
stringify(item, ctx, onComment, onChompKeep) {
// not sure how to handle the match that arrives here
return stringifyString(item, ctx, onComment, onChompKeep)
}
} as ScalarTag;
const jsobj = { ":some_rules": [ { "id": 1 }, { "id": 2 }, { "id": 3 } ]};
console.log(YAML.stringify(
jsobj,
{ customTags: [someRuleRubyTag] }
)); It seems possible as I get that custom tag back which is great but the value for that tag I can't seem to get (the list item). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The tag method that you're missing isn't actually |
Beta Was this translation helpful? Give feedback.
The tag method that you're missing isn't actually
stringify
, butcreateNode
. Tags that extend maps and sequences require you to define how to turn e.g. something like a!ruby/object:SomeRule
into a YAML map. Once you've done that, you should not need to customise the stringifier at all.