Repository navigation
Add Bun.markdown.fromHTML(): native HTML to Markdown conversion #41527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sosukesuzuki
wants to merge
19
commits into
main
Choose a base branch
from
claude/markdown-from-html
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
45803ca
Add Bun.markdown.fromHTML(): convert HTML to Markdown natively
sosukesuzuki ed4958e
fromHTML: read options before taking the input buffer view
sosukesuzuki 78a1ca3
fromHTML: 2-3x faster with a byte-oriented tokenizer feeding html5eve…
sosukesuzuki 6e95d75
fromHTML: inline SIMD scans, one token per text run, linear-time guar…
sosukesuzuki d819ece
fromHTML: cut ~320 KB of static data; bound table/attribute output; e…
sosukesuzuki 2737e40
[autofix.ci] apply automated fixes
autofix-ci[bot] 9d90a4d
fromHTML: cap foreign-content nesting, snapshot shared input, review …
sosukesuzuki 4573326
fromHTML: name push_text_content's parameters after next_in_preorder'…
sosukesuzuki cc8d476
fromHTML: build the tree from HTMLRewriter's parser instead of html5ever
sosukesuzuki e0b9874
[autofix.ci] apply automated fixes
autofix-ci[bot] 4e7296b
fromHTML: bound formatting reopen work, fix <a>-in-<a> past a marker,…
sosukesuzuki dd5cb9f
fromHTML: name what insert() does with the element instead of passing…
sosukesuzuki cf20807
fromHTML: forget unreopenable formatting entries, keep links working …
sosukesuzuki 3b65b67
fromHTML: drive lol-html's tokenizer directly instead of going throug…
sosukesuzuki 436c73f
fromHTML: take text and attributes raw from the tokenizer; identify t…
sosukesuzuki 3ed8ce0
deps: lol-html at oven-sh/lol-html#3 head (pinned CI actions, warning…
sosukesuzuki c72517e
deps: lol-html at oven-sh/lol-html#3 head (same sources as d2efffb)
sosukesuzuki f666869
fromHTML: keep typed-arena in workspace deps after rebasing on main; …
sosukesuzuki 452da9a
fromHTML: return a named struct from scan::load_tail instead of a (u6…
sosukesuzuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // HTML → Markdown: Bun.markdown.fromHTML vs turndown (+ turndown-plugin-gfm). | ||
| // | ||
| // bun bench/snippets/html-to-markdown.mjs # synthetic pages | ||
| // bun bench/snippets/html-to-markdown.mjs page.html # your own saved pages | ||
| // | ||
| // turndown is what most JS apps use for this today; it builds a DOM with a | ||
| // pure-JS HTML parser (domino) and walks it with regex-heavy rules, so large | ||
| // pages cost hundreds of milliseconds of main-thread time and a lot of | ||
| // transient garbage. The synthetic input below is shaped like a docs/article | ||
| // page (nav, prose with inline markup, code blocks, tables, lists) and is | ||
| // repeated to reach the sizes real pages have. | ||
| import { readFileSync } from "node:fs"; | ||
| import { basename } from "node:path"; | ||
| import TurndownService from "turndown"; | ||
| import { gfm } from "turndown-plugin-gfm"; | ||
| import { bench, group, run } from "../runner.mjs"; | ||
|
|
||
| const td = new TurndownService({ headingStyle: "atx", codeBlockStyle: "fenced", bulletListMarker: "-", hr: "---" }); | ||
| td.use(gfm); | ||
| td.remove(["script", "style"]); | ||
|
|
||
| const section = i => ` | ||
| <section id="s${i}"> | ||
| <h2 class="heading"><a href="#s${i}">Section ${i}: configuring the <code>thing</code></a></h2> | ||
| <p> | ||
| Lorem ipsum <strong>dolor sit amet</strong>, consectetur <em>adipiscing</em> elit. Use | ||
| <a href="/docs/api/thing?id=${i}" title="API reference">the <code>thing()</code> API</a> to | ||
| frobnicate the widget — it’s <del>slow</del> fast now. See issue | ||
| <a href="https://example.com/issues/${1000 + i}">#${1000 + i}</a> for details, and note that | ||
| 2 * 3 = 6 and snake_case_names are left alone. | ||
| </p> | ||
| <div class="highlight highlight-source-ts"><pre><code class="language-ts">import { thing } from "pkg"; | ||
|
|
||
| export async function run${i}(input: string): Promise<number> { | ||
| const result = await thing(input, { retries: ${i % 5}, verbose: true }); | ||
| if (!result.ok) throw new Error(\`failed: \${result.error}\`); | ||
| return result.value * 2; | ||
| } | ||
| </code></pre></div> | ||
| <table class="params"> | ||
| <thead><tr><th>Option</th><th align="center">Type</th><th align="right">Default</th><th>Description</th></tr></thead> | ||
| <tbody> | ||
| <tr><td><code>retries</code></td><td><code>number</code></td><td>3</td><td>How many times to retry before giving up.</td></tr> | ||
| <tr><td><code>verbose</code></td><td><code>boolean</code></td><td><code>false</code></td><td>Log each attempt to <code>stderr</code>.</td></tr> | ||
| <tr><td><code>signal</code></td><td><code>AbortSignal</code></td><td>—</td><td>Cancels the <em>in-flight</em> attempt | and any pending retries.</td></tr> | ||
| </tbody> | ||
| </table> | ||
| <ul> | ||
| <li>First point with <b>bold</b> and a <a href="/x/${i}">link</a></li> | ||
| <li>Second point | ||
| <ol start="3"><li>nested three</li><li>nested four<ul><li>deeper</li></ul></li></ol> | ||
| </li> | ||
| <li><input type="checkbox" checked> done item</li> | ||
| <li><p>Loose item with a paragraph.</p><blockquote><p>And a quote<br>with a break.</p></blockquote></li> | ||
| </ul> | ||
| <p><img src="/img/diagram-${i}.png" alt="Diagram ${i}" width="600" height="400"> <span class="caption">Figure ${i}.</span></p> | ||
| <hr> | ||
| </section>`; | ||
|
|
||
| function page(sections) { | ||
| let nav = ""; | ||
| for (let i = 0; i < sections; i++) nav += `<li><a href="#s${i}">Section ${i}</a></li>`; | ||
| let body = ""; | ||
| for (let i = 0; i < sections; i++) body += section(i); | ||
| return `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Bench page</title> | ||
| <style>body{font-family:sans-serif}.heading{color:#333}</style> | ||
| <script>window.dataLayer=[];function gtag(){dataLayer.push(arguments)}</script></head> | ||
| <body><header><nav><ul>${nav}</ul></nav></header><main><article><h1>Benchmark <em>page</em></h1>${body}</article></main> | ||
| <footer><p>© 2025 Example · <a href="/privacy">Privacy</a></p></footer> | ||
| <script type="application/json">${JSON.stringify({ props: { sections } })}</script></body></html>`; | ||
| } | ||
|
|
||
| const inputs = []; | ||
| const files = process.argv.slice(2); | ||
| if (files.length) { | ||
| for (const f of files) inputs.push([basename(f), readFileSync(f, "utf8")]); | ||
| } else { | ||
| for (const n of [2, 40, 400]) { | ||
| const html = page(n); | ||
| inputs.push([`${(html.length / 1024).toFixed(0)} KB page`, html]); | ||
| } | ||
| } | ||
|
|
||
| const hasBun = typeof Bun !== "undefined" && Bun.markdown && typeof Bun.markdown.fromHTML === "function"; | ||
|
|
||
| for (const [name, html] of inputs) { | ||
| group(name, () => { | ||
| if (hasBun) { | ||
| bench("Bun.markdown.fromHTML", () => Bun.markdown.fromHTML(html)); | ||
| } | ||
| bench("turndown + gfm", () => td.turndown(html)); | ||
| }); | ||
| } | ||
|
|
||
| await run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Include
Bun.markdown.ansi()in the API overview.Line 10 presents the bullets as the Markdown API set.
src/runtime/api/MarkdownObject.rsalso registersBun.markdown.ansi(), but the overview omits it. Add that API or state that the list is partial.🤖 Prompt for AI Agents