jxson / front-matter

Extract YAML front matter from strings
MIT License
676 stars 76 forks source link

Serializer too #70

Open peterbe opened 4 years ago

peterbe commented 4 years ago

I'd love to be able to do fm.serialize(attributes, body).

This should work:

const metadata = {foo: "bar"}
const payload = "Hi there!"
const combined = fm.serialize(metadata, payload)
const parsed = fm(combined);
assert(JSON.stringify(parsed.attributes) === JSON.stringify(metadata))
assert(parsed.body === payload)
peterbe commented 4 years ago

And, it should use yaml.safeDump. So, perhaps to be complete, this should work too:

const metadata = {match: "!!js/regexp /pattern/gim"}
const payload = "Hi there!"
const combined = fm.serialize(metadata, payload, {unsafe: true} ) // Or something
const parsed = fm(combined);
assert(JSON.stringify(parsed.attributes) === JSON.stringify(metadata))
assert(parsed.body === payload)