Skip to content

[BUG] lib/index.js frontmatter regex uses \s in string literal, becomes s* #1

Description

@Jiaoyc224

Bug description

In lib/index.js, parseFrontmatter() builds a RegExp 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.

Reproduction

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

Expected behavior

The regex should match zero or more whitespace characters (\s*), not zero or
more literal s characters.

Environment

  • github-explore v3.1.0
  • Installed as a dsh profile bundle on Windows

Suggested fix

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions