@opencode/ui 0.0.0-dev-20640 → 0.0.0-dev-20644

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,3 @@
1
+ import type { MarkedExtension } from "marked";
1
2
  export declare function createMarkdownParser(highlight: (code: string, language: string) => string | Promise<string>): import("marked").Marked<string, string>;
3
+ export declare const markdownMath: MarkedExtension[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-20640",
3
+ "version": "0.0.0-dev-20644",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -251,6 +251,7 @@
251
251
  "fuzzysort": "3.1.0",
252
252
  "katex": "0.16.47",
253
253
  "marked": "18.0.11",
254
+ "marked-katex-extension": "5.1.13",
254
255
  "marked-shiki": "1.2.1",
255
256
  "motion": "12.34.5",
256
257
  "remeda": "2.26.0",
@@ -16,7 +16,8 @@ let smallParser: Marked | undefined
16
16
 
17
17
  export function parseSmallMarkdown(text: string) {
18
18
  // Any possible KaTeX delimiter stays on the worker, including escaped ones.
19
- if (text.length > 1024 || text.includes("\\(") || text.includes("$$")) return
19
+ // Dollar math needs a pair, so a lone price such as `$5` can stay on this path.
20
+ if (text.length > 1024 || text.includes("\\(") || text.indexOf("$") !== text.lastIndexOf("$")) return
20
21
 
21
22
  // Ordinary prose does not need the block/inline lexer's cold regular expressions.
22
23
  // Keep every possible Markdown construct, autolink, and hard break on that lexer.
@@ -1,65 +1,45 @@
1
1
  import katex from "katex"
2
2
  import type { MarkedExtension, Tokens } from "marked"
3
+ import markedKatex from "marked-katex-extension"
3
4
  import markedShiki from "marked-shiki"
4
5
  import { createMarkdownBase } from "./marked-base"
5
6
 
6
7
  export function createMarkdownParser(highlight: (code: string, language: string) => string | Promise<string>) {
7
- return createMarkdownBase().use(katexExtension, markedShiki({ highlight }))
8
+ return createMarkdownBase().use(...markdownMath, markedShiki({ highlight }))
8
9
  }
9
10
 
10
- const inlineMathRegex = /^\\\(((?:\\.|[^\\\n])*?)\\\)/
11
-
12
- const blockMathRegex = /^\$\$\n([\s\S]+?)\n\$\$(?:\n|$)/
13
-
14
- const katexExtension: MarkedExtension = {
15
- extensions: [
16
- {
17
- name: "inlineKatex",
18
- level: "inline",
19
- start(src) {
20
- const index = src.indexOf("\\(")
21
-
22
- if (index === -1) return
23
-
24
- return index
25
- },
26
- tokenizer(src) {
27
- const match = src.match(inlineMathRegex)
28
-
29
- if (!match) return
30
-
31
- return {
32
- type: "inlineKatex",
33
- raw: match[0],
34
- text: match[1].trim(),
35
- displayMode: false,
36
- }
11
+ const inlineParenMathRegex = /^\\\(((?:\\.|[^\\\n])*?)\\\)/
12
+
13
+ // marked-katex-extension handles `$...$`, `$$...$$`, and `$$` fenced blocks; it has no `\(...\)` syntax.
14
+ export const markdownMath: MarkedExtension[] = [
15
+ markedKatex({ throwOnError: false }),
16
+ {
17
+ extensions: [
18
+ {
19
+ name: "inlineParenKatex",
20
+ level: "inline",
21
+ start(src) {
22
+ const index = src.indexOf("\\(")
23
+
24
+ if (index === -1) return
25
+
26
+ return index
27
+ },
28
+ tokenizer(src) {
29
+ const match = src.match(inlineParenMathRegex)
30
+
31
+ if (!match) return
32
+
33
+ return {
34
+ type: "inlineParenKatex",
35
+ raw: match[0],
36
+ text: match[1].trim(),
37
+ }
38
+ },
39
+ renderer(token: Tokens.Generic) {
40
+ return katex.renderToString(token.text, { throwOnError: false })
41
+ },
37
42
  },
38
- renderer: renderKatexToken,
39
- },
40
- {
41
- name: "blockKatex",
42
- level: "block",
43
- tokenizer(src) {
44
- const match = src.match(blockMathRegex)
45
-
46
- if (!match) return
47
-
48
- return {
49
- type: "blockKatex",
50
- raw: match[0],
51
- text: match[1].trim(),
52
- displayMode: true,
53
- }
54
- },
55
- renderer: renderKatexToken,
56
- },
57
- ],
58
- }
59
-
60
- function renderKatexToken(token: Tokens.Generic) {
61
- return katex.renderToString(typeof token.text === "string" ? token.text : "", {
62
- displayMode: token.displayMode === true,
63
- throwOnError: false,
64
- })
65
- }
43
+ ],
44
+ },
45
+ ]