by PaulHoule · 5y ago · 0 comments
For one of my art projects I make YAML files that describe the front side, back side, and web side of a "three sided card". I generate these out of several templates, currently using ordinary string templating.
I'd love to be able to load a YAML file and add something programatically to the list and have the list stay in the same format that it was in, so if it was a
[1,2,3]
list I get [1,2,3,4]
if it was a - 1
- 2
- 3
list I want - 1
- 2
- 3
- 4
sadly I'm the only one who thinks this way. import assert from 'assert'
import { parseDocument } from 'yaml'
const flowDoc = parseDocument(`[1,2,3]`)
flowDoc.add(4)
assert(flowDoc.toString(), '[ 1, 2, 3, 4 ]\n')
const blockDoc = parseDocument(`\
- 1
- 2
- 3`)
blockDoc.add(4)
assert(
blockDoc.toString(),
`\
- 1
- 2
- 3
- 4
`
)E.g. I tried exactly the same thing, and it was quiet difficult with Rust. Because the way to parse it usually is with serde and it just removes the AST of course.
In the end I gave up, and just used JSON for my use case.