Skip to content

Commit 9a51765

Browse files
authored
fix(ui): preserve code spans adjacent to tildes (anomalyco#35835)
1 parent bb31c9b commit 9a51765

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

bun.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"hono-openapi": "1.1.2",
7171
"fuzzysort": "3.1.0",
7272
"luxon": "3.6.1",
73-
"marked": "17.0.1",
73+
"marked": "17.0.6",
7474
"marked-shiki": "1.2.1",
7575
"remend": "1.3.0",
7676
"@playwright/test": "1.59.1",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from "bun:test"
2+
import { Marked } from "marked"
3+
import { markedCodeSpanBoundary } from "./marked-code-span"
4+
5+
test("preserves code spans adjacent to tildes", async () => {
6+
const marked = new Marked(markedCodeSpanBoundary)
7+
8+
expect(await marked.parse("~`0.1576` to measurement-window-only `0.00092`")).toBe(
9+
"<p>~<code>0.1576</code> to measurement-window-only <code>0.00092</code></p>\n",
10+
)
11+
expect(await marked.parse("`before`~`after`")).toBe(
12+
"<p><code>before</code>~<code>after</code></p>\n",
13+
)
14+
expect(await marked.parse("~~`deleted code`~~")).toBe("<p><del><code>deleted code</code></del></p>\n")
15+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { MarkedExtension } from "marked"
2+
3+
// Keep adjacent tilde and backtick runs separate until markedjs/marked#4011 is released.
4+
export const markedCodeSpanBoundary = {
5+
tokenizer: {
6+
inlineText(src) {
7+
const match = /^(`+(?=~)|~+(?=`))/.exec(src)
8+
if (!match) return false
9+
return {
10+
type: "text",
11+
raw: match[0],
12+
text: match[0],
13+
escaped: this.lexer.state.inRawBlock,
14+
}
15+
},
16+
},
17+
} satisfies MarkedExtension

packages/ui/src/context/marked.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import markedShiki from "marked-shiki"
33
import katex from "katex"
44
import { bundledLanguages, type BundledLanguage } from "shiki"
55
import { createSimpleContext } from "./helper"
6+
import { markedCodeSpanBoundary } from "./marked-code-span"
67
import { getSharedHighlighter, registerCustomTheme, ThemeRegistrationResolved } from "@pierre/diffs"
78

89
export const OpenCodeTheme = {
@@ -521,6 +522,7 @@ export const { use: useMarked, provider: MarkedProvider } = createSimpleContext(
521522
name: "Marked",
522523
init: (props: { nativeParser?: NativeMarkdownParser }) => {
523524
const jsParser = marked.use(
525+
markedCodeSpanBoundary,
524526
{
525527
renderer: {
526528
link({ href, title, text }) {

0 commit comments

Comments
 (0)