Skip to content

fix: allow spaces around the language name in fenced code blocks (fixes #62) - #123

Open
Ultroncn wants to merge 1 commit into
developit:mainfrom
Ultroncn:fix/fenced-code-language-spaces
Open

Ultroncn wants to merge 1 commit into
developit:mainfrom
Ultroncn:fix/fenced-code-language-spaces

Conversation

@Ultroncn

Copy link
Copy Markdown

Problem

A fenced code block whose info string has spaces around the language name is not recognized at all and passes through literally:

snarkdown('```js \ncode\n```')
// before: "```js \ncode\n```"  (literal, unparsed)
// after:  "<pre class=\"code js\"><code class=\"language-js\">code</code></pre>"

Same for a trailing tab (js\t`) or spaces on both sides ( js ). A leading space already worked (^``` (\w)`), the trailing side did not. Reported in #62.

Change

One change in the tokenizer in src/index.js — the fence-opening pattern now allows spaces/tabs on both sides of the language name:

-(?:^``` *(\w*)\n...
+(?:^```[ \t]*(\w*)[ \t]*\n...

Tests

Added a test in test/index.js (code & quotes suite) covering trailing space, both-side spaces, trailing tab, and a bare fence with a trailing space. Verified it fails on the old code (first assertion: expected literal passthrough to equal the parsed <pre> output). Full suite: npm test (eslint + mocha) — 34 passing, 0 failing.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T00:21:38.198961Z c8c1a46 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c8c1a46c0c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/index.js
/** Parse Markdown into an HTML String. */
export default function parse(md, prevLinks) {
let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,
let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^```[ \t]*(\w*)[ \t]*\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid overlapping whitespace matches when the language is absent

For an unclosed fence without a language, the language capture can be empty, allowing both surrounding whitespace repetitions to consume the same characters and causing quadratic backtracking. On Node 24, snarkdown('```' + ' '.repeat(40000) + '\nbody') took about 1.75 seconds at this commit versus 2 ms in its parent. Applications rendering user-supplied Markdown can therefore have their event loop or UI stalled by a small document. Make the nonempty language and its trailing whitespace optional together so that a language-less opener consumes its whitespace only once.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the whitespace overlap in the diff. I haven’t independently measured the slowdown. Would you be open to a small revision that removes the overlap while preserving support for spaces and tabs around the language name? I'd appreciate your guidance before updating the PR.

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