mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
Fix frontmatter parsing with utf8 bom (#12664)
This commit is contained in:
parent
f13417bfbf
commit
a71e9b93b3
3 changed files with 32 additions and 6 deletions
5
.changeset/unlucky-wasps-refuse.md
Normal file
5
.changeset/unlucky-wasps-refuse.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/markdown-remark': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes frontmatter parsing if file is encoded in UTF8 with BOM
|
|
@ -11,9 +11,10 @@ export function isFrontmatterValid(frontmatter: Record<string, any>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capture frontmatter wrapped with `---`, including any characters and new lines within it.
|
// 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
|
// Only capture if `---` exists near the top of the file, including:
|
||||||
// the start of `---` are allowed)
|
// 1. Start of file (including if has BOM encoding)
|
||||||
const frontmatterRE = /^\s*---([\s\S]*?\n)---/;
|
// 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 {
|
export function extractFrontmatter(code: string): string | undefined {
|
||||||
return frontmatterRE.exec(code)?.[1];
|
return frontmatterRE.exec(code)?.[1];
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,16 +2,21 @@ import assert from 'node:assert/strict';
|
||||||
import { describe, it } from 'node:test';
|
import { describe, it } from 'node:test';
|
||||||
import { extractFrontmatter, parseFrontmatter } from '../dist/index.js';
|
import { extractFrontmatter, parseFrontmatter } from '../dist/index.js';
|
||||||
|
|
||||||
|
const bom = '\uFEFF';
|
||||||
|
|
||||||
describe('extractFrontmatter', () => {
|
describe('extractFrontmatter', () => {
|
||||||
it('works', () => {
|
it('works', () => {
|
||||||
const yaml = `\nfoo: bar\n`;
|
const yaml = `\nfoo: bar\n`;
|
||||||
assert.equal(extractFrontmatter(`---${yaml}---`), yaml);
|
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---${yaml}---`), yaml);
|
||||||
assert.equal(extractFrontmatter(`\n \n---${yaml}---`), yaml);
|
assert.equal(extractFrontmatter(`\n \n---${yaml}---`), yaml);
|
||||||
assert.equal(extractFrontmatter(`---${yaml}---\ncontent`), 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(`\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);
|
assert.equal(extractFrontmatter(`text\n---${yaml}---\n\ncontent`), undefined);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -24,10 +29,10 @@ describe('parseFrontmatter', () => {
|
||||||
rawFrontmatter: yaml,
|
rawFrontmatter: yaml,
|
||||||
content: '',
|
content: '',
|
||||||
});
|
});
|
||||||
assert.deepEqual(parseFrontmatter(` ---${yaml}---`), {
|
assert.deepEqual(parseFrontmatter(`${bom}---${yaml}---`), {
|
||||||
frontmatter: { foo: 'bar' },
|
frontmatter: { foo: 'bar' },
|
||||||
rawFrontmatter: yaml,
|
rawFrontmatter: yaml,
|
||||||
content: ' ',
|
content: bom,
|
||||||
});
|
});
|
||||||
assert.deepEqual(parseFrontmatter(`\n---${yaml}---`), {
|
assert.deepEqual(parseFrontmatter(`\n---${yaml}---`), {
|
||||||
frontmatter: { foo: 'bar' },
|
frontmatter: { foo: 'bar' },
|
||||||
|
@ -44,6 +49,11 @@ describe('parseFrontmatter', () => {
|
||||||
rawFrontmatter: yaml,
|
rawFrontmatter: yaml,
|
||||||
content: '\ncontent',
|
content: '\ncontent',
|
||||||
});
|
});
|
||||||
|
assert.deepEqual(parseFrontmatter(`${bom}---${yaml}---\ncontent`), {
|
||||||
|
frontmatter: { foo: 'bar' },
|
||||||
|
rawFrontmatter: yaml,
|
||||||
|
content: `${bom}\ncontent`,
|
||||||
|
});
|
||||||
assert.deepEqual(parseFrontmatter(`\n\n---${yaml}---\n\ncontent`), {
|
assert.deepEqual(parseFrontmatter(`\n\n---${yaml}---\n\ncontent`), {
|
||||||
frontmatter: { foo: 'bar' },
|
frontmatter: { foo: 'bar' },
|
||||||
rawFrontmatter: yaml,
|
rawFrontmatter: yaml,
|
||||||
|
@ -54,6 +64,16 @@ describe('parseFrontmatter', () => {
|
||||||
rawFrontmatter: yaml,
|
rawFrontmatter: yaml,
|
||||||
content: '\n \n\n\ncontent',
|
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`), {
|
assert.deepEqual(parseFrontmatter(`text\n---${yaml}---\n\ncontent`), {
|
||||||
frontmatter: {},
|
frontmatter: {},
|
||||||
rawFrontmatter: '',
|
rawFrontmatter: '',
|
||||||
|
|
Loading…
Reference in a new issue