An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): give the frontmatter parser a plain fields record (#7620)
Cindy Zhang59c4ef79b8e4
diff --git a/.changeset/markdown-frontmatter-plain-fields.md b/.changeset/markdown-frontmatter-plain-fields.mdnew file mode 100644index 0000000000..4b4f3b10e8--- /dev/null+++ b/.changeset/markdown-frontmatter-plain-fields.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `createMarkdownFrontmatter` now hands its `parse` function a plain object, so a parser that returns the decoded fields unchanged (`parse: fields => fields`) is accepted as metadata. Before, the record had no prototype, failed the JSON-like metadata check, and the document fell back to readable text with its frontmatter still showing.++@cixzhangdiff --git a/packages/core/src/Markdown/plugins/frontmatter.test.tsx b/packages/core/src/Markdown/plugins/frontmatter.test.tsxindex 2bdb7d6596..1f92a330f3 100644--- a/packages/core/src/Markdown/plugins/frontmatter.test.tsx+++ b/packages/core/src/Markdown/plugins/frontmatter.test.tsx@@ -117,6 +117,31 @@ describe('createMarkdownFrontmatter', () => {).not.toThrow();});+ it('accepts the decoded fields record as metadata (identity parse)', () => {+ const fields = createMarkdownFrontmatter({+ name: 'fields',+ parse: record => record,+ });+ const source = '---\ntitle: Release notes\n__proto__: kept\n---\n# Shipped';++ const parsed = fields.parse(source);+ expect(parsed).toEqual({+ status: 'match',+ contentStart: source.indexOf('# Shipped'),+ metadata: {title: 'Release notes', ['__proto__']: 'kept'},+ });+ if (parsed.status !== 'match') {+ throw new Error('expected a frontmatter match');+ }+ // A field named like a prototype key stays an ordinary own field.+ expect(Object.hasOwn(parsed.metadata, '__proto__')).toBe(true);+ expect(Object.getPrototypeOf(parsed.metadata)).toBe(Object.prototype);++ const root = parseMarkdownAst(source, {plugins: [fields.plugin]});+ expect(fields.getMetadata(root)).toEqual(parsed.metadata);+ expect(JSON.stringify(root)).not.toContain('title: Release notes');+ });+it('returns a transform plugin that composes in the ordered pipeline', () => {expectTypeOf<MarkdownTransform>().toBeFunction();const root = parseMarkdownAst(SOURCE, {plugins: [documentMetadata.plugin]});diff --git a/packages/core/src/Markdown/plugins/frontmatter.ts b/packages/core/src/Markdown/plugins/frontmatter.tsindex 1228a0d1a0..7ce3304b8e 100644--- a/packages/core/src/Markdown/plugins/frontmatter.ts+++ b/packages/core/src/Markdown/plugins/frontmatter.ts@@ -104,18 +104,22 @@ function readFrontmatter(source: string):if (close == null) {return {status: 'defer'};}- const fields: Record<string, string> = Object.create(null);+ const entries = new Map<string, string>();for (const line of body.slice(0, close.index).split(/\r?\n/)) {if (line.trim() === '') {continue;}const separator = line.indexOf(':');const key = line.slice(0, separator).trim();- if (separator <= 0 || key === '' || Object.hasOwn(fields, key)) {+ if (separator <= 0 || key === '' || entries.has(key)) {return {status: 'none'};}- fields[key] = line.slice(separator + 1).trim();+ entries.set(key, line.slice(separator + 1).trim());}+ // A plain object, so a parser that returns the record unchanged passes the+ // JSON-like metadata check. `fromEntries` defines every key as an own data+ // property, so a field named `__proto__` stays a field.+ const fields: Record<string, string> = Object.fromEntries(entries);let end = openingLength + close.index + close[0].length;if (source.startsWith('\r\n', end)) {end += 2;