Skip to content

Commit

Permalink
Fix frontmatter parsing with utf8 bom (#12664)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Dec 6, 2024
1 parent f13417b commit a71e9b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-wasps-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': patch
---

Fixes frontmatter parsing if file is encoded in UTF8 with BOM
7 changes: 4 additions & 3 deletions packages/markdown/remark/src/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export function isFrontmatterValid(frontmatter: Record<string, any>) {
}

// Capture frontmatter wrapped with `---`, including any characters and new lines within it.
// Only capture if it exists near the top of the file (whitespaces between the start of file and
// the start of `---` are allowed)
const frontmatterRE = /^\s*---([\s\S]*?\n)---/;
// Only capture if `---` exists near the top of the file, including:
// 1. Start of file (including if has BOM encoding)
// 2. Start of file with any whitespace (but `---` must still start on a new line)
const frontmatterRE = /(?:^\uFEFF?|^\s*\n)---([\s\S]*?\n)---/;
export function extractFrontmatter(code: string): string | undefined {
return frontmatterRE.exec(code)?.[1];
}
Expand Down
26 changes: 23 additions & 3 deletions packages/markdown/remark/test/frontmatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { extractFrontmatter, parseFrontmatter } from '../dist/index.js';

const bom = '\uFEFF';

describe('extractFrontmatter', () => {
it('works', () => {
const yaml = `\nfoo: bar\n`;
assert.equal(extractFrontmatter(`---${yaml}---`), yaml);
assert.equal(extractFrontmatter(` ---${yaml}---`), yaml);
assert.equal(extractFrontmatter(`${bom}---${yaml}---`), yaml);
assert.equal(extractFrontmatter(`\n---${yaml}---`), yaml);
assert.equal(extractFrontmatter(`\n \n---${yaml}---`), yaml);
assert.equal(extractFrontmatter(`---${yaml}---\ncontent`), yaml);
assert.equal(extractFrontmatter(`${bom}---${yaml}---\ncontent`), yaml);
assert.equal(extractFrontmatter(`\n\n---${yaml}---\n\ncontent`), yaml);
assert.equal(extractFrontmatter(`\n \n---${yaml}---\n\ncontent`), yaml);
assert.equal(extractFrontmatter(` ---${yaml}---`), undefined);
assert.equal(extractFrontmatter(`---${yaml} ---`), undefined);
assert.equal(extractFrontmatter(`text\n---${yaml}---\n\ncontent`), undefined);
});
});
Expand All @@ -24,10 +29,10 @@ describe('parseFrontmatter', () => {
rawFrontmatter: yaml,
content: '',
});
assert.deepEqual(parseFrontmatter(` ---${yaml}---`), {
assert.deepEqual(parseFrontmatter(`${bom}---${yaml}---`), {
frontmatter: { foo: 'bar' },
rawFrontmatter: yaml,
content: ' ',
content: bom,
});
assert.deepEqual(parseFrontmatter(`\n---${yaml}---`), {
frontmatter: { foo: 'bar' },
Expand All @@ -44,6 +49,11 @@ describe('parseFrontmatter', () => {
rawFrontmatter: yaml,
content: '\ncontent',
});
assert.deepEqual(parseFrontmatter(`${bom}---${yaml}---\ncontent`), {
frontmatter: { foo: 'bar' },
rawFrontmatter: yaml,
content: `${bom}\ncontent`,
});
assert.deepEqual(parseFrontmatter(`\n\n---${yaml}---\n\ncontent`), {
frontmatter: { foo: 'bar' },
rawFrontmatter: yaml,
Expand All @@ -54,6 +64,16 @@ describe('parseFrontmatter', () => {
rawFrontmatter: yaml,
content: '\n \n\n\ncontent',
});
assert.deepEqual(parseFrontmatter(` ---${yaml}---`), {
frontmatter: {},
rawFrontmatter: '',
content: ` ---${yaml}---`,
});
assert.deepEqual(parseFrontmatter(`---${yaml} ---`), {
frontmatter: {},
rawFrontmatter: '',
content: `---${yaml} ---`,
});
assert.deepEqual(parseFrontmatter(`text\n---${yaml}---\n\ncontent`), {
frontmatter: {},
rawFrontmatter: '',
Expand Down

0 comments on commit a71e9b9

Please sign in to comment.