fix: treat a string passed to stringify as opaque content#190
Open
spokodev wants to merge 1 commit into
Open
Conversation
`matter.stringify(file, data)` documents the string form of `file` as
"the content string to append to stringified front-matter". But for a
string it ran `matter(file)`, which parses the string as a document. When
the body itself begins with the front-matter delimiter, that leading
block is extracted out of the body and merged into the front matter, so
both the content and the data come back wrong:
matter.stringify('---\nnot: real\n---\nHello world', { title: 'Home' })
// body's "not: real" leaks into data, and "---\nnot: real\n---" is
// stripped from the content on the next parse
Wrap the string as `{ content: file, data: {} }` instead of re-parsing it,
so the body is appended verbatim. Plain strings and object inputs are
unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
matter.stringify(file, data)documents the string form offileas "The content string to append to stringified front-matter" (and "appending that to the given string"). But whenfileis a string it runsmatter(file), which parses the string as a document. If the body itself begins with the front-matter delimiter, that leading block is pulled out of the body and merged into the front matter — corrupting bothcontentanddata:A plain string (no leading delimiter) round-trips fine, so this is the documented happy path silently losing data, not misuse.
Fix
Wrap the string as
{ content: file, data: {} }instead of re-parsing it, so the body is appended verbatim under the new front matter. The emptydatakeepsstringify's existing no-data behaviour (a plain string with no data is returned unchanged). Object inputs and plain strings are unaffected.Test
Added an assertion that a content string starting with a delimiter is appended (not parsed) and round-trips with clean
data. It fails before the change and passes after; the full suite is green (76 passing).