4ee44f7f21 feat: add linkParenPossible to lexer state to fail fast for link token generation (#4070)
* perf: fast-fail link tokenizing when no ')' remains ahead

The inline link rule backtracks quadratically over unterminated
link sequences. '[a](b'.repeat(n) parses in O(n^2) — 97KB took
~2.5s — because at every '[' the regex's greedy unquoted-href run
re-partitions the whole remainder while hunting for a closing ')'
that never comes.

Lexer.inlineTokens now anchors one paren scan per inline run
(lastIndexOf(')') over the paragraph), and Tokenizer.link consults
it: since every src it receives during the run is a suffix of that
anchor, 'is there a ) ahead?' is O(1). When none remains the regex
cannot match anyway, so skipping is semantics-preserving; anchors
are saved/restored around nested runs (e.g. link labels) and live
in a WeakMap so they stay off the public tokenizer API.

Measured on Node 24:

  '[a](b'.repeat(20000)   ~2500ms -> ~24ms
  '[a](b'.repeat(80000)   (OOM-adjacent) -> ~60ms

Differential check: 1506 generated documents (links with titles,
balanced parens, angle destinations, images, autolinks, code spans,
reflinks + defs, nested brackets, unterminated sequences) x gfm on/
off produce byte-identical output before and after the change.

Adds test/specs/redos/quadratic_unterminated_links.cjs following
the existing guard convention; it exceeds the redos budget on
master and passes with this change.

* refactor: one paren scan per inline run via lexer state

Per review feedback: drop the WeakMap anchor machinery in favor of a
single state flag. Lexer.inlineTokens scans for ')' once per run and
stores it in state.linkParenPossible (same pattern as linkEmitted),
restoring the previous value after nested runs. Tokenizer.link bails
out only when the flag is false, which is exactly when its regex
cannot match.

This keeps the fast-fail while avoiding a repeated full-text
includes() on reflink-heavy paragraphs, where attempting link() at
every token would otherwise re-scan the remainder each time
(quadratic_inline_masking[2]: ~1.03s vs ~144ms here, upstream ~140ms).

* refactor: skip paren scan for nested runs when outer had no ')'

When the outer inline run contains no ')' (linkParenPossible=false),
a nested run's substring can't contain one either, so skip the
redundant src.includes(')') scan. Addresses UziTech's observation
on line 354: 'This also only needs to be checked if prevLinkParenPossible
is true.'

* make inner function private

Co-authored-by: Steven <steven@ceriously.com>

* Change inlineTokensInner to private method call

---------

Co-authored-by: Tony Brix <tony@brix.ninja>
Co-authored-by: Steven <steven@ceriously.com>
2026-09-29 16:56:59 -06:00
2018-04-05 11:36:07 -04:00
2021-11-02 09:40:27 -05:00
2026-09-22 06:07:51 +00:00
2023-08-09 23:33:46 -06:00
2020-08-29 17:55:07 -04:00

Marked

npm install size downloads github actions snyk inspect.software

  • ⚡ built for speed
  • ⬇️ low-level compiler for parsing markdown without caching or blocking for long periods of time
  • ⚖️ light-weight while implementing all markdown features from the supported flavors & specifications
  • 🛠️ easily adaptable with custom extensions
  • 🌐 works in a browser, on a server, or from a command line interface (CLI)

Demo

Check out the demo page to see Marked in action ⛹️

Docs

Our documentation pages are also rendered using marked 💯

Also read about:

Compatibility

Node.js: Only current and LTS Node.js versions are supported. End of life Node.js versions may become incompatible with Marked at any point in time.

Browser: Baseline Widely Available

Installation

CLI:

npm install -g marked

In-browser:

npm install marked

Usage

DOMPurify.sanitize(marked.parse(`<img src="x" onerror="alert('not happening')">`));

CLI

# Example with stdin input
$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>
# Print all options
$ marked --help

Node.js

import { marked } from 'marked';
const html = marked.parse('# Marked in Node.js');
console.log(html);

Browser

<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
</head>
<body>
  <div id="content"></div>
  <script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.js"></script>
  <script>
    document.getElementById('content').innerHTML =
      marked.parse('# Marked in the browser\n\nRendered by **marked**.');
  </script>
</body>
</html>

or import esm module

<script type="module">
  import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
  document.getElementById('content').innerHTML =
    marked.parse('# Marked in the browser\n\nRendered by **marked**.');
</script>

License

Copyright (c) 2018+, MarkedJS. (MIT License) Copyright (c) 2011-2018, Christopher Jeffrey. (MIT License)

S
Description
Lustro github.com/markedjs/marked
Readme Multiple Licenses
22 MiB
Languages
TypeScript 37.9%
JavaScript 36.5%
HTML 25.6%