Skip to content

refactor: replace gray-matter with an in-house frontmatter splitter - #3947

Merged
dyoshikawa merged 3 commits into
mainfrom
refactor/drop-gray-matter
Nov 24, 2026
Merged

dyoshikawa merged 3 commits into
mainfrom
refactor/drop-gray-matter

Conversation

@dyoshikawa

@dyoshikawa dyoshikawa commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Replaces gray-matter with a small in-house frontmatter splitter in src/utils/frontmatter.ts.

Why:

  • gray-matter 4.0.3 was last published in 2023-07.
  • It needed a local pnpm patch to remove its eval()-based JavaScript engine. pnpm patches and overrides only apply inside this repository, so installed copies of rulesync ran unpatched gray-matter on js-yaml 3. The language check and the engine override in frontmatter.ts were what actually protected users.
  • It pulled in five more small packages: section-matter, strip-bom-string, extend-shallow, is-extendable and kind-of.

What changed

  • Parsing: parseFrontmatter splits the file using gray-matter's grammar. The rules:
    • one BOM is skipped;
    • a ---- line is a horizontal rule, not a fence;
    • the fence language is read the same way (only YAML is accepted, as before);
    • the first \n--- closes the block;
    • one line break after the closing fence is dropped;
    • an unclosed fence takes the rest of the file;
    • a comment-only block is empty;
    • an empty file still counts as having (empty) frontmatter.
  • Stringifying: stringifyFrontmatter writes ---\n<yaml>\n---\n followed by the body with a trailing newline. An empty mapping writes the body alone, as gray-matter did.
  • YAML library: frontmatter is still parsed and dumped with js-yaml 4, now a direct dependency under the npm alias js-yaml-v4 (npm:[email protected], still maintained, already in the lockfile through secretlint). Minimal typings are in src/types/js-yaml-v4.d.ts. The avoidBlockScalars path keeps using js-yaml 5, as it did before. js-yaml 5 is not used for frontmatter parsing because:
    • it rejects YAML that has always been accepted. For example, a flow list whose closing ] sits at column 0 (covered by an existing Trae test) fails with deficient indentation.
    • it drops v4's default timestamp, merge and binary tags;
    • by default it stops folding long strings that have no space to break at, which would change generated files.
  • Hardening, from review:
    • gray-matter's blank-block regex /^\s*#[^\n]+/gm backtracked quadratically (about 10 s on 65k blank lines). It is now a line-by-line check. The one known difference: a lone \r now ends a comment line, as it does in YAML.
    • The raw size guard now measures the exact block handed to js-yaml. A whitespace-only fence language or a file with no line break could previously get past it.
  • Removed: the gray-matter dependency, patches/[email protected], the gray-matter>js-yaml override and the patchedDependencies entry.
  • Comments: comments in trae-rule.ts, factorydroid-check.ts and content-equivalence.ts that referred to gray-matter now refer to parseFrontmatter.

Verification

  • pnpm cicheck passes: 17266 tests, with lint, typecheck, cspell and secretlint clean.
  • A one-off differential check compared the old implementation (on main, with gray-matter installed) against the new one:
    • inputs: every .md/.mdc file in the repository and in node_modules (986 files) plus about 35 edge cases (BOM, CRLF, ---yaml, ----, unclosed fences, !!binary, !!set, merge keys, complex keys, dates, a column-0 ], octal);
    • also stringify round-trips in both modes;
    • result: 2375 comparisons, 0 differences.
  • New unit tests pin the splitting rules, the empty-frontmatter and trailing-newline output, and the js-yaml 4 tags (dates, <<, !!binary, complex keys).
  • Removed tests: the two tests that exercised the gray-matter patch and engine override directly, and the gray-matter module mock.

Note for npm users

Installed rulesync previously parsed frontmatter with js-yaml 3, because the repository's override does not ship. It now uses js-yaml 4 everywhere, matching what the repository and the release binaries already used. The only difference observed between the two versions is how octal-looking scalars are read: 012 and 0o12.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 3 commits November 24, 2026 17:38
gray-matter 4.0.3 has not been published since 2023-07 and needed a local
pnpm patch to drop its eval-based JavaScript engine, a patch that never
reaches installed copies of rulesync. Frontmatter is now split in
src/utils/frontmatter.ts with the same grammar gray-matter used (BOM,
`----` rule, fence language, first `\n---` closes the block, one line
break dropped after it), and the YAML is still parsed and dumped with
js-yaml 4, now a direct dependency under the `js-yaml-v4` alias, so every
file reads and writes exactly as before. js-yaml 5 is stricter (it
rejects a flow list whose closing `]` sits at column 0) and drops v4's
timestamp/merge/binary tags, so it is not used for frontmatter.

Removes gray-matter and its transitive deps (section-matter,
strip-bom-string, extend-shallow, is-extendable, kind-of), the
gray-matter patch and its js-yaml override.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…n the split block

- Replace gray-matter's `/^\s*#[^\n]+/gm` blank-block check with a
  line-by-line test; its `\s*` spans line breaks and backtracked
  quadratically (about 10 s on 65k blank lines).
- Apply the raw size guard to the block splitFrontmatter returns, so a
  whitespace-only fence language or a file with no line break can no
  longer hand the parser more than MAX_FRONTMATTER_RAW_CHARS.
- Pin the js-yaml 4 folding of long unbreakable values, an unclosed
  language fence and a null block with tests.
- Correct comments: gray-matter ran on js-yaml 4 only in this repository
  and the release binaries, and the factorydroid strip keeps skill
  metadata out of the check body rather than out of its frontmatter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
@dyoshikawa
dyoshikawa merged commit 1c6b6dd into main Nov 24, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the refactor/drop-gray-matter branch November 24, 2026 09:00
@dyoshikawa

Copy link
Copy Markdown
Owner Author

@dyoshikawa Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant