Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix frontmatter parsing with utf8 bom #12664

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading