@zyro-sec/latex 1.0.2 → 1.0.3

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.
package/src/limits.ts CHANGED
@@ -1,40 +1,40 @@
1
- import { LatexParseError } from "./types"
2
-
3
- export const DEFAULT_MAX_SOURCE_LENGTH = 100_000
4
- export const DEFAULT_MAX_NESTING_DEPTH = 256
5
-
6
- export function resolvePositiveInteger(value: number | undefined, fallback: number, optionName: string): number {
7
- if (value === undefined) return fallback
8
- if (!Number.isSafeInteger(value) || value < 1) {
9
- throw new RangeError(`${optionName} must be a positive safe integer`)
10
- }
11
- return value
12
- }
13
-
14
- export function assertSourceLength(source: string, maximum: number, label = "LaTeX source"): void {
15
- if (source.length > maximum) {
16
- throw new LatexParseError(`${label} exceeds the ${maximum}-character limit`, maximum)
17
- }
18
- }
19
-
20
- export function assertNestingDepth(source: string, maximum: number): void {
21
- let depth = 0
22
- let slashRun = 0
23
- for (let index = 0; index < source.length; index++) {
24
- const char = source[index]
25
- if (char === "\\") {
26
- slashRun++
27
- continue
28
- }
29
- const escaped = slashRun % 2 === 1
30
- slashRun = 0
31
- if (char === "{" && !escaped) {
32
- depth++
33
- if (depth > maximum) {
34
- throw new LatexParseError(`LaTeX nesting exceeds the ${maximum}-level limit`, index)
35
- }
36
- } else if (char === "}" && !escaped) {
37
- depth = Math.max(0, depth - 1)
38
- }
39
- }
40
- }
1
+ import { LatexParseError } from "./types"
2
+
3
+ export const DEFAULT_MAX_SOURCE_LENGTH = 100_000
4
+ export const DEFAULT_MAX_NESTING_DEPTH = 256
5
+
6
+ export function resolvePositiveInteger(value: number | undefined, fallback: number, optionName: string): number {
7
+ if (value === undefined) return fallback
8
+ if (!Number.isSafeInteger(value) || value < 1) {
9
+ throw new RangeError(`${optionName} must be a positive safe integer`)
10
+ }
11
+ return value
12
+ }
13
+
14
+ export function assertSourceLength(source: string, maximum: number, label = "LaTeX source"): void {
15
+ if (source.length > maximum) {
16
+ throw new LatexParseError(`${label} exceeds the ${maximum}-character limit`, maximum)
17
+ }
18
+ }
19
+
20
+ export function assertNestingDepth(source: string, maximum: number): void {
21
+ let depth = 0
22
+ let slashRun = 0
23
+ for (let index = 0; index < source.length; index++) {
24
+ const char = source[index]
25
+ if (char === "\\") {
26
+ slashRun++
27
+ continue
28
+ }
29
+ const escaped = slashRun % 2 === 1
30
+ slashRun = 0
31
+ if (char === "{" && !escaped) {
32
+ depth++
33
+ if (depth > maximum) {
34
+ throw new LatexParseError(`LaTeX nesting exceeds the ${maximum}-level limit`, index)
35
+ }
36
+ } else if (char === "}" && !escaped) {
37
+ depth = Math.max(0, depth - 1)
38
+ }
39
+ }
40
+ }
package/src/markdown.ts CHANGED
@@ -1,116 +1,116 @@
1
- import {
2
- CodeRenderable,
3
- RenderableEvents,
4
- ScrollBoxRenderable,
5
- StyledText,
6
- TextRenderable,
7
- createTextAttributes,
8
- parseColor,
9
- type ColorInput,
10
- type MarkdownCodeBlockRenderer,
11
- type RenderContext,
12
- } from "@opentui/core"
13
- import stringWidth from "string-width"
14
- import { renderLatex } from "./render"
15
- import { LatexParseError, type MathLayout } from "./types"
16
-
17
- export type LatexOptions = {
18
- text: ColorInput
19
- subdued: ColorInput
20
- }
21
-
22
- type LatexFrame = {
23
- source: string
24
- layout: MathLayout
25
- }
26
-
27
- export function createLatexCodeBlockRenderer(
28
- context: RenderContext,
29
- options: () => LatexOptions,
30
- ): MarkdownCodeBlockRenderer {
31
- const lastGood = new Map<string, LatexFrame>()
32
- return (block, render) => {
33
- const fallback = render.defaultRender()
34
- const key = fallback?.id
35
- const previous = key ? lastGood.get(key) : undefined
36
- const retained = previous && block.text.startsWith(previous.source) ? previous : undefined
37
- // A fence still streaming in keeps the last formula that parsed.
38
- const streaming = fallback instanceof CodeRenderable && fallback.streaming && !block.closed
39
- const layout = layoutLatex(block.text)
40
- const frame: LatexFrame | undefined = layout
41
- ? { source: block.text, layout }
42
- : streaming && retained
43
- ? { ...retained }
44
- : undefined
45
- if (!frame) return fallback ?? undefined
46
- const palette = options()
47
- const text = parseColor(palette.text)
48
- const subdued = parseColor(palette.subdued)
49
- const formula = new TextRenderable(context, {
50
- content: new StyledText(
51
- frame.layout.cells.flatMap((row, index) => [
52
- ...Array.from(row).flatMap((cell, column) => {
53
- // Wide glyphs already occupy the following cell; do not emit another space for it.
54
- if (column > 0 && stringWidth(row[column - 1]?.char ?? "") > 1) return []
55
- return [
56
- {
57
- __isChunk: true as const,
58
- text: cell?.char ?? " ",
59
- fg: /^[()[\]{}|\u221a\u239b-\u23ad\u2500-\u257f]$/u.test(cell?.char ?? "") ? subdued : text,
60
- attributes: createTextAttributes({
61
- bold: cell?.style?.bold || /^[=<>\u2260\u2261\u2264\u2265\u2248]$/u.test(cell?.char ?? ""),
62
- italic: cell?.style?.italic,
63
- dim: cell?.style?.dim,
64
- }),
65
- },
66
- ]
67
- }),
68
- ...(index < frame.layout.height - 1 ? [{ __isChunk: true as const, text: "\n", fg: text }] : []),
69
- ]),
70
- ),
71
- width: "100%",
72
- minWidth: frame.layout.width,
73
- height: frame.layout.height,
74
- wrapMode: "none",
75
- selectable: false,
76
- flexShrink: 0,
77
- })
78
- const viewport = new ScrollBoxRenderable(context, {
79
- width: "100%",
80
- height: frame.layout.height,
81
- flexShrink: 0,
82
- marginTop: 1,
83
- scrollX: true,
84
- scrollY: false,
85
- onMouseScroll(event) {
86
- if (event.modifiers.shift || event.scroll?.direction === "left" || event.scroll?.direction === "right") {
87
- event.stopPropagation()
88
- }
89
- },
90
- })
91
- // The setters opt out of automatic scrollbar visibility; constructor options do not.
92
- viewport.horizontalScrollBar.visible = false
93
- viewport.verticalScrollBar.visible = false
94
- viewport.add(formula)
95
- if (key) {
96
- lastGood.set(key, frame)
97
- viewport.once(RenderableEvents.DESTROYED, () => {
98
- // Markdown destroys the old block before constructing its replacement in the same stack.
99
- queueMicrotask(() => {
100
- if (lastGood.get(key) === frame) lastGood.delete(key)
101
- })
102
- })
103
- }
104
- return viewport
105
- }
106
- }
107
-
108
- function layoutLatex(source: string) {
109
- try {
110
- return renderLatex(source, { strict: true, displayMode: true })
111
- } catch (error) {
112
- // Preserve the exact source for incomplete math, unsupported commands, and oversized input.
113
- if (error instanceof LatexParseError || error instanceof RangeError) return undefined
114
- throw error
115
- }
116
- }
1
+ import {
2
+ CodeRenderable,
3
+ RenderableEvents,
4
+ ScrollBoxRenderable,
5
+ StyledText,
6
+ TextRenderable,
7
+ createTextAttributes,
8
+ parseColor,
9
+ type ColorInput,
10
+ type MarkdownCodeBlockRenderer,
11
+ type RenderContext,
12
+ } from "@opentui/core"
13
+ import stringWidth from "string-width"
14
+ import { renderLatex } from "./render"
15
+ import { LatexParseError, type MathLayout } from "./types"
16
+
17
+ export type LatexOptions = {
18
+ text: ColorInput
19
+ subdued: ColorInput
20
+ }
21
+
22
+ type LatexFrame = {
23
+ source: string
24
+ layout: MathLayout
25
+ }
26
+
27
+ export function createLatexCodeBlockRenderer(
28
+ context: RenderContext,
29
+ options: () => LatexOptions,
30
+ ): MarkdownCodeBlockRenderer {
31
+ const lastGood = new Map<string, LatexFrame>()
32
+ return (block, render) => {
33
+ const fallback = render.defaultRender()
34
+ const key = fallback?.id
35
+ const previous = key ? lastGood.get(key) : undefined
36
+ const retained = previous && block.text.startsWith(previous.source) ? previous : undefined
37
+ // A fence still streaming in keeps the last formula that parsed.
38
+ const streaming = fallback instanceof CodeRenderable && fallback.streaming && !block.closed
39
+ const layout = layoutLatex(block.text)
40
+ const frame: LatexFrame | undefined = layout
41
+ ? { source: block.text, layout }
42
+ : streaming && retained
43
+ ? { ...retained }
44
+ : undefined
45
+ if (!frame) return fallback ?? undefined
46
+ const palette = options()
47
+ const text = parseColor(palette.text)
48
+ const subdued = parseColor(palette.subdued)
49
+ const formula = new TextRenderable(context, {
50
+ content: new StyledText(
51
+ frame.layout.cells.flatMap((row, index) => [
52
+ ...Array.from(row).flatMap((cell, column) => {
53
+ // Wide glyphs already occupy the following cell; do not emit another space for it.
54
+ if (column > 0 && stringWidth(row[column - 1]?.char ?? "") > 1) return []
55
+ return [
56
+ {
57
+ __isChunk: true as const,
58
+ text: cell?.char ?? " ",
59
+ fg: /^[()[\]{}|\u221a\u239b-\u23ad\u2500-\u257f]$/u.test(cell?.char ?? "") ? subdued : text,
60
+ attributes: createTextAttributes({
61
+ bold: cell?.style?.bold || /^[=<>\u2260\u2261\u2264\u2265\u2248]$/u.test(cell?.char ?? ""),
62
+ italic: cell?.style?.italic,
63
+ dim: cell?.style?.dim,
64
+ }),
65
+ },
66
+ ]
67
+ }),
68
+ ...(index < frame.layout.height - 1 ? [{ __isChunk: true as const, text: "\n", fg: text }] : []),
69
+ ]),
70
+ ),
71
+ width: "100%",
72
+ minWidth: frame.layout.width,
73
+ height: frame.layout.height,
74
+ wrapMode: "none",
75
+ selectable: false,
76
+ flexShrink: 0,
77
+ })
78
+ const viewport = new ScrollBoxRenderable(context, {
79
+ width: "100%",
80
+ height: frame.layout.height,
81
+ flexShrink: 0,
82
+ marginTop: 1,
83
+ scrollX: true,
84
+ scrollY: false,
85
+ onMouseScroll(event) {
86
+ if (event.modifiers.shift || event.scroll?.direction === "left" || event.scroll?.direction === "right") {
87
+ event.stopPropagation()
88
+ }
89
+ },
90
+ })
91
+ // The setters opt out of automatic scrollbar visibility; constructor options do not.
92
+ viewport.horizontalScrollBar.visible = false
93
+ viewport.verticalScrollBar.visible = false
94
+ viewport.add(formula)
95
+ if (key) {
96
+ lastGood.set(key, frame)
97
+ viewport.once(RenderableEvents.DESTROYED, () => {
98
+ // Markdown destroys the old block before constructing its replacement in the same stack.
99
+ queueMicrotask(() => {
100
+ if (lastGood.get(key) === frame) lastGood.delete(key)
101
+ })
102
+ })
103
+ }
104
+ return viewport
105
+ }
106
+ }
107
+
108
+ function layoutLatex(source: string) {
109
+ try {
110
+ return renderLatex(source, { strict: true, displayMode: true })
111
+ } catch (error) {
112
+ // Preserve the exact source for incomplete math, unsupported commands, and oversized input.
113
+ if (error instanceof LatexParseError || error instanceof RangeError) return undefined
114
+ throw error
115
+ }
116
+ }