Skip to content

Commit 75a25ca

Browse files
committed
Fix front matter matching behaviour
1 parent 79c9236 commit 75a25ca

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/lib/services/contents/file/parse.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export const parseEntryFile = async ({
6060
parserConfig: { extension, format, frontmatterDelimiter },
6161
},
6262
}) => {
63-
text = text.trim();
63+
// Normalize line breaks
64+
text = text.trim().replace(/\r\n?/g, '\n');
6465

6566
format ||= /** @type {FileFormat | undefined} */ (
6667
['md', 'markdown'].includes(extension ?? '') || path.match(/\.(?:md|markdown)$/)
@@ -98,16 +99,13 @@ export const parseEntryFile = async ({
9899

99100
if (format.match(/^(?:yaml|toml|json)-frontmatter$/)) {
100101
const [startDelimiter, endDelimiter] = getFrontMatterDelimiters(format, frontmatterDelimiter);
102+
const sd = escapeRegExp(startDelimiter);
103+
const ed = escapeRegExp(endDelimiter);
104+
// Front matter matching: allow an empty head
105+
const regex = new RegExp(`^${sd}$\\n(?:(?<head>.*?)$\\n)?${ed}$(?:\\n(?<body>.+))?`, 'ms');
106+
const { head, body } = text.match(regex)?.groups ?? {};
101107

102-
const [, head, body = ''] =
103-
text.match(
104-
new RegExp(
105-
`^${escapeRegExp(startDelimiter)}\\n(.+?)\\n${escapeRegExp(endDelimiter)}(?:\\n(.+))?`,
106-
'ms',
107-
),
108-
) ?? [];
109-
110-
if (!head) {
108+
if (!head && !body) {
111109
throw new Error('No front matter block found');
112110
}
113111

0 commit comments

Comments
 (0)