fix: treat a string passed to stringify as opaque content #190

Open
spokodev wants to merge 1 commit from spokodev/fix-stringify-string-content into master
spokodev commented 2026-06-22 20:42:47 +00:00 (Migrated from github.com)

Problem

matter.stringify(file, data) documents the string form of file as "The content string to append to stringified front-matter" (and "appending that to the given string"). But when file is a string it runs matter(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 both content and data:

const matter = require('gray-matter');

const body = '---\nnot: real\n---\nHello world'; // intended as opaque content
const out  = matter.stringify(body, { title: 'Home' });
const back = matter(out);

back.data;    // { not: 'real', title: 'Home' }   <- body key leaked into front matter
back.content; // 'Hello world\n'                  <- '---\nnot: real\n---' lost from the body

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 empty data keeps stringify'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).

### Problem `matter.stringify(file, data)` documents the string form of `file` as *"The content string to append to stringified front-matter"* (and *"appending that to the given string"*). But when `file` is a string it runs `matter(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 both `content` and `data`: ```js const matter = require('gray-matter'); const body = '---\nnot: real\n---\nHello world'; // intended as opaque content const out = matter.stringify(body, { title: 'Home' }); const back = matter(out); back.data; // { not: 'real', title: 'Home' } <- body key leaked into front matter back.content; // 'Hello world\n' <- '---\nnot: real\n---' lost from the body ``` 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 empty `data` keeps `stringify`'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).
This pull request has changes conflicting with the target branch.
  • index.js
  • test/stringify.js
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin "spokodev/fix-stringify-string-content:spokodev/fix-stringify-string-content"
git switch "spokodev/fix-stringify-string-content"
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rtlabs-llm-agents/gms-gray-matter!190
No description provided.