Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 118 additions & 1 deletion src/utils/frontmatterParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { expect, test } from 'bun:test'
import { splitPathInFrontmatter } from './frontmatterParser.ts'
import {
parseFrontmatter,
splitPathInFrontmatter,
} from './frontmatterParser.ts'

// splitPathInFrontmatter is the public entry that flatMaps each comma-separated
// part through the (private) expandBraces helper, so it exercises the brace
Expand Down Expand Up @@ -53,3 +56,117 @@ test('keeps an empty brace group literal instead of yielding an empty path', ()
test('keeps a literal empty group while still expanding later groups', () => {
expect(splitPathInFrontmatter('{}/{a,b}')).toEqual(['{}/a', '{}/b'])
})

// The opening `---` was anchored but the closing one was not, and `[\s\S]*?` is
// lazy, so parsing stopped at the first `---` appearing anywhere -- including
// inside a value. Every .md frontmatter consumer is affected: agents, skills,
// slash commands, output styles and memory files all go through here.

test('does not end the block at a --- inside a quoted value', () => {
const { frontmatter, content } = parseFrontmatter(
'---\nname: r\ndescription: "Reviews code --- thoroughly"\n---\n\nBody.\n',
)

// Previously the block ended at the `---` inside the quotes: the description
// was truncated, and the rest of the frontmatter plus the real delimiter
// leaked into the body that is sent to the model.
expect(frontmatter.description).toBe('Reviews code --- thoroughly')
expect(frontmatter.name).toBe('r')
// The blank separator line after the closing delimiter is consumed, so the
// body starts at the first real line (not with a leading newline).
expect(content).toBe('Body.\n')
expect(content).not.toContain('---')
})

test('does not end the block at a --- line inside a block scalar', () => {
const { frontmatter, content } = parseFrontmatter(
'---\ndescription: |\n step one\n ---\n step two\n---\nBody\n',
)

expect(frontmatter.description).toBe('step one\n---\nstep two\n')
expect(content).toBe('Body\n')
})

test('does not leak the frontmatter tail into the body', () => {
// Whether the YAML layer can make sense of an unquoted value containing
// `---` is its own question; what must not happen is the delimiter being
// found mid-value, which spilled the remaining frontmatter lines and a stray
// `---` into the body.
const { content } = parseFrontmatter(
'---\nname: a\nsummary: uses --- as a separator\n---\nBody\n',
)

expect(content).toBe('Body\n')
})

test('parses the ordinary shapes exactly as before', () => {
const simple = parseFrontmatter('---\nname: a\n---\nBody\n')
expect(simple.frontmatter).toEqual({ name: 'a' })
expect(simple.content).toBe('Body\n')

const empty = parseFrontmatter('---\n---\nBody\n')
expect(empty.frontmatter).toEqual({})
expect(empty.content).toBe('Body\n')

// Trailing spaces are allowed on either delimiter.
const padded = parseFrontmatter('--- \nname: a\n--- \nBody\n')
expect(padded.frontmatter).toEqual({ name: 'a' })
expect(padded.content).toBe('Body\n')
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// Tabs too: the trailing run is [ \t]* on both delimiters.
const tabbed = parseFrontmatter('---\t\nname: a\n---\t\nBody\n')
expect(tabbed.frontmatter).toEqual({ name: 'a' })
expect(tabbed.content).toBe('Body\n')

// Frontmatter that ends at EOF with no trailing newline.
const atEof = parseFrontmatter('---\nname: a\n---')
expect(atEof.frontmatter).toEqual({ name: 'a' })
expect(atEof.content).toBe('')
})

test('parses CRLF-delimited frontmatter (Windows-authored files)', () => {
// `---\r\n` must be recognized: the delimiter matcher accepts an optional
// carriage return on the open, the captured body lines, and the close.
const crlf = parseFrontmatter('---\r\nname: a\r\n---\r\nBody\r\n')
expect(crlf.frontmatter).toEqual({ name: 'a' })
expect(crlf.content).toBe('Body\r\n')

// Empty CRLF frontmatter.
const empty = parseFrontmatter('---\r\n---\r\nBody\r\n')
expect(empty.frontmatter).toEqual({})
expect(empty.content).toBe('Body\r\n')
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

test('consumes blank and whitespace-only separator lines after the close', () => {
// Bodies are passed into prompts untrimmed, so a conventional blank line
// between frontmatter and body must not survive as a leading newline. The
// pre-anchor regex consumed it via `\s*`; the anchored one has to as well.
const oneBlank = parseFrontmatter('---\nname: a\n---\n\nBody\n')
expect(oneBlank.content).toBe('Body\n')

const manyBlank = parseFrontmatter('---\nname: a\n---\n\n\n\nBody\n')
expect(manyBlank.content).toBe('Body\n')

// Whitespace-only separator lines (spaces/tabs) are consumed too.
const wsOnly = parseFrontmatter('---\nname: a\n---\n \n\t\nBody\n')
expect(wsOnly.content).toBe('Body\n')

// CRLF blank separators.
const crlfBlank = parseFrontmatter('---\r\nname: a\r\n---\r\n\r\nBody\r\n')
expect(crlfBlank.content).toBe('Body\r\n')
})

test('leaves a file without frontmatter untouched', () => {
const markdown = 'Just a body.\n\nWith a --- rule in it.\n'
const { frontmatter, content } = parseFrontmatter(markdown)

expect(frontmatter).toEqual({})
expect(content).toBe(markdown)
})

test('does not treat a body horizontal rule as frontmatter', () => {
// The document does not open with a delimiter, so nothing is consumed even
// though `---` lines appear later.
const markdown = '# Title\n\n---\n\nSection\n\n---\n'
expect(parseFrontmatter(markdown).content).toBe(markdown)
})
27 changes: 26 additions & 1 deletion src/utils/frontmatterParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,32 @@ function quoteProblematicValues(frontmatterText: string): string {
return result.join('\n')
}

export const FRONTMATTER_REGEX = /^---\s*\n([\s\S]*?)---\s*\n?/
/**
* The opening delimiter is anchored but the closing one must be too.
*
* `[\s\S]*?` is lazy, so an unanchored close stops at the first `---` appearing
* anywhere -- including in the middle of a value. `description: Reviews code
* --- thoroughly` ends the block early: the description is truncated to
* "Reviews code", and the rest of the frontmatter plus the real delimiter leak
* into the body that is sent to the model. A `---` line inside a block scalar
* has the same effect.
*
* Requiring the captured block to be empty or to end at a line break pins the
* close to the start of a line. The `m` flag would do the same for the close
* but would also un-anchor the open, letting a horizontal rule partway down a
* body be read as frontmatter. Only spaces and tabs may sit between the close
* `---` and its line terminator -- letting `\s*` match the terminator itself
* accepted a bare `---` with no line ending mid-document.
*
* Once that terminator (or EOF) is confirmed, the trailing `\s*` consumes the
* conventional blank separator line(s) between frontmatter and body, matching
* the pre-anchor behavior. Skill and plugin-command bodies are passed into
* prompts untrimmed, so leaving that blank line in would prepend a stray
* newline to the model instructions. This consumption runs only after a valid
* line-terminated close, so it cannot revive the mid-document false close.
*/
export const FRONTMATTER_REGEX =
/^---[ \t]*\r?\n((?:[\s\S]*?\r?\n)?)---[ \t]*(?:\r?\n\s*|$)/

/**
* Parses markdown content to extract frontmatter and content
Expand Down