Skip to content

Commit 79451c1

Browse files
committedApr 22, 2024
fix(cli): handle frontmatter ending with CRLF
On Windows, lines usually end with `\r\n`/CRLF/␍␊. Although YAML supports this as a line-break, our regex was incorrectly breaking this up, which was causing the `\r` character to be appending into some strings, instead of `\r\n`.
1 parent 17ef4a2 commit 79451c1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
 

‎packages/cli/src/frontmatter.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { extractFrontMatter } from './frontmatter.js';
3+
4+
describe('extractFrontMatter()', () => {
5+
it(String.raw`should handle \r\n (␍␊)`, () => {
6+
const text = '---\r\nid: test\r\n---\r\ninfo\r\n';
7+
const result = extractFrontMatter(text);
8+
expect(result.metadata.id).toBe('test');
9+
});
10+
});

‎packages/cli/src/frontmatter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { parseDocument, type Document, YAMLMap, isMap } from 'yaml';
55

6-
const frontMatterRegex = /^-{3}\s*[\n\r](.*?)[\n\r]-{3}\s*[\n\r]+/s;
6+
const frontMatterRegex = /^-{3}\s*[\n\r](.*?[\n\r])-{3}\s*[\n\r]+/s;
77
const urlIDRegex = /(?<baseURL>.*)\/d\/(?<documentID>[\w-]+)/;
88

99
type UrlID = `${string}/d/${string}`;

0 commit comments

Comments
 (0)
Failed to load comments.