Skip to content

fix: escape backslash in frontmatter regex - #2

Open
Jiaoyc224 wants to merge 1 commit into
Fectivnfy112357:mainfrom
Jiaoyc224:fix/frontmatter-regex
Open

fix: escape backslash in frontmatter regex#2
Jiaoyc224 wants to merge 1 commit into
Fectivnfy112357:mainfrom
Jiaoyc224:fix/frontmatter-regex

Conversation

@Jiaoyc224

Copy link
Copy Markdown

Summary

Fix a frontmatter parsing bug in lib/index.js.

Problem

In parseFrontmatter(), the regex is built from a string:

const line = fm.match(new RegExp('^' + key + ':\s*(.+)$', 'm'));

In JavaScript string literals, \s is not an escape sequence and becomes s.
The resulting regex is /^key:s*(.+)$/m instead of /^key:\s*(.+)$/m.

This can drop the leading s from a frontmatter value when the value starts
with s and has no space after the colon, e.g. description:search GitHub repos.

Fix

- const line = fm.match(new RegExp('^' + key + ':\s*(.+)$', 'm'));
+ const line = fm.match(new RegExp('^' + key + ':\\s*(.+)$', 'm'));

Test

const re = new RegExp('^description:\\s*(.+)$', 'm');
console.log(re); // /^description:\s*(.+)$/m
console.log('description:search repos'.match(re)?.[1]); // "search repos"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant