@earendil-works/pi-tui 0.85.0 → 0.86.0

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.
Files changed (64) hide show
  1. package/dist/autocomplete.d.ts.map +1 -1
  2. package/dist/autocomplete.js +18 -11
  3. package/dist/autocomplete.js.map +1 -1
  4. package/dist/components/editor.d.ts.map +1 -1
  5. package/dist/components/editor.js +7 -6
  6. package/dist/components/editor.js.map +1 -1
  7. package/dist/components/select-list.d.ts.map +1 -1
  8. package/dist/components/select-list.js +6 -11
  9. package/dist/components/select-list.js.map +1 -1
  10. package/dist/components/settings-list.d.ts.map +1 -1
  11. package/dist/components/settings-list.js +5 -10
  12. package/dist/components/settings-list.js.map +1 -1
  13. package/dist/fuzzy.d.ts.map +1 -1
  14. package/dist/fuzzy.js +23 -22
  15. package/dist/fuzzy.js.map +1 -1
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +2 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/latex.d.ts.map +1 -1
  21. package/dist/latex.js +109 -16
  22. package/dist/latex.js.map +1 -1
  23. package/dist/native-modifiers.d.ts +2 -1
  24. package/dist/native-modifiers.d.ts.map +1 -1
  25. package/dist/native-modifiers.js +3 -44
  26. package/dist/native-modifiers.js.map +1 -1
  27. package/dist/native-platform.d.ts +18 -0
  28. package/dist/native-platform.d.ts.map +1 -0
  29. package/dist/native-platform.js +42 -0
  30. package/dist/native-platform.js.map +1 -0
  31. package/dist/terminal.d.ts.map +1 -1
  32. package/dist/terminal.js +2 -20
  33. package/dist/terminal.js.map +1 -1
  34. package/dist/tui-alt-screen.d.ts +5 -3
  35. package/dist/tui-alt-screen.d.ts.map +1 -1
  36. package/dist/tui-alt-screen.js +26 -5
  37. package/dist/tui-alt-screen.js.map +1 -1
  38. package/dist/utils.d.ts +3 -0
  39. package/dist/utils.d.ts.map +1 -1
  40. package/dist/utils.js +4 -0
  41. package/dist/utils.js.map +1 -1
  42. package/native/clipboard.h +170 -0
  43. package/native/darwin/README.md +8 -6
  44. package/native/darwin/build.sh +12 -5
  45. package/native/darwin/prebuilds/darwin-arm64/{darwin-modifiers.node → darwin-platform.node} +0 -0
  46. package/native/darwin/prebuilds/darwin-x64/{darwin-modifiers.node → darwin-platform.node} +0 -0
  47. package/native/darwin/src/darwin-platform.m +93 -0
  48. package/native/linux/README.md +23 -0
  49. package/native/linux/build.sh +65 -0
  50. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  51. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  52. package/native/linux/src/clipboard-worker.h +70 -0
  53. package/native/linux/src/linux-platform-x11.c +274 -0
  54. package/native/napi.h +89 -0
  55. package/native/win32/README.md +16 -7
  56. package/native/win32/build.mjs +7 -4
  57. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  58. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  59. package/native/win32/src/win32-platform.c +241 -0
  60. package/package.json +11 -4
  61. package/native/darwin/src/darwin-modifiers.c +0 -70
  62. package/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
  63. package/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
  64. package/native/win32/src/win32-console-mode.c +0 -119
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAElC,OAAO,EAAE,MAAM,EAA2B,MAAM,QAAQ,CAAC;AACzD,uBAAuB;AACvB,OAAO,EAIN,4BAA4B,GAE5B,MAAM,mBAAmB,CAAC;AAC3B,aAAa;AACb,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,MAAM,EAAwC,MAAM,wBAAwB,CAAC;AACtF,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAsC,MAAM,uBAAuB,CAAC;AAClF,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAA+B,MAAM,wBAAwB,CAAC;AAC7E,OAAO,EAAyB,QAAQ,EAA4C,MAAM,0BAA0B,CAAC;AACrH,OAAO,EAAE,WAAW,EAA2B,MAAM,8BAA8B,CAAC;AACpF,OAAO,EACN,UAAU,GAIV,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAEN,UAAU,GAIV,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAoB,YAAY,EAA0B,MAAM,+BAA+B,CAAC;AACvG,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAKN,MAAM,GACN,MAAM,yBAAyB,CAAC;AAGjC,iBAAiB;AACjB,OAAO,EAAmB,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACtE,cAAc;AACd,OAAO,EACN,cAAc,EAOd,kBAAkB,EAClB,cAAc,EACd,eAAe,GACf,MAAM,kBAAkB,CAAC;AAC1B,0BAA0B;AAC1B,OAAO,EACN,oBAAoB,EACpB,YAAY,EACZ,WAAW,EACX,qBAAqB,EACrB,GAAG,EAGH,UAAU,EACV,QAAQ,EACR,sBAAsB,GACtB,MAAM,WAAW,CAAC;AACnB,kBAAkB;AAClB,OAAO,EAA2B,WAAW,EAAE,MAAM,YAAY,CAAC;AAClE,sCAAsC;AACtC,OAAO,EAAE,WAAW,EAAqD,MAAM,mBAAmB,CAAC;AACnG,yCAAyC;AACzC,OAAO,EAAE,eAAe,EAAiB,MAAM,eAAe,CAAC;AAC/D,kBAAkB;AAClB,OAAO,EACN,yBAAyB,EACzB,8BAA8B,GAG9B,MAAM,sBAAsB,CAAC;AAC9B,yBAAyB;AACzB,OAAO,EACN,eAAe,EAEf,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EAIT,aAAa,EACb,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,sBAAsB,EACtB,iBAAiB,GAEjB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEN,SAAS,EACT,aAAa,EACb,gBAAgB,EAEhB,WAAW,EACX,aAAa,GAkBb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,YAAY,EAA4B,MAAM,qBAAqB,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAiC,MAAM,sBAAsB,CAAC;AACpF,YAAY;AACZ,OAAO,EACN,mBAAmB,EACnB,aAAa,EACb,sBAAsB,EACtB,eAAe,EACf,YAAY,EACZ,gBAAgB,GAChB,MAAM,YAAY,CAAC","sourcesContent":["// Core TUI interfaces and classes\n\nexport { Marked, type Token, type Tokens } from \"marked\";\n// Autocomplete support\nexport {\n\ttype AutocompleteItem,\n\ttype AutocompleteProvider,\n\ttype AutocompleteSuggestions,\n\tCombinedAutocompleteProvider,\n\ttype SlashCommand,\n} from \"./autocomplete.ts\";\n// Components\nexport { Box } from \"./components/box.ts\";\nexport { CancellableLoader } from \"./components/cancellable-loader.ts\";\nexport { Editor, type EditorOptions, type EditorTheme } from \"./components/editor.ts\";\nexport { HStack } from \"./components/h-stack.ts\";\nexport { Image, type ImageOptions, type ImageTheme } from \"./components/image.ts\";\nexport { Input } from \"./components/input.ts\";\nexport { Loader, type LoaderIndicatorOptions } from \"./components/loader.ts\";\nexport { type DefaultTextStyle, Markdown, type MarkdownOptions, type MarkdownTheme } from \"./components/markdown.ts\";\nexport { MouseRegion, type MouseRegionHandler } from \"./components/mouse-region.ts\";\nexport {\n\tScrollView,\n\ttype ScrollViewOptions,\n\ttype ScrollViewScrollbar,\n\ttype ScrollViewScrollToOptions,\n} from \"./components/scroll-view.ts\";\nexport {\n\ttype SelectItem,\n\tSelectList,\n\ttype SelectListLayoutOptions,\n\ttype SelectListTheme,\n\ttype SelectListTruncatePrimaryContext,\n} from \"./components/select-list.ts\";\nexport { type SettingItem, SettingsList, type SettingsListTheme } from \"./components/settings-list.ts\";\nexport { Spacer } from \"./components/spacer.ts\";\nexport { Text } from \"./components/text.ts\";\nexport { TruncatedText } from \"./components/truncated-text.ts\";\nexport {\n\ttype StackChild,\n\ttype StackEntry,\n\ttype StackEntryOptions,\n\ttype StackOptions,\n\tVStack,\n} from \"./components/v-stack.ts\";\n// Editor component interface (for custom editors)\nexport type { EditorComponent } from \"./editor-component.ts\";\n// Fuzzy matching\nexport { type FuzzyMatch, fuzzyFilter, fuzzyMatch } from \"./fuzzy.ts\";\n// Keybindings\nexport {\n\tgetKeybindings,\n\ttype Keybinding,\n\ttype KeybindingConflict,\n\ttype KeybindingDefinition,\n\ttype KeybindingDefinitions,\n\ttype Keybindings,\n\ttype KeybindingsConfig,\n\tKeybindingsManager,\n\tsetKeybindings,\n\tTUI_KEYBINDINGS,\n} from \"./keybindings.ts\";\n// Keyboard input handling\nexport {\n\tdecodeKittyPrintable,\n\tisKeyRelease,\n\tisKeyRepeat,\n\tisKittyProtocolActive,\n\tKey,\n\ttype KeyEventType,\n\ttype KeyId,\n\tmatchesKey,\n\tparseKey,\n\tsetKittyProtocolActive,\n} from \"./keys.ts\";\n// LaTeX rendering\nexport { type RenderLatexOptions, renderLatex } from \"./latex.ts\";\n// Input buffering for batch splitting\nexport { StdinBuffer, type StdinBufferEventMap, type StdinBufferOptions } from \"./stdin-buffer.ts\";\n// Terminal interface and implementations\nexport { ProcessTerminal, type Terminal } from \"./terminal.ts\";\n// Terminal colors\nexport {\n\tparseOsc11BackgroundColor,\n\tparseTerminalColorSchemeReport,\n\ttype RgbColor,\n\ttype TerminalColorScheme,\n} from \"./terminal-colors.ts\";\n// Terminal image support\nexport {\n\tallocateImageId,\n\ttype CellDimensions,\n\tcalculateImageRows,\n\tdeleteAllKittyImages,\n\tdeleteKittyImage,\n\tdetectCapabilities,\n\tencodeITerm2,\n\tencodeKitty,\n\tgetCapabilities,\n\tgetCellDimensions,\n\tgetGifDimensions,\n\tgetImageDimensions,\n\tgetJpegDimensions,\n\tgetPngDimensions,\n\tgetWebpDimensions,\n\thyperlink,\n\ttype ImageDimensions,\n\ttype ImageProtocol,\n\ttype ImageRenderOptions,\n\timageFallback,\n\trenderImage,\n\tresetCapabilitiesCache,\n\tsetCapabilities,\n\tsetCapabilityOverrides,\n\tsetCellDimensions,\n\ttype TerminalCapabilities,\n} from \"./terminal-image.ts\";\nexport {\n\ttype Component,\n\tContainer,\n\tCURSOR_MARKER,\n\tcompositeTuiLine,\n\ttype Focusable,\n\tisFocusable,\n\tisViewportTUI,\n\ttype OverlayAnchor,\n\ttype OverlayBounds,\n\ttype OverlayHandle,\n\ttype OverlayMargin,\n\ttype OverlayOptions,\n\ttype OverlayUnfocusOptions,\n\ttype SizeValue,\n\ttype TUI,\n\ttype TuiInputListener,\n\ttype TuiInputListenerResult,\n\ttype TuiMode,\n\ttype TuiMouseButton,\n\ttype TuiMouseEvent,\n\ttype TuiMouseEventResult,\n\ttype TuiMouseEventType,\n\ttype TuiStopOptions,\n\ttype ViewportTUI,\n} from \"./tui.ts\";\nexport { TuiAltScreen, type TuiAltScreenOptions } from \"./tui-alt-screen.ts\";\nexport { TuiMainScreen, type TuiMainScreenRenderState } from \"./tui-main-screen.ts\";\n// Utilities\nexport {\n\tgetOsc8LinkAtColumn,\n\tsliceByColumn,\n\tstripTerminalSequences,\n\ttruncateToWidth,\n\tvisibleWidth,\n\twrapTextWithAnsi,\n} from \"./utils.ts\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAElC,OAAO,EAAE,MAAM,EAA2B,MAAM,QAAQ,CAAC;AACzD,uBAAuB;AACvB,OAAO,EAIN,4BAA4B,GAE5B,MAAM,mBAAmB,CAAC;AAC3B,aAAa;AACb,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,MAAM,EAAwC,MAAM,wBAAwB,CAAC;AACtF,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAsC,MAAM,uBAAuB,CAAC;AAClF,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAA+B,MAAM,wBAAwB,CAAC;AAC7E,OAAO,EAAyB,QAAQ,EAA4C,MAAM,0BAA0B,CAAC;AACrH,OAAO,EAAE,WAAW,EAA2B,MAAM,8BAA8B,CAAC;AACpF,OAAO,EACN,UAAU,GAIV,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAEN,UAAU,GAIV,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAoB,YAAY,EAA0B,MAAM,+BAA+B,CAAC;AACvG,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAKN,MAAM,GACN,MAAM,yBAAyB,CAAC;AAGjC,iBAAiB;AACjB,OAAO,EAAmB,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACtE,cAAc;AACd,OAAO,EACN,cAAc,EAOd,kBAAkB,EAClB,cAAc,EACd,eAAe,GACf,MAAM,kBAAkB,CAAC;AAC1B,0BAA0B;AAC1B,OAAO,EACN,oBAAoB,EACpB,YAAY,EACZ,WAAW,EACX,qBAAqB,EACrB,GAAG,EAGH,UAAU,EACV,QAAQ,EACR,sBAAsB,GACtB,MAAM,WAAW,CAAC;AACnB,kBAAkB;AAClB,OAAO,EAA2B,WAAW,EAAE,MAAM,YAAY,CAAC;AAClE,8BAA8B;AAC9B,OAAO,EAAE,kBAAkB,EAAwB,MAAM,sBAAsB,CAAC;AAChF,sCAAsC;AACtC,OAAO,EAAE,WAAW,EAAqD,MAAM,mBAAmB,CAAC;AACnG,yCAAyC;AACzC,OAAO,EAAE,eAAe,EAAiB,MAAM,eAAe,CAAC;AAC/D,kBAAkB;AAClB,OAAO,EACN,yBAAyB,EACzB,8BAA8B,GAG9B,MAAM,sBAAsB,CAAC;AAC9B,yBAAyB;AACzB,OAAO,EACN,eAAe,EAEf,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EAIT,aAAa,EACb,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,sBAAsB,EACtB,iBAAiB,GAEjB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEN,SAAS,EACT,aAAa,EACb,gBAAgB,EAEhB,WAAW,EACX,aAAa,GAkBb,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,YAAY,EAA4B,MAAM,qBAAqB,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAiC,MAAM,sBAAsB,CAAC;AACpF,YAAY;AACZ,OAAO,EACN,mBAAmB,EACnB,aAAa,EACb,sBAAsB,EACtB,eAAe,EACf,YAAY,EACZ,gBAAgB,GAChB,MAAM,YAAY,CAAC","sourcesContent":["// Core TUI interfaces and classes\n\nexport { Marked, type Token, type Tokens } from \"marked\";\n// Autocomplete support\nexport {\n\ttype AutocompleteItem,\n\ttype AutocompleteProvider,\n\ttype AutocompleteSuggestions,\n\tCombinedAutocompleteProvider,\n\ttype SlashCommand,\n} from \"./autocomplete.ts\";\n// Components\nexport { Box } from \"./components/box.ts\";\nexport { CancellableLoader } from \"./components/cancellable-loader.ts\";\nexport { Editor, type EditorOptions, type EditorTheme } from \"./components/editor.ts\";\nexport { HStack } from \"./components/h-stack.ts\";\nexport { Image, type ImageOptions, type ImageTheme } from \"./components/image.ts\";\nexport { Input } from \"./components/input.ts\";\nexport { Loader, type LoaderIndicatorOptions } from \"./components/loader.ts\";\nexport { type DefaultTextStyle, Markdown, type MarkdownOptions, type MarkdownTheme } from \"./components/markdown.ts\";\nexport { MouseRegion, type MouseRegionHandler } from \"./components/mouse-region.ts\";\nexport {\n\tScrollView,\n\ttype ScrollViewOptions,\n\ttype ScrollViewScrollbar,\n\ttype ScrollViewScrollToOptions,\n} from \"./components/scroll-view.ts\";\nexport {\n\ttype SelectItem,\n\tSelectList,\n\ttype SelectListLayoutOptions,\n\ttype SelectListTheme,\n\ttype SelectListTruncatePrimaryContext,\n} from \"./components/select-list.ts\";\nexport { type SettingItem, SettingsList, type SettingsListTheme } from \"./components/settings-list.ts\";\nexport { Spacer } from \"./components/spacer.ts\";\nexport { Text } from \"./components/text.ts\";\nexport { TruncatedText } from \"./components/truncated-text.ts\";\nexport {\n\ttype StackChild,\n\ttype StackEntry,\n\ttype StackEntryOptions,\n\ttype StackOptions,\n\tVStack,\n} from \"./components/v-stack.ts\";\n// Editor component interface (for custom editors)\nexport type { EditorComponent } from \"./editor-component.ts\";\n// Fuzzy matching\nexport { type FuzzyMatch, fuzzyFilter, fuzzyMatch } from \"./fuzzy.ts\";\n// Keybindings\nexport {\n\tgetKeybindings,\n\ttype Keybinding,\n\ttype KeybindingConflict,\n\ttype KeybindingDefinition,\n\ttype KeybindingDefinitions,\n\ttype Keybindings,\n\ttype KeybindingsConfig,\n\tKeybindingsManager,\n\tsetKeybindings,\n\tTUI_KEYBINDINGS,\n} from \"./keybindings.ts\";\n// Keyboard input handling\nexport {\n\tdecodeKittyPrintable,\n\tisKeyRelease,\n\tisKeyRepeat,\n\tisKittyProtocolActive,\n\tKey,\n\ttype KeyEventType,\n\ttype KeyId,\n\tmatchesKey,\n\tparseKey,\n\tsetKittyProtocolActive,\n} from \"./keys.ts\";\n// LaTeX rendering\nexport { type RenderLatexOptions, renderLatex } from \"./latex.ts\";\n// Native platform integration\nexport { getNativeClipboard, type NativeClipboard } from \"./native-platform.ts\";\n// Input buffering for batch splitting\nexport { StdinBuffer, type StdinBufferEventMap, type StdinBufferOptions } from \"./stdin-buffer.ts\";\n// Terminal interface and implementations\nexport { ProcessTerminal, type Terminal } from \"./terminal.ts\";\n// Terminal colors\nexport {\n\tparseOsc11BackgroundColor,\n\tparseTerminalColorSchemeReport,\n\ttype RgbColor,\n\ttype TerminalColorScheme,\n} from \"./terminal-colors.ts\";\n// Terminal image support\nexport {\n\tallocateImageId,\n\ttype CellDimensions,\n\tcalculateImageRows,\n\tdeleteAllKittyImages,\n\tdeleteKittyImage,\n\tdetectCapabilities,\n\tencodeITerm2,\n\tencodeKitty,\n\tgetCapabilities,\n\tgetCellDimensions,\n\tgetGifDimensions,\n\tgetImageDimensions,\n\tgetJpegDimensions,\n\tgetPngDimensions,\n\tgetWebpDimensions,\n\thyperlink,\n\ttype ImageDimensions,\n\ttype ImageProtocol,\n\ttype ImageRenderOptions,\n\timageFallback,\n\trenderImage,\n\tresetCapabilitiesCache,\n\tsetCapabilities,\n\tsetCapabilityOverrides,\n\tsetCellDimensions,\n\ttype TerminalCapabilities,\n} from \"./terminal-image.ts\";\nexport {\n\ttype Component,\n\tContainer,\n\tCURSOR_MARKER,\n\tcompositeTuiLine,\n\ttype Focusable,\n\tisFocusable,\n\tisViewportTUI,\n\ttype OverlayAnchor,\n\ttype OverlayBounds,\n\ttype OverlayHandle,\n\ttype OverlayMargin,\n\ttype OverlayOptions,\n\ttype OverlayUnfocusOptions,\n\ttype SizeValue,\n\ttype TUI,\n\ttype TuiInputListener,\n\ttype TuiInputListenerResult,\n\ttype TuiMode,\n\ttype TuiMouseButton,\n\ttype TuiMouseEvent,\n\ttype TuiMouseEventResult,\n\ttype TuiMouseEventType,\n\ttype TuiStopOptions,\n\ttype ViewportTUI,\n} from \"./tui.ts\";\nexport { TuiAltScreen, type TuiAltScreenOptions } from \"./tui-alt-screen.ts\";\nexport { TuiMainScreen, type TuiMainScreenRenderState } from \"./tui-main-screen.ts\";\n// Utilities\nexport {\n\tgetOsc8LinkAtColumn,\n\tsliceByColumn,\n\tstripTerminalSequences,\n\ttruncateToWidth,\n\tvisibleWidth,\n\twrapTextWithAnsi,\n} from \"./utils.ts\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"latex.d.ts","sourceRoot":"","sources":["../src/latex.ts"],"names":[],"mappings":"AAs1CA,MAAM,WAAW,kBAAkB;IAClC,wFAAwF;IACxF,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,GAAG,SAAS,CAkBhG","sourcesContent":["import { visibleWidth } from \"./utils.ts\";\n\nconst SYMBOLS: Readonly<Record<string, string>> = {\n\talpha: \"α\",\n\tbeta: \"β\",\n\tgamma: \"γ\",\n\tdelta: \"δ\",\n\tepsilon: \"ϵ\",\n\tvarepsilon: \"ε\",\n\tzeta: \"ζ\",\n\teta: \"η\",\n\ttheta: \"θ\",\n\tvartheta: \"ϑ\",\n\tiota: \"ι\",\n\tkappa: \"κ\",\n\tvarkappa: \"ϰ\",\n\tlambda: \"λ\",\n\tmu: \"μ\",\n\tnu: \"ν\",\n\txi: \"ξ\",\n\tpi: \"π\",\n\tvarpi: \"ϖ\",\n\trho: \"ρ\",\n\tvarrho: \"ϱ\",\n\tsigma: \"σ\",\n\tvarsigma: \"ς\",\n\ttau: \"τ\",\n\tupsilon: \"υ\",\n\tphi: \"ϕ\",\n\tvarphi: \"φ\",\n\tchi: \"χ\",\n\tpsi: \"ψ\",\n\tomega: \"ω\",\n\tGamma: \"Γ\",\n\tDelta: \"Δ\",\n\tTheta: \"Θ\",\n\tLambda: \"Λ\",\n\tXi: \"Ξ\",\n\tPi: \"Π\",\n\tSigma: \"Σ\",\n\tUpsilon: \"Υ\",\n\tPhi: \"Φ\",\n\tPsi: \"Ψ\",\n\tOmega: \"Ω\",\n\tpm: \"±\",\n\tmp: \"∓\",\n\ttimes: \"×\",\n\tdiv: \"÷\",\n\tcdot: \"·\",\n\tast: \"∗\",\n\tstar: \"⋆\",\n\tcirc: \"∘\",\n\tbullet: \"•\",\n\toplus: \"⊕\",\n\tominus: \"⊖\",\n\totimes: \"⊗\",\n\toslash: \"⊘\",\n\todot: \"⊙\",\n\tbigcirc: \"○\",\n\tdagger: \"†\",\n\tddagger: \"‡\",\n\tamalg: \"⨿\",\n\tuplus: \"⊎\",\n\tsqcap: \"⊓\",\n\tsqcup: \"⊔\",\n\tbowtie: \"⋈\",\n\tJoin: \"⋈\",\n\tltimes: \"⋉\",\n\trtimes: \"⋊\",\n\tleftouterjoin: \"⟕\",\n\trightouterjoin: \"⟖\",\n\tfullouterjoin: \"⟗\",\n\ttriangleleft: \"◁\",\n\ttriangleright: \"▷\",\n\twr: \"≀\",\n\tcap: \"∩\",\n\tcup: \"∪\",\n\tbigcap: \"⋂\",\n\tbigcup: \"⋃\",\n\tbigwedge: \"⋀\",\n\tbigvee: \"⋁\",\n\tbigsqcup: \"⨆\",\n\tbiguplus: \"⨄\",\n\tbigoplus: \"⨁\",\n\tbigotimes: \"⨂\",\n\tbigodot: \"⨀\",\n\tsetminus: \"∖\",\n\tin: \"∈\",\n\tnotin: \"∉\",\n\tni: \"∋\",\n\tsubset: \"⊂\",\n\tsupset: \"⊃\",\n\tsubseteq: \"⊆\",\n\tsupseteq: \"⊇\",\n\tsqsubset: \"⊏\",\n\tsqsupset: \"⊐\",\n\tsqsubseteq: \"⊑\",\n\tsqsupseteq: \"⊒\",\n\tprec: \"≺\",\n\tpreceq: \"≼\",\n\tsucc: \"≻\",\n\tsucceq: \"≽\",\n\tll: \"≪\",\n\tgg: \"≫\",\n\tle: \"≤\",\n\tleq: \"≤\",\n\tleqslant: \"≤\",\n\tge: \"≥\",\n\tgeq: \"≥\",\n\tgeqslant: \"≥\",\n\tne: \"≠\",\n\tneq: \"≠\",\n\tequiv: \"≡\",\n\tapprox: \"≈\",\n\tsim: \"∼\",\n\tsimeq: \"≃\",\n\tcong: \"≅\",\n\tasymp: \"≍\",\n\tdoteq: \"≐\",\n\tpropto: \"∝\",\n\tparallel: \"∥\",\n\tperp: \"⊥\",\n\tmid: \"∣\",\n\tvdash: \"⊢\",\n\tdashv: \"⊣\",\n\tmodels: \"⊨\",\n\tVdash: \"⊩\",\n\tVvdash: \"⊪\",\n\tnvdash: \"⊬\",\n\tnvDash: \"⊭\",\n\tforall: \"∀\",\n\texists: \"∃\",\n\tnexists: \"∄\",\n\tneg: \"¬\",\n\tland: \"∧\",\n\twedge: \"∧\",\n\tlor: \"∨\",\n\tvee: \"∨\",\n\tto: \"→\",\n\trightarrow: \"→\",\n\tlongrightarrow: \"→\",\n\tleftarrow: \"←\",\n\tlongleftarrow: \"←\",\n\tgets: \"←\",\n\tleftrightarrow: \"↔\",\n\tlongleftrightarrow: \"↔\",\n\thookleftarrow: \"↩\",\n\thookrightarrow: \"↪\",\n\ttwoheadleftarrow: \"↞\",\n\ttwoheadrightarrow: \"↠\",\n\tleftharpoonup: \"↼\",\n\tleftharpoondown: \"↽\",\n\trightharpoonup: \"⇀\",\n\trightharpoondown: \"⇁\",\n\trightleftharpoons: \"⇌\",\n\tleftrightharpoons: \"⇋\",\n\tnearrow: \"↗\",\n\tsearrow: \"↘\",\n\tswarrow: \"↙\",\n\tnwarrow: \"↖\",\n\trightsquigarrow: \"⇝\",\n\tleadsto: \"⇝\",\n\tRightarrow: \"⇒\",\n\tLongrightarrow: \"⇒\",\n\tLeftarrow: \"⇐\",\n\tLongleftarrow: \"⇐\",\n\tLeftrightarrow: \"⇔\",\n\tLongleftrightarrow: \"⇔\",\n\timplies: \"⇒\",\n\tiff: \"⇔\",\n\tmapsto: \"↦\",\n\tlongmapsto: \"↦\",\n\tuparrow: \"↑\",\n\tdownarrow: \"↓\",\n\tpartial: \"∂\",\n\tnabla: \"∇\",\n\tint: \"∫\",\n\tiint: \"∬\",\n\tiiint: \"∭\",\n\toint: \"∮\",\n\tsum: \"∑\",\n\tprod: \"∏\",\n\tcoprod: \"∐\",\n\tinfty: \"∞\",\n\temptyset: \"∅\",\n\tvarnothing: \"∅\",\n\tangle: \"∠\",\n\ttherefore: \"∴\",\n\tbecause: \"∵\",\n\taleph: \"ℵ\",\n\tbeth: \"ℶ\",\n\tgimel: \"ℷ\",\n\tdaleth: \"ℸ\",\n\ttop: \"⊤\",\n\tbot: \"⊥\",\n\ttriangle: \"△\",\n\tsquare: \"□\",\n\tlozenge: \"◊\",\n\tcheckmark: \"✓\",\n\tcomplement: \"∁\",\n\twp: \"℘\",\n\tprime: \"′\",\n\tldots: \"…\",\n\tdots: \"…\",\n\tcdots: \"⋯\",\n\tvdots: \"⋮\",\n\tddots: \"⋱\",\n\tell: \"ℓ\",\n\thbar: \"ℏ\",\n\tIm: \"ℑ\",\n\tRe: \"ℜ\",\n\tlangle: \"⟨\",\n\trangle: \"⟩\",\n\tvert: \"|\",\n\tlvert: \"|\",\n\trvert: \"|\",\n\tVert: \"‖\",\n\tlVert: \"‖\",\n\trVert: \"‖\",\n\tlbrace: \"{\",\n\trbrace: \"}\",\n\tbackslash: \"\\\\\",\n\tlfloor: \"⌊\",\n\trfloor: \"⌋\",\n\tlceil: \"⌈\",\n\trceil: \"⌉\",\n\tcolon: \":\",\n};\n\nconst NAMED_OPERATORS = new Set([\n\t\"arccos\",\n\t\"arcsin\",\n\t\"arctan\",\n\t\"arg\",\n\t\"cos\",\n\t\"cosh\",\n\t\"cot\",\n\t\"coth\",\n\t\"csc\",\n\t\"deg\",\n\t\"det\",\n\t\"dim\",\n\t\"exp\",\n\t\"gcd\",\n\t\"hom\",\n\t\"inf\",\n\t\"ker\",\n\t\"lg\",\n\t\"lim\",\n\t\"liminf\",\n\t\"limsup\",\n\t\"ln\",\n\t\"log\",\n\t\"max\",\n\t\"min\",\n\t\"Pr\",\n\t\"sec\",\n\t\"sin\",\n\t\"sinh\",\n\t\"sup\",\n\t\"tan\",\n\t\"tanh\",\n]);\n\nconst LIMIT_OPERATORS = new Set([\n\t\"argmax\",\n\t\"argmin\",\n\t\"inf\",\n\t\"injlim\",\n\t\"lim\",\n\t\"liminf\",\n\t\"limsup\",\n\t\"max\",\n\t\"min\",\n\t\"projlim\",\n\t\"sup\",\n]);\n\nconst DISPLAY_LIMIT_SYMBOLS = new Set([\n\t\"bigcap\",\n\t\"bigcup\",\n\t\"bigodot\",\n\t\"bigoplus\",\n\t\"bigotimes\",\n\t\"bigsqcup\",\n\t\"biguplus\",\n\t\"bigvee\",\n\t\"bigwedge\",\n\t\"coprod\",\n\t\"int\",\n\t\"iint\",\n\t\"iiint\",\n\t\"oint\",\n\t\"prod\",\n\t\"sum\",\n]);\n\nconst RELATION_COMMANDS = new Set([\n\t\"Leftarrow\",\n\t\"Leftrightarrow\",\n\t\"Longleftarrow\",\n\t\"Longleftrightarrow\",\n\t\"Longrightarrow\",\n\t\"Rightarrow\",\n\t\"Join\",\n\t\"Vdash\",\n\t\"Vvdash\",\n\t\"approx\",\n\t\"asymp\",\n\t\"bowtie\",\n\t\"cong\",\n\t\"dashv\",\n\t\"fullouterjoin\",\n\t\"doteq\",\n\t\"downarrow\",\n\t\"equiv\",\n\t\"ge\",\n\t\"geq\",\n\t\"geqslant\",\n\t\"gets\",\n\t\"gg\",\n\t\"hookleftarrow\",\n\t\"hookrightarrow\",\n\t\"iff\",\n\t\"implies\",\n\t\"in\",\n\t\"leadsto\",\n\t\"le\",\n\t\"leftarrow\",\n\t\"leftharpoondown\",\n\t\"leftharpoonup\",\n\t\"leftrightarrow\",\n\t\"leftrightharpoons\",\n\t\"leftouterjoin\",\n\t\"leq\",\n\t\"leqslant\",\n\t\"ll\",\n\t\"longleftarrow\",\n\t\"longleftrightarrow\",\n\t\"longmapsto\",\n\t\"longrightarrow\",\n\t\"ltimes\",\n\t\"mapsto\",\n\t\"mid\",\n\t\"models\",\n\t\"ne\",\n\t\"nearrow\",\n\t\"neq\",\n\t\"ni\",\n\t\"notin\",\n\t\"nvdash\",\n\t\"nvDash\",\n\t\"nwarrow\",\n\t\"parallel\",\n\t\"perp\",\n\t\"prec\",\n\t\"preceq\",\n\t\"propto\",\n\t\"rightharpoondown\",\n\t\"rightharpoonup\",\n\t\"rightleftharpoons\",\n\t\"rightouterjoin\",\n\t\"rightarrow\",\n\t\"rightsquigarrow\",\n\t\"rtimes\",\n\t\"searrow\",\n\t\"sim\",\n\t\"simeq\",\n\t\"sqsubset\",\n\t\"sqsubseteq\",\n\t\"sqsupset\",\n\t\"sqsupseteq\",\n\t\"subset\",\n\t\"subseteq\",\n\t\"succ\",\n\t\"succeq\",\n\t\"supset\",\n\t\"supseteq\",\n\t\"swarrow\",\n\t\"to\",\n\t\"triangleleft\",\n\t\"triangleright\",\n\t\"twoheadleftarrow\",\n\t\"twoheadrightarrow\",\n\t\"uparrow\",\n\t\"vdash\",\n]);\n\nconst NEGATED_SYMBOLS: Readonly<Record<string, string>> = {\n\t\"<\": \"≮\",\n\t\">\": \"≯\",\n\t\"=\": \"≠\",\n\t\"∈\": \"∉\",\n\t\"∋\": \"∌\",\n\t\"∣\": \"∤\",\n\t\"∥\": \"∦\",\n\t\"∼\": \"≁\",\n\t\"≃\": \"≄\",\n\t\"≅\": \"≇\",\n\t\"≈\": \"≉\",\n\t\"≡\": \"≢\",\n\t\"≤\": \"≰\",\n\t\"≥\": \"≱\",\n\t\"≺\": \"⊀\",\n\t\"≻\": \"⊁\",\n\t\"⊂\": \"⊄\",\n\t\"⊃\": \"⊅\",\n\t\"⊆\": \"⊈\",\n\t\"⊇\": \"⊉\",\n\t\"⊢\": \"⊬\",\n\t\"⊨\": \"⊭\",\n\t\"↔\": \"↮\",\n\t\"←\": \"↚\",\n\t\"→\": \"↛\",\n\t\"⇒\": \"⇏\",\n\t\"⇐\": \"⇍\",\n\t\"⇔\": \"⇎\",\n\t\"≼\": \"⋠\",\n\t\"≽\": \"⋡\",\n};\n\nconst BLACKBOARD: Readonly<Record<string, string>> = {\n\tC: \"ℂ\",\n\tH: \"ℍ\",\n\tN: \"ℕ\",\n\tP: \"ℙ\",\n\tQ: \"ℚ\",\n\tR: \"ℝ\",\n\tZ: \"ℤ\",\n};\n\nconst SUPERSCRIPTS: Readonly<Record<string, string>> = {\n\t\"0\": \"⁰\",\n\t\"1\": \"¹\",\n\t\"2\": \"²\",\n\t\"3\": \"³\",\n\t\"4\": \"⁴\",\n\t\"5\": \"⁵\",\n\t\"6\": \"⁶\",\n\t\"7\": \"⁷\",\n\t\"8\": \"⁸\",\n\t\"9\": \"⁹\",\n\t\"+\": \"⁺\",\n\t\"-\": \"⁻\",\n\t\"=\": \"⁼\",\n\t\"(\": \"⁽\",\n\t\")\": \"⁾\",\n\ta: \"ᵃ\",\n\tb: \"ᵇ\",\n\tc: \"ᶜ\",\n\td: \"ᵈ\",\n\te: \"ᵉ\",\n\tf: \"ᶠ\",\n\tg: \"ᵍ\",\n\th: \"ʰ\",\n\ti: \"ⁱ\",\n\tj: \"ʲ\",\n\tk: \"ᵏ\",\n\tl: \"ˡ\",\n\tm: \"ᵐ\",\n\tn: \"ⁿ\",\n\to: \"ᵒ\",\n\tp: \"ᵖ\",\n\tr: \"ʳ\",\n\ts: \"ˢ\",\n\tt: \"ᵗ\",\n\tu: \"ᵘ\",\n\tv: \"ᵛ\",\n\tw: \"ʷ\",\n\tx: \"ˣ\",\n\ty: \"ʸ\",\n\tz: \"ᶻ\",\n};\n\nconst SUBSCRIPTS: Readonly<Record<string, string>> = {\n\t\"0\": \"₀\",\n\t\"1\": \"₁\",\n\t\"2\": \"₂\",\n\t\"3\": \"₃\",\n\t\"4\": \"₄\",\n\t\"5\": \"₅\",\n\t\"6\": \"₆\",\n\t\"7\": \"₇\",\n\t\"8\": \"₈\",\n\t\"9\": \"₉\",\n\t\"+\": \"₊\",\n\t\"-\": \"₋\",\n\t\"=\": \"₌\",\n\t\"(\": \"₍\",\n\t\")\": \"₎\",\n\ta: \"ₐ\",\n\te: \"ₑ\",\n\th: \"ₕ\",\n\ti: \"ᵢ\",\n\tj: \"ⱼ\",\n\tk: \"ₖ\",\n\tl: \"ₗ\",\n\tm: \"ₘ\",\n\tn: \"ₙ\",\n\to: \"ₒ\",\n\tp: \"ₚ\",\n\tr: \"ᵣ\",\n\ts: \"ₛ\",\n\tt: \"ₜ\",\n\tu: \"ᵤ\",\n\tv: \"ᵥ\",\n\tx: \"ₓ\",\n};\n\nconst SPACING_COMMANDS = new Set([\n\t\",\",\n\t\":\",\n\t\";\",\n\t\" \",\n\t\">\",\n\t\"enspace\",\n\t\"enskip\",\n\t\"medspace\",\n\t\"quad\",\n\t\"qquad\",\n\t\"thickspace\",\n\t\"thinspace\",\n]);\nconst NEGATIVE_SPACING_COMMANDS = new Set([\"!\", \"negmedspace\", \"negthickspace\", \"negthinspace\"]);\nconst NEGATIVE_SPACE = \"\\u0000\";\nconst IGNORED_COMMANDS = new Set([\n\t\"displaystyle\",\n\t\"limits\",\n\t\"nolimits\",\n\t\"scriptstyle\",\n\t\"scriptscriptstyle\",\n\t\"textstyle\",\n]);\nconst SIZE_COMMANDS = new Set([\n\t\"big\",\n\t\"Big\",\n\t\"bigg\",\n\t\"Bigg\",\n\t\"bigl\",\n\t\"Bigl\",\n\t\"biggl\",\n\t\"Biggl\",\n\t\"bigr\",\n\t\"Bigr\",\n\t\"biggr\",\n\t\"Biggr\",\n]);\nconst PLAIN_WRAPPERS = new Set([\n\t\"emph\",\n\t\"mathcal\",\n\t\"mathbf\",\n\t\"mathfrak\",\n\t\"mathit\",\n\t\"mathrm\",\n\t\"mathnormal\",\n\t\"mathscr\",\n\t\"mathsf\",\n\t\"mathtt\",\n\t\"mathup\",\n\t\"mbox\",\n\t\"overbrace\",\n\t\"pmb\",\n\t\"smash\",\n\t\"substack\",\n\t\"text\",\n\t\"textbf\",\n\t\"textit\",\n\t\"textmd\",\n\t\"textnormal\",\n\t\"textrm\",\n\t\"textsc\",\n\t\"textsf\",\n\t\"textsl\",\n\t\"texttt\",\n\t\"textup\",\n\t\"underbrace\",\n\t\"bm\",\n\t\"boldsymbol\",\n]);\nconst ACCENTS: Readonly<Record<string, string>> = {\n\tacute: \"\\u0301\",\n\tbar: \"\\u0305\",\n\tbreve: \"\\u0306\",\n\tcheck: \"\\u030c\",\n\tddot: \"\\u0308\",\n\tdot: \"\\u0307\",\n\tgrave: \"\\u0300\",\n\that: \"\\u0302\",\n\tmathring: \"\\u030a\",\n\toverleftarrow: \"\\u20d6\",\n\toverleftrightarrow: \"\\u20e1\",\n\toverline: \"\\u0305\",\n\toverrightarrow: \"\\u20d7\",\n\ttilde: \"\\u0303\",\n\tunderline: \"\\u0332\",\n\tvec: \"\\u20d7\",\n\twidehat: \"\\u0302\",\n\twidetilde: \"\\u0303\",\n};\n\nfunction replaceCharacters(value: string, replacements: Readonly<Record<string, string>>): string | undefined {\n\tlet result = \"\";\n\tfor (const character of value) {\n\t\tconst replacement = replacements[character];\n\t\tif (replacement === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\tresult += replacement;\n\t}\n\treturn result;\n}\n\nfunction formatScript(value: string, kind: \"sub\" | \"sup\"): string {\n\tvalue = value.trim();\n\tconst replacements = kind === \"sub\" ? SUBSCRIPTS : SUPERSCRIPTS;\n\tconst unicode = replaceCharacters(value.replace(/\\s*([=+-])\\s*/g, \"$1\"), replacements);\n\tif (unicode !== undefined) {\n\t\treturn unicode;\n\t}\n\n\tconst prefix = kind === \"sub\" ? \"_\" : \"^\";\n\tif (Array.from(value).length === 1 || (kind === \"sub\" && /^[A-Za-z]+$/.test(value))) {\n\t\treturn `${prefix}${value}`;\n\t}\n\treturn `${prefix}(${value})`;\n}\n\nfunction formatFraction(numerator: string, denominator: string): string {\n\tnumerator = numerator.trim();\n\tdenominator = denominator.trim();\n\tconst simpleNumerator = /^[\\p{L}\\p{N}.]+$/u.test(numerator);\n\tconst simpleDenominator = /^[\\p{N}.]+$/u.test(denominator) || Array.from(denominator).length === 1;\n\treturn `${simpleNumerator ? numerator : `(${numerator})`}/${simpleDenominator ? denominator : `(${denominator})`}`;\n}\n\nfunction formatRoot(value: string, symbol = \"√\"): string {\n\tvalue = value.trim();\n\treturn /^[\\p{L}\\p{N}.]+$/u.test(value) ? `${symbol}${value}` : `${symbol}(${value})`;\n}\n\nconst NAMED_OPERATOR_START = \"\\u{f0004}\";\nconst NAMED_OPERATOR_END = \"\\u{f0005}\";\nconst NAMED_OPERATOR_LEFT_SPACING_PATTERN = /(?<=[\\p{L}\\p{N})\\]}\\u{f0001}])\\u{f0004}/gu;\nconst NAMED_OPERATOR_RIGHT_SPACING_PATTERN = /\\u{f0005}(?=[\\p{L}\\p{N}√\\u{f0000}])/gu;\n\nfunction normalizeOutput(value: string): string {\n\treturn value\n\t\t.replace(NAMED_OPERATOR_LEFT_SPACING_PATTERN, \" \")\n\t\t.replaceAll(NAMED_OPERATOR_START, \"\")\n\t\t.replace(NAMED_OPERATOR_RIGHT_SPACING_PATTERN, \" \")\n\t\t.replaceAll(NAMED_OPERATOR_END, \"\")\n\t\t.split(\"\\n\")\n\t\t.map((line) => line.replace(/[ \\t]+/g, \" \").trim())\n\t\t.filter((line, index, lines) => line.length > 0 || (index > 0 && index < lines.length - 1))\n\t\t.join(\"\\n\")\n\t\t.trim();\n}\n\ninterface FractionNode {\n\ttype: \"fraction\";\n\tnumerator: string;\n\tdenominator: string;\n}\n\ninterface OperatorNode {\n\ttype: \"operator\";\n\toperator: string;\n\tlower?: string;\n\tupper?: string;\n}\n\ninterface MatrixNode {\n\ttype: \"matrix\";\n\tlines: string[];\n\tbaseline: number;\n}\n\ntype LayoutNode = FractionNode | OperatorNode | MatrixNode;\n\ninterface Layout {\n\tlines: string[];\n\twidth: number;\n\tbaseline: number;\n}\n\nconst LAYOUT_MARKER_START = \"\\u{f0000}\";\nconst LAYOUT_MARKER_END = \"\\u{f0001}\";\nconst LAYOUT_MARKER_PATTERN = /\\u{f0000}(\\d+)\\u{f0001}/gu;\nconst TRAILING_LAYOUT_MARKER_PATTERN = /\\u{f0000}(\\d+)\\u{f0001}$/u;\nconst PROTECTED_SPACE = \"\\u{f0002}\";\n\nfunction padLayoutLine(line: string, width: number, centered = false): string {\n\tconst padding = Math.max(0, width - visibleWidth(line));\n\tconst left = centered ? Math.floor(padding / 2) : 0;\n\treturn `${\" \".repeat(left)}${line}${\" \".repeat(padding - left)}`;\n}\n\nfunction joinLayouts(layouts: readonly Layout[]): Layout {\n\tif (layouts.length === 0) {\n\t\treturn { lines: [\"\"], width: 0, baseline: 0 };\n\t}\n\tconst baseline = Math.max(...layouts.map((layout) => layout.baseline));\n\tconst below = Math.max(...layouts.map((layout) => layout.lines.length - layout.baseline - 1));\n\tconst lines: string[] = [];\n\tfor (let row = 0; row <= baseline + below; row++) {\n\t\tlet line = \"\";\n\t\tfor (const layout of layouts) {\n\t\t\tconst sourceRow = row - baseline + layout.baseline;\n\t\t\tline +=\n\t\t\t\tsourceRow >= 0 && sourceRow < layout.lines.length\n\t\t\t\t\t? padLayoutLine(layout.lines[sourceRow] ?? \"\", layout.width)\n\t\t\t\t\t: \" \".repeat(layout.width);\n\t\t}\n\t\tlines.push(line.trimEnd());\n\t}\n\treturn {\n\t\tlines,\n\t\twidth: layouts.reduce((width, layout) => width + layout.width, 0),\n\t\tbaseline,\n\t};\n}\n\nfunction renderLayout(source: string, nodes: readonly LayoutNode[]): Layout {\n\tconst renderedLines: string[] = [];\n\tlet firstBaseline = 0;\n\tfor (const sourceLine of source.split(\"\\n\")) {\n\t\tconst layouts: Layout[] = [];\n\t\tlet position = 0;\n\t\tlet previousNode: LayoutNode | undefined;\n\t\tfor (const match of sourceLine.matchAll(LAYOUT_MARKER_PATTERN)) {\n\t\t\tconst index = match.index;\n\t\t\tconst node = nodes[Number(match[1])];\n\t\t\tif (!node) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (index > position) {\n\t\t\t\tconst sliced = sourceLine.slice(position, index);\n\t\t\t\tconst trimmed = (previousNode ? sliced.trimStart() : sliced).trimEnd();\n\t\t\t\tconst preserveLeadingSpace = previousNode?.type === \"matrix\" && /^\\s/.test(sliced);\n\t\t\t\tconst preserveTrailingSpace = node.type === \"matrix\" && /\\s$/.test(sliced);\n\t\t\t\tconst text = trimmed\n\t\t\t\t\t? `${preserveLeadingSpace ? \" \" : \"\"}${trimmed}${preserveTrailingSpace ? \" \" : \"\"}`\n\t\t\t\t\t: preserveLeadingSpace || preserveTrailingSpace\n\t\t\t\t\t\t? \" \"\n\t\t\t\t\t\t: \"\";\n\t\t\t\tlayouts.push({ lines: [text], width: visibleWidth(text), baseline: 0 });\n\t\t\t}\n\t\t\tif (node.type === \"fraction\") {\n\t\t\t\tconst numerator = renderLayout(node.numerator, nodes);\n\t\t\t\tconst denominator = renderLayout(node.denominator, nodes);\n\t\t\t\tconst contentWidth = Math.max(numerator.width, denominator.width, 1);\n\t\t\t\tconst width = contentWidth + 2;\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines: [\n\t\t\t\t\t\t...numerator.lines.map((line) => padLayoutLine(line, width, true)),\n\t\t\t\t\t\t` ${\"─\".repeat(contentWidth)} `,\n\t\t\t\t\t\t...denominator.lines.map((line) => padLayoutLine(line, width, true)),\n\t\t\t\t\t],\n\t\t\t\t\twidth,\n\t\t\t\t\tbaseline: numerator.lines.length,\n\t\t\t\t});\n\t\t\t} else if (node.type === \"operator\") {\n\t\t\t\tconst contentWidth = Math.max(\n\t\t\t\t\tvisibleWidth(node.operator),\n\t\t\t\t\tnode.lower === undefined ? 0 : visibleWidth(node.lower),\n\t\t\t\t\tnode.upper === undefined ? 0 : visibleWidth(node.upper),\n\t\t\t\t);\n\t\t\t\tconst lines: string[] = [];\n\t\t\t\tif (node.upper !== undefined) {\n\t\t\t\t\tlines.push(`${padLayoutLine(node.upper, contentWidth, true)} `);\n\t\t\t\t}\n\t\t\t\tlines.push(`${padLayoutLine(node.operator, contentWidth, true)} `);\n\t\t\t\tif (node.lower !== undefined) {\n\t\t\t\t\tlines.push(`${padLayoutLine(node.lower, contentWidth, true)} `);\n\t\t\t\t}\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines,\n\t\t\t\t\twidth: contentWidth + 1,\n\t\t\t\t\tbaseline: node.upper === undefined ? 0 : 1,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst width = Math.max(0, ...node.lines.map((line) => visibleWidth(line)));\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines: node.lines.map((line) => padLayoutLine(line, width)),\n\t\t\t\t\twidth,\n\t\t\t\t\tbaseline: node.baseline,\n\t\t\t\t});\n\t\t\t}\n\t\t\tposition = index + match[0].length;\n\t\t\tpreviousNode = node;\n\t\t}\n\t\tif (position < sourceLine.length) {\n\t\t\tconst sliced = sourceLine.slice(position);\n\t\t\tconst trimmed = previousNode ? sliced.trimStart() : sliced;\n\t\t\tconst text = previousNode?.type === \"matrix\" && /^\\s/.test(sliced) ? ` ${trimmed}` : trimmed;\n\t\t\tlayouts.push({ lines: [text], width: visibleWidth(text), baseline: 0 });\n\t\t}\n\t\tconst lineLayout = joinLayouts(layouts);\n\t\tif (renderedLines.length === 0) {\n\t\t\tfirstBaseline = lineLayout.baseline;\n\t\t}\n\t\trenderedLines.push(...lineLayout.lines);\n\t}\n\treturn {\n\t\tlines: renderedLines,\n\t\twidth: Math.max(0, ...renderedLines.map((line) => visibleWidth(line))),\n\t\tbaseline: firstBaseline,\n\t};\n}\n\nclass LatexParser {\n\tprivate readonly source: string;\n\tprivate readonly layoutNodes: LayoutNode[];\n\tprivate readonly display: boolean;\n\tprivate position = 0;\n\tprivate supported = true;\n\tprivate stackFractions = true;\n\n\tconstructor(source: string, layoutNodes: LayoutNode[], display: boolean) {\n\t\tthis.source = source;\n\t\tthis.layoutNodes = layoutNodes;\n\t\tthis.display = display;\n\t}\n\n\trender(): string | undefined {\n\t\tconst rendered = this.parseSequence();\n\t\tif (!this.supported || this.position !== this.source.length) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn normalizeOutput(rendered);\n\t}\n\n\tprivate parseSequence(endCharacter?: string): string {\n\t\tlet result = \"\";\n\t\twhile (this.position < this.source.length) {\n\t\t\tconst character = this.source[this.position];\n\t\t\tif (endCharacter && character === endCharacter) {\n\t\t\t\tthis.position++;\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (character === \"}\") {\n\t\t\t\tthis.supported = false;\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (character === \"{\") {\n\t\t\t\tthis.position++;\n\t\t\t\tresult += this.parseSequence(\"}\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"\\\\\") {\n\t\t\t\tconst command = this.parseCommand();\n\t\t\t\tif (command === NEGATIVE_SPACE) {\n\t\t\t\t\tresult = result.trimEnd();\n\t\t\t\t\tif (result.endsWith(NAMED_OPERATOR_END)) {\n\t\t\t\t\t\tresult = result.slice(0, -NAMED_OPERATOR_END.length);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tresult += command;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"^\" || character === \"_\") {\n\t\t\t\tthis.position++;\n\t\t\t\tresult = result.trimEnd();\n\t\t\t\tconst script = formatScript(this.parseRequiredArgument(false), character === \"_\" ? \"sub\" : \"sup\");\n\t\t\t\tif (result.endsWith(NAMED_OPERATOR_END)) {\n\t\t\t\t\tresult = `${result.slice(0, -NAMED_OPERATOR_END.length)}${script}${NAMED_OPERATOR_END}`;\n\t\t\t\t} else {\n\t\t\t\t\tresult += script;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (/\\s/.test(character)) {\n\t\t\t\tresult += this.parseWhitespace();\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"=\" || character === \"<\" || character === \">\") {\n\t\t\t\tresult = `${result.trimEnd()} ${character} `;\n\t\t\t\tthis.position++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"&\") {\n\t\t\t\tthis.position++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"~\") {\n\t\t\t\tthis.position++;\n\t\t\t\tresult += \" \";\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \".\") {\n\t\t\t\tconst marker = TRAILING_LAYOUT_MARKER_PATTERN.exec(result);\n\t\t\t\tconst node = marker ? this.layoutNodes[Number(marker[1])] : undefined;\n\t\t\t\tif (node?.type === \"matrix\") {\n\t\t\t\t\tconst lastLine = node.lines.length - 1;\n\t\t\t\t\tnode.lines[lastLine] = `${node.lines[lastLine] ?? \"\"}${character}`;\n\t\t\t\t\tthis.position++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tresult += character;\n\t\t\tthis.position++;\n\t\t}\n\n\t\tif (endCharacter) {\n\t\t\tthis.supported = false;\n\t\t}\n\t\treturn result;\n\t}\n\n\tprivate parseWhitespace(): string {\n\t\twhile (this.position < this.source.length && /\\s/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\treturn \" \";\n\t}\n\n\tprivate parseCommand(): string {\n\t\tthis.position++;\n\t\tif (this.position >= this.source.length) {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\n\t\tlet command = \"\";\n\t\tconst first = this.source[this.position] ?? \"\";\n\t\tif (first === \"\\n\" || first === \"\\r\") {\n\t\t\tthis.position++;\n\t\t\tif (first === \"\\r\" && this.source[this.position] === \"\\n\") {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\treturn \" \";\n\t\t}\n\t\tif (/[A-Za-z]/.test(first)) {\n\t\t\tconst start = this.position;\n\t\t\twhile (this.position < this.source.length && /[A-Za-z]/.test(this.source[this.position] ?? \"\")) {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\tcommand = this.source.slice(start, this.position);\n\t\t} else {\n\t\t\tcommand = first;\n\t\t\tthis.position++;\n\t\t}\n\n\t\tif (command === \"\\\\\") {\n\t\t\treturn \"\\n\";\n\t\t}\n\t\tif (SPACING_COMMANDS.has(command)) {\n\t\t\treturn \" \";\n\t\t}\n\t\tif (NEGATIVE_SPACING_COMMANDS.has(command)) {\n\t\t\treturn NEGATIVE_SPACE;\n\t\t}\n\t\tif (IGNORED_COMMANDS.has(command)) {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (\n\t\t\tcommand === \"{\" ||\n\t\t\tcommand === \"}\" ||\n\t\t\tcommand === \"$\" ||\n\t\t\tcommand === \"%\" ||\n\t\t\tcommand === \"#\" ||\n\t\t\tcommand === \"_\" ||\n\t\t\tcommand === \"&\"\n\t\t) {\n\t\t\treturn command;\n\t\t}\n\t\tif (command === \"|\") {\n\t\t\treturn \"‖\";\n\t\t}\n\t\tif (command === \"not\") {\n\t\t\tconst value = this.parseRequiredArgument(false).trim();\n\t\t\tconst negated = NEGATED_SYMBOLS[value];\n\t\t\tif (negated !== undefined) {\n\t\t\t\treturn ` ${negated} `;\n\t\t\t}\n\t\t\tconst characters = Array.from(value);\n\t\t\tif (characters.length === 0) {\n\t\t\t\tthis.supported = false;\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\treturn ` ${characters[0]}\\u0338${characters.slice(1).join(\"\")} `;\n\t\t}\n\t\tif (LIMIT_OPERATORS.has(command)) {\n\t\t\treturn this.parseOperator(command, \"bracket\", true, true);\n\t\t}\n\n\t\tconst symbol = SYMBOLS[command];\n\t\tif (symbol !== undefined) {\n\t\t\tif (DISPLAY_LIMIT_SYMBOLS.has(command)) {\n\t\t\t\treturn this.parseOperator(symbol, \"script\", true);\n\t\t\t}\n\t\t\treturn command === \"cdot\" || command === \"times\" || RELATION_COMMANDS.has(command) ? ` ${symbol} ` : symbol;\n\t\t}\n\t\tif (NAMED_OPERATORS.has(command)) {\n\t\t\treturn `${NAMED_OPERATOR_START}${command}${NAMED_OPERATOR_END}`;\n\t\t}\n\t\tif (SIZE_COMMANDS.has(command)) {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (command === \"left\" || command === \"middle\" || command === \"right\") {\n\t\t\tif (this.source[this.position] === \".\") {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\treturn \"\";\n\t\t}\n\t\tif (command === \"frac\" || command === \"dfrac\" || command === \"tfrac\") {\n\t\t\tconst shouldStack = this.display && this.stackFractions && command !== \"tfrac\";\n\t\t\tconst numerator = this.parseRequiredArgument(!shouldStack);\n\t\t\tconst denominator = this.parseRequiredArgument(!shouldStack);\n\t\t\tif (shouldStack) {\n\t\t\t\tconst index =\n\t\t\t\t\tthis.layoutNodes.push({\n\t\t\t\t\t\ttype: \"fraction\",\n\t\t\t\t\t\tnumerator: normalizeOutput(numerator),\n\t\t\t\t\t\tdenominator: normalizeOutput(denominator),\n\t\t\t\t\t}) - 1;\n\t\t\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t\t\t}\n\t\t\treturn formatFraction(numerator, denominator);\n\t\t}\n\t\tif (command === \"sqrt\") {\n\t\t\tconst degree = this.parseOptionalArgument()?.trim();\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\tif (degree === undefined || degree === \"2\") {\n\t\t\t\treturn formatRoot(value);\n\t\t\t}\n\t\t\tif (degree === \"3\") {\n\t\t\t\treturn formatRoot(value, \"∛\");\n\t\t\t}\n\t\t\tif (degree === \"4\") {\n\t\t\t\treturn formatRoot(value, \"∜\");\n\t\t\t}\n\t\t\treturn `${formatScript(degree, \"sup\")}${formatRoot(value)}`;\n\t\t}\n\t\tif (command === \"boxed\" || command === \"fbox\") {\n\t\t\treturn `[${this.parseRequiredArgument().trim()}]`;\n\t\t}\n\t\tif (command === \"binom\" || command === \"dbinom\" || command === \"tbinom\") {\n\t\t\treturn `(${this.parseRequiredArgument()} choose ${this.parseRequiredArgument()})`;\n\t\t}\n\t\tconst accent = ACCENTS[command];\n\t\tif (accent !== undefined) {\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\treturn Array.from(value).length === 1 ? `${value}${accent}` : `${command}(${value})`;\n\t\t}\n\t\tif (command === \"mathbb\") {\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\treturn Array.from(value, (character) => BLACKBOARD[character] ?? character).join(\"\");\n\t\t}\n\t\tif (command === \"operatorname\") {\n\t\t\tconst starred = this.source[this.position] === \"*\";\n\t\t\tif (starred) {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\tconst operator = normalizeOutput(this.parseRequiredArgument()).trim();\n\t\t\treturn this.parseOperator(operator, \"bracket\", starred, true);\n\t\t}\n\t\tif (command === \"mod\" || command === \"bmod\") {\n\t\t\treturn \" mod \";\n\t\t}\n\t\tif (command === \"pmod\" || command === \"pod\") {\n\t\t\tconst value = this.parseRequiredArgument().trim();\n\t\t\treturn command === \"pmod\" ? ` (mod ${value})` : ` (${value})`;\n\t\t}\n\t\tif (command === \"overset\" || command === \"stackrel\") {\n\t\t\tconst upper = this.parseRequiredArgument();\n\t\t\tconst value = this.parseRequiredArgument().trim();\n\t\t\treturn `${value}${formatScript(upper, \"sup\")}`;\n\t\t}\n\t\tif (command === \"underset\") {\n\t\t\tconst lower = this.parseRequiredArgument();\n\t\t\tconst value = this.parseRequiredArgument().trim();\n\t\t\treturn `${value}${formatScript(lower, \"sub\")}`;\n\t\t}\n\t\tif (PLAIN_WRAPPERS.has(command)) {\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\treturn command.startsWith(\"text\") || command === \"mbox\" ? value : value.trim();\n\t\t}\n\t\tif (command === \"begin\") {\n\t\t\treturn this.parseEnvironment();\n\t\t}\n\t\tif (command === \"end\") {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\n\t\tthis.supported = false;\n\t\treturn `\\\\${command}`;\n\t}\n\n\tprivate parseOperator(\n\t\toperator: string,\n\t\tinlineLowerStyle: \"bracket\" | \"script\",\n\t\tdisplayLimits: boolean,\n\t\tspaced = false,\n\t): string {\n\t\tlet useDisplayLimits = displayLimits;\n\t\tlet modifierPosition = this.position;\n\t\twhile (modifierPosition < this.source.length && /[ \\t]/.test(this.source[modifierPosition] ?? \"\")) {\n\t\t\tmodifierPosition++;\n\t\t}\n\t\tconst modifier = /^\\\\(limits|nolimits)(?![A-Za-z])/.exec(this.source.slice(modifierPosition));\n\t\tif (modifier) {\n\t\t\tuseDisplayLimits = modifier[1] === \"limits\";\n\t\t\tthis.position = modifierPosition + modifier[0].length;\n\t\t}\n\n\t\tlet lower: string | undefined;\n\t\tlet upper: string | undefined;\n\t\twhile (true) {\n\t\t\tlet scriptPosition = this.position;\n\t\t\twhile (scriptPosition < this.source.length && /[ \\t]/.test(this.source[scriptPosition] ?? \"\")) {\n\t\t\t\tscriptPosition++;\n\t\t\t}\n\t\t\tconst kind = this.source[scriptPosition];\n\t\t\tif (kind !== \"_\" && kind !== \"^\") {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tthis.position = scriptPosition + 1;\n\t\t\tconst value = normalizeOutput(this.parseRequiredArgument(false)).replaceAll(\" \", \"\");\n\t\t\tif (kind === \"_\") {\n\t\t\t\tif (lower !== undefined) {\n\t\t\t\t\tthis.supported = false;\n\t\t\t\t}\n\t\t\t\tlower = value;\n\t\t\t} else {\n\t\t\t\tif (upper !== undefined) {\n\t\t\t\t\tthis.supported = false;\n\t\t\t\t}\n\t\t\t\tupper = value;\n\t\t\t}\n\t\t}\n\n\t\tif (this.display && useDisplayLimits && (lower !== undefined || upper !== undefined)) {\n\t\t\tconst index = this.layoutNodes.push({ type: \"operator\", operator, lower, upper }) - 1;\n\t\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t\t}\n\n\t\tlet rendered = operator;\n\t\tif (lower !== undefined) {\n\t\t\trendered += inlineLowerStyle === \"bracket\" ? `[${lower}]` : formatScript(lower, \"sub\");\n\t\t}\n\t\tif (upper !== undefined) {\n\t\t\trendered += formatScript(upper, \"sup\");\n\t\t}\n\t\treturn spaced ? ` ${rendered} ` : rendered;\n\t}\n\n\tprivate parseRequiredArgument(stackFractions = true): string {\n\t\tconst previousStackFractions = this.stackFractions;\n\t\tthis.stackFractions = previousStackFractions && stackFractions;\n\t\tconst value = this.parseRequiredArgumentValue();\n\t\tthis.stackFractions = previousStackFractions;\n\t\treturn value;\n\t}\n\n\tprivate parseRequiredArgumentValue(): string {\n\t\twhile (this.position < this.source.length && /\\s/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\tif (this.position >= this.source.length) {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\t\tif (this.source[this.position] === \"{\") {\n\t\t\tthis.position++;\n\t\t\treturn this.parseSequence(\"}\");\n\t\t}\n\t\tif (this.source[this.position] === \"\\\\\") {\n\t\t\treturn this.parseCommand();\n\t\t}\n\t\tconst value = this.source[this.position] ?? \"\";\n\t\tthis.position++;\n\t\treturn value;\n\t}\n\n\tprivate parseOptionalArgument(): string | undefined {\n\t\twhile (this.position < this.source.length && /[ \\t]/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\tif (this.source[this.position] !== \"[\") {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst end = this.source.indexOf(\"]\", this.position + 1);\n\t\tif (end < 0) {\n\t\t\tthis.supported = false;\n\t\t\treturn undefined;\n\t\t}\n\t\tconst value = this.source.slice(this.position + 1, end);\n\t\tthis.position = end + 1;\n\t\treturn this.renderNested(value);\n\t}\n\n\tprivate readRawGroup(): string | undefined {\n\t\twhile (this.position < this.source.length && /[ \\t]/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\tif (this.source[this.position] !== \"{\") {\n\t\t\tthis.supported = false;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst start = ++this.position;\n\t\tlet depth = 1;\n\t\twhile (this.position < this.source.length) {\n\t\t\tconst character = this.source[this.position];\n\t\t\tif (character === \"\\\\\") {\n\t\t\t\tthis.position += 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (character === \"{\") depth++;\n\t\t\tif (character === \"}\") depth--;\n\t\t\tif (depth === 0) {\n\t\t\t\tconst value = this.source.slice(start, this.position);\n\t\t\t\tthis.position++;\n\t\t\t\treturn value;\n\t\t\t}\n\t\t\tthis.position++;\n\t\t}\n\t\tthis.supported = false;\n\t\treturn undefined;\n\t}\n\n\tprivate splitEnvironmentRows(body: string): string[] {\n\t\treturn body.split(/\\\\\\\\(?:\\[[^\\]\\n]*\\])?/);\n\t}\n\n\tprivate parseEnvironment(): string {\n\t\tconst environment = this.readRawGroup();\n\t\tif (!environment) {\n\t\t\treturn \"\";\n\t\t}\n\t\tconst endMarker = `\\\\end{${environment}}`;\n\t\tconst end = this.source.indexOf(endMarker, this.position);\n\t\tif (end < 0) {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\t\tconst body = this.source.slice(this.position, end);\n\t\tthis.position = end + endMarker.length;\n\n\t\tif (environment === \"equation\" || environment === \"equation*\" || environment === \"displaymath\") {\n\t\t\treturn this.renderNested(body).trim();\n\t\t}\n\n\t\tif (\n\t\t\tenvironment === \"aligned\" ||\n\t\t\tenvironment === \"align\" ||\n\t\t\tenvironment === \"align*\" ||\n\t\t\tenvironment === \"alignedat\" ||\n\t\t\tenvironment === \"alignat\" ||\n\t\t\tenvironment === \"alignat*\" ||\n\t\t\tenvironment === \"gather\" ||\n\t\t\tenvironment === \"gathered\" ||\n\t\t\tenvironment === \"multline\" ||\n\t\t\tenvironment === \"multline*\" ||\n\t\t\tenvironment === \"split\"\n\t\t) {\n\t\t\tconst alignedAt = [\"alignedat\", \"alignat\", \"alignat*\"].includes(environment);\n\t\t\tconst alignedBody = alignedAt ? body.replace(/^\\s*\\{[^}]*\\}/, \"\") : body;\n\t\t\treturn this.splitEnvironmentRows(alignedBody)\n\t\t\t\t.map((row) => {\n\t\t\t\t\tconst cells = row.split(\"&\");\n\t\t\t\t\tconst source = alignedAt\n\t\t\t\t\t\t? Array.from({ length: Math.ceil(cells.length / 2) }, (_, index) =>\n\t\t\t\t\t\t\t\tcells.slice(index * 2, index * 2 + 2).join(\"\"),\n\t\t\t\t\t\t\t).join(\" \")\n\t\t\t\t\t\t: cells.join(\"\");\n\t\t\t\t\treturn this.renderNested(source).trim();\n\t\t\t\t})\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(\"\\n\");\n\t\t}\n\n\t\tif (environment === \"cases\" || environment === \"cases*\") {\n\t\t\tconst rows = this.splitEnvironmentRows(body)\n\t\t\t\t.map((row) => row.split(\"&\").map((cell) => this.renderNested(cell, false).trim()))\n\t\t\t\t.filter((row) => row.some(Boolean));\n\t\t\treturn rows\n\t\t\t\t.map((row, index) => {\n\t\t\t\t\tconst value = (row[0] ?? \"\").replace(/,\\s*$/, \"\");\n\t\t\t\t\tconst condition = row[1] ?? \"\";\n\t\t\t\t\tconst delimiter = index === 0 ? \"⎧\" : index === rows.length - 1 ? \"⎩\" : \"⎨\";\n\t\t\t\t\tconst conditionPrefix = /^(?:if|when|for|otherwise)\\b/i.test(condition) ? \" \" : \" if \";\n\t\t\t\t\treturn `${delimiter} ${value}${condition ? `${conditionPrefix}${condition}` : \"\"}`;\n\t\t\t\t})\n\t\t\t\t.join(\"\\n\");\n\t\t}\n\n\t\tif (\n\t\t\t[\"array\", \"matrix\", \"smallmatrix\", \"pmatrix\", \"bmatrix\", \"Bmatrix\", \"vmatrix\", \"Vmatrix\"].includes(environment)\n\t\t) {\n\t\t\tconst matrixBody = environment === \"array\" ? body.replace(/^\\s*\\{[^}]*\\}/, \"\") : body;\n\t\t\treturn this.renderMatrix(environment, matrixBody);\n\t\t}\n\n\t\tthis.supported = false;\n\t\treturn body;\n\t}\n\n\tprivate renderMatrix(environment: string, body: string): string {\n\t\tconst matrix = this.splitEnvironmentRows(body)\n\t\t\t.map((row) => row.split(\"&\").map((cell) => this.renderNested(cell, false).trim()))\n\t\t\t.filter((row) => row.some(Boolean));\n\t\tconst columnCount = Math.max(0, ...matrix.map((row) => row.length));\n\t\tconst columnWidths = Array.from({ length: columnCount }, (_, column) =>\n\t\t\tMath.max(0, ...matrix.map((row) => visibleWidth(row[column] ?? \"\"))),\n\t\t);\n\t\tconst rows = matrix.map((row) =>\n\t\t\tArray.from({ length: columnCount }, (_, column) => {\n\t\t\t\tconst cell = row[column] ?? \"\";\n\t\t\t\treturn `${cell}${PROTECTED_SPACE.repeat(Math.max(0, (columnWidths[column] ?? 0) - visibleWidth(cell)))}`;\n\t\t\t}).join(\" │ \"),\n\t\t);\n\n\t\tlet lines: string[];\n\t\tif (environment === \"array\" || environment === \"matrix\" || environment === \"smallmatrix\") {\n\t\t\tlines = rows;\n\t\t} else {\n\t\t\tconst delimiters: Readonly<Record<string, readonly [string, string, string, string, string, string]>> = {\n\t\t\t\tpmatrix: [\"⎛\", \"⎞\", \"⎜\", \"⎟\", \"⎝\", \"⎠\"],\n\t\t\t\tbmatrix: [\"⎡\", \"⎤\", \"⎢\", \"⎥\", \"⎣\", \"⎦\"],\n\t\t\t\tBmatrix: [\"⎧\", \"⎫\", \"⎨\", \"⎬\", \"⎩\", \"⎭\"],\n\t\t\t\tvmatrix: [\"│\", \"│\", \"│\", \"│\", \"│\", \"│\"],\n\t\t\t\tVmatrix: [\"║\", \"║\", \"║\", \"║\", \"║\", \"║\"],\n\t\t\t};\n\t\t\tconst delimiter = delimiters[environment];\n\t\t\tif (!delimiter) {\n\t\t\t\tthis.supported = false;\n\t\t\t\treturn rows.join(\"\\n\");\n\t\t\t}\n\t\t\tlines = rows.map((row, index) => {\n\t\t\t\tconst left = index === 0 ? delimiter[0] : index === rows.length - 1 ? delimiter[4] : delimiter[2];\n\t\t\t\tconst right = index === 0 ? delimiter[1] : index === rows.length - 1 ? delimiter[5] : delimiter[3];\n\t\t\t\treturn `${left} ${row} ${right}`;\n\t\t\t});\n\t\t}\n\n\t\tif (lines.length <= 1) {\n\t\t\treturn lines[0] ?? \"\";\n\t\t}\n\t\tconst index = this.layoutNodes.push({ type: \"matrix\", lines, baseline: 0 }) - 1;\n\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t}\n\n\tprivate renderNested(source: string, stackFractions = true): string {\n\t\tconst rendered = new LatexParser(source, this.layoutNodes, this.display && stackFractions).render();\n\t\tif (rendered === undefined) {\n\t\t\tthis.supported = false;\n\t\t\treturn source;\n\t\t}\n\t\treturn rendered;\n\t}\n}\n\nexport interface RenderLatexOptions {\n\t/** Stack fractions and operator limits vertically for display math (default: false). */\n\tdisplay?: boolean;\n}\n\n/**\n * Render a basic LaTeX math expression as terminal-friendly Unicode text.\n * Returns undefined when the expression contains unsupported or malformed syntax.\n */\nexport function renderLatex(source: string, options: RenderLatexOptions = {}): string | undefined {\n\tconst layoutNodes: LayoutNode[] = [];\n\tconst rendered = new LatexParser(source, layoutNodes, options.display === true).render();\n\tif (rendered === undefined) {\n\t\treturn undefined;\n\t}\n\tif (layoutNodes.length === 0) {\n\t\treturn rendered.replaceAll(PROTECTED_SPACE, \" \");\n\t}\n\tconst lines = renderLayout(rendered, layoutNodes).lines;\n\tconst indentation = Math.min(\n\t\t...lines.filter((line) => line.trim()).map((line) => line.length - line.trimStart().length),\n\t);\n\treturn lines\n\t\t.map((line) => line.slice(indentation).trimEnd())\n\t\t.join(\"\\n\")\n\t\t.trimEnd()\n\t\t.replaceAll(PROTECTED_SPACE, \" \");\n}\n"]}
1
+ {"version":3,"file":"latex.d.ts","sourceRoot":"","sources":["../src/latex.ts"],"names":[],"mappings":"AAs8CA,MAAM,WAAW,kBAAkB;IAClC,wFAAwF;IACxF,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,GAAG,SAAS,CAkBhG","sourcesContent":["import { visibleWidth } from \"./utils.ts\";\n\nconst SYMBOLS: Readonly<Record<string, string>> = {\n\talpha: \"α\",\n\tbeta: \"β\",\n\tgamma: \"γ\",\n\tdelta: \"δ\",\n\tepsilon: \"ϵ\",\n\tvarepsilon: \"ε\",\n\tzeta: \"ζ\",\n\teta: \"η\",\n\ttheta: \"θ\",\n\tvartheta: \"ϑ\",\n\tiota: \"ι\",\n\tkappa: \"κ\",\n\tvarkappa: \"ϰ\",\n\tlambda: \"λ\",\n\tmu: \"μ\",\n\tnu: \"ν\",\n\txi: \"ξ\",\n\tpi: \"π\",\n\tvarpi: \"ϖ\",\n\trho: \"ρ\",\n\tvarrho: \"ϱ\",\n\tsigma: \"σ\",\n\tvarsigma: \"ς\",\n\ttau: \"τ\",\n\tupsilon: \"υ\",\n\tphi: \"ϕ\",\n\tvarphi: \"φ\",\n\tchi: \"χ\",\n\tpsi: \"ψ\",\n\tomega: \"ω\",\n\tGamma: \"Γ\",\n\tDelta: \"Δ\",\n\tTheta: \"Θ\",\n\tLambda: \"Λ\",\n\tXi: \"Ξ\",\n\tPi: \"Π\",\n\tSigma: \"Σ\",\n\tUpsilon: \"Υ\",\n\tPhi: \"Φ\",\n\tPsi: \"Ψ\",\n\tOmega: \"Ω\",\n\tpm: \"±\",\n\tmp: \"∓\",\n\ttimes: \"×\",\n\tdiv: \"÷\",\n\tcdot: \"·\",\n\tast: \"∗\",\n\tstar: \"⋆\",\n\tcirc: \"∘\",\n\tbullet: \"•\",\n\toplus: \"⊕\",\n\tominus: \"⊖\",\n\totimes: \"⊗\",\n\toslash: \"⊘\",\n\todot: \"⊙\",\n\tbigcirc: \"○\",\n\tdagger: \"†\",\n\tddagger: \"‡\",\n\tamalg: \"⨿\",\n\tuplus: \"⊎\",\n\tsqcap: \"⊓\",\n\tsqcup: \"⊔\",\n\tbowtie: \"⋈\",\n\tJoin: \"⋈\",\n\tltimes: \"⋉\",\n\trtimes: \"⋊\",\n\tleftouterjoin: \"⟕\",\n\trightouterjoin: \"⟖\",\n\tfullouterjoin: \"⟗\",\n\ttriangleleft: \"◁\",\n\ttriangleright: \"▷\",\n\twr: \"≀\",\n\tcap: \"∩\",\n\tcup: \"∪\",\n\tbigcap: \"⋂\",\n\tbigcup: \"⋃\",\n\tbigwedge: \"⋀\",\n\tbigvee: \"⋁\",\n\tbigsqcup: \"⨆\",\n\tbiguplus: \"⨄\",\n\tbigoplus: \"⨁\",\n\tbigotimes: \"⨂\",\n\tbigodot: \"⨀\",\n\tsetminus: \"∖\",\n\tin: \"∈\",\n\tnotin: \"∉\",\n\tni: \"∋\",\n\tsubset: \"⊂\",\n\tsupset: \"⊃\",\n\tsubseteq: \"⊆\",\n\tsupseteq: \"⊇\",\n\tsqsubset: \"⊏\",\n\tsqsupset: \"⊐\",\n\tsqsubseteq: \"⊑\",\n\tsqsupseteq: \"⊒\",\n\tprec: \"≺\",\n\tpreceq: \"≼\",\n\tsucc: \"≻\",\n\tsucceq: \"≽\",\n\tll: \"≪\",\n\tgg: \"≫\",\n\tle: \"≤\",\n\tleq: \"≤\",\n\tleqslant: \"≤\",\n\tge: \"≥\",\n\tgeq: \"≥\",\n\tgeqslant: \"≥\",\n\tne: \"≠\",\n\tneq: \"≠\",\n\tequiv: \"≡\",\n\tapprox: \"≈\",\n\tsim: \"∼\",\n\tsimeq: \"≃\",\n\tcong: \"≅\",\n\tasymp: \"≍\",\n\tdoteq: \"≐\",\n\tpropto: \"∝\",\n\tparallel: \"∥\",\n\tperp: \"⊥\",\n\tmid: \"∣\",\n\tvdash: \"⊢\",\n\tdashv: \"⊣\",\n\tmodels: \"⊨\",\n\tVdash: \"⊩\",\n\tVvdash: \"⊪\",\n\tnvdash: \"⊬\",\n\tnvDash: \"⊭\",\n\tforall: \"∀\",\n\texists: \"∃\",\n\tnexists: \"∄\",\n\tneg: \"¬\",\n\tland: \"∧\",\n\twedge: \"∧\",\n\tlor: \"∨\",\n\tvee: \"∨\",\n\tto: \"→\",\n\trightarrow: \"→\",\n\tlongrightarrow: \"→\",\n\tleftarrow: \"←\",\n\tlongleftarrow: \"←\",\n\tgets: \"←\",\n\tleftrightarrow: \"↔\",\n\tlongleftrightarrow: \"↔\",\n\thookleftarrow: \"↩\",\n\thookrightarrow: \"↪\",\n\ttwoheadleftarrow: \"↞\",\n\ttwoheadrightarrow: \"↠\",\n\tleftharpoonup: \"↼\",\n\tleftharpoondown: \"↽\",\n\trightharpoonup: \"⇀\",\n\trightharpoondown: \"⇁\",\n\trightleftharpoons: \"⇌\",\n\tleftrightharpoons: \"⇋\",\n\tnearrow: \"↗\",\n\tsearrow: \"↘\",\n\tswarrow: \"↙\",\n\tnwarrow: \"↖\",\n\trightsquigarrow: \"⇝\",\n\tleadsto: \"⇝\",\n\tRightarrow: \"⇒\",\n\tLongrightarrow: \"⇒\",\n\tLeftarrow: \"⇐\",\n\tLongleftarrow: \"⇐\",\n\tLeftrightarrow: \"⇔\",\n\tLongleftrightarrow: \"⇔\",\n\timplies: \"⇒\",\n\tiff: \"⇔\",\n\tmapsto: \"↦\",\n\tlongmapsto: \"↦\",\n\tuparrow: \"↑\",\n\tdownarrow: \"↓\",\n\tpartial: \"∂\",\n\tnabla: \"∇\",\n\tint: \"∫\",\n\tiint: \"∬\",\n\tiiint: \"∭\",\n\toint: \"∮\",\n\tsum: \"∑\",\n\tprod: \"∏\",\n\tcoprod: \"∐\",\n\tinfty: \"∞\",\n\temptyset: \"∅\",\n\tvarnothing: \"∅\",\n\tangle: \"∠\",\n\ttherefore: \"∴\",\n\tbecause: \"∵\",\n\taleph: \"ℵ\",\n\tbeth: \"ℶ\",\n\tgimel: \"ℷ\",\n\tdaleth: \"ℸ\",\n\ttop: \"⊤\",\n\tbot: \"⊥\",\n\ttriangle: \"△\",\n\tsquare: \"□\",\n\tlozenge: \"◊\",\n\tcheckmark: \"✓\",\n\tcomplement: \"∁\",\n\twp: \"℘\",\n\tprime: \"′\",\n\tldots: \"…\",\n\tdots: \"…\",\n\tcdots: \"⋯\",\n\tvdots: \"⋮\",\n\tddots: \"⋱\",\n\tell: \"ℓ\",\n\thbar: \"ℏ\",\n\tIm: \"ℑ\",\n\tRe: \"ℜ\",\n\tlangle: \"⟨\",\n\trangle: \"⟩\",\n\tvert: \"|\",\n\tlvert: \"|\",\n\trvert: \"|\",\n\tVert: \"‖\",\n\tlVert: \"‖\",\n\trVert: \"‖\",\n\tlbrace: \"{\",\n\trbrace: \"}\",\n\tbackslash: \"\\\\\",\n\tlfloor: \"⌊\",\n\trfloor: \"⌋\",\n\tlceil: \"⌈\",\n\trceil: \"⌉\",\n\tcolon: \":\",\n};\n\nconst NAMED_OPERATORS = new Set([\n\t\"arccos\",\n\t\"arcsin\",\n\t\"arctan\",\n\t\"arg\",\n\t\"cos\",\n\t\"cosh\",\n\t\"cot\",\n\t\"coth\",\n\t\"csc\",\n\t\"deg\",\n\t\"det\",\n\t\"dim\",\n\t\"exp\",\n\t\"gcd\",\n\t\"hom\",\n\t\"inf\",\n\t\"ker\",\n\t\"lg\",\n\t\"lim\",\n\t\"liminf\",\n\t\"limsup\",\n\t\"ln\",\n\t\"log\",\n\t\"max\",\n\t\"min\",\n\t\"Pr\",\n\t\"sec\",\n\t\"sin\",\n\t\"sinh\",\n\t\"sup\",\n\t\"tan\",\n\t\"tanh\",\n]);\n\nconst LIMIT_OPERATORS = new Set([\n\t\"argmax\",\n\t\"argmin\",\n\t\"inf\",\n\t\"injlim\",\n\t\"lim\",\n\t\"liminf\",\n\t\"limsup\",\n\t\"max\",\n\t\"min\",\n\t\"projlim\",\n\t\"sup\",\n]);\n\nconst DISPLAY_LIMIT_SYMBOLS = new Set([\n\t\"bigcap\",\n\t\"bigcup\",\n\t\"bigodot\",\n\t\"bigoplus\",\n\t\"bigotimes\",\n\t\"bigsqcup\",\n\t\"biguplus\",\n\t\"bigvee\",\n\t\"bigwedge\",\n\t\"coprod\",\n\t\"int\",\n\t\"iint\",\n\t\"iiint\",\n\t\"oint\",\n\t\"prod\",\n\t\"sum\",\n]);\n\nconst RELATION_COMMANDS = new Set([\n\t\"Leftarrow\",\n\t\"Leftrightarrow\",\n\t\"Longleftarrow\",\n\t\"Longleftrightarrow\",\n\t\"Longrightarrow\",\n\t\"Rightarrow\",\n\t\"Join\",\n\t\"Vdash\",\n\t\"Vvdash\",\n\t\"approx\",\n\t\"asymp\",\n\t\"bowtie\",\n\t\"cong\",\n\t\"dashv\",\n\t\"fullouterjoin\",\n\t\"doteq\",\n\t\"downarrow\",\n\t\"equiv\",\n\t\"ge\",\n\t\"geq\",\n\t\"geqslant\",\n\t\"gets\",\n\t\"gg\",\n\t\"hookleftarrow\",\n\t\"hookrightarrow\",\n\t\"iff\",\n\t\"implies\",\n\t\"in\",\n\t\"leadsto\",\n\t\"le\",\n\t\"leftarrow\",\n\t\"leftharpoondown\",\n\t\"leftharpoonup\",\n\t\"leftrightarrow\",\n\t\"leftrightharpoons\",\n\t\"leftouterjoin\",\n\t\"leq\",\n\t\"leqslant\",\n\t\"ll\",\n\t\"longleftarrow\",\n\t\"longleftrightarrow\",\n\t\"longmapsto\",\n\t\"longrightarrow\",\n\t\"ltimes\",\n\t\"mapsto\",\n\t\"mid\",\n\t\"models\",\n\t\"ne\",\n\t\"nearrow\",\n\t\"neq\",\n\t\"ni\",\n\t\"notin\",\n\t\"nvdash\",\n\t\"nvDash\",\n\t\"nwarrow\",\n\t\"parallel\",\n\t\"perp\",\n\t\"prec\",\n\t\"preceq\",\n\t\"propto\",\n\t\"rightharpoondown\",\n\t\"rightharpoonup\",\n\t\"rightleftharpoons\",\n\t\"rightouterjoin\",\n\t\"rightarrow\",\n\t\"rightsquigarrow\",\n\t\"rtimes\",\n\t\"searrow\",\n\t\"sim\",\n\t\"simeq\",\n\t\"sqsubset\",\n\t\"sqsubseteq\",\n\t\"sqsupset\",\n\t\"sqsupseteq\",\n\t\"subset\",\n\t\"subseteq\",\n\t\"succ\",\n\t\"succeq\",\n\t\"supset\",\n\t\"supseteq\",\n\t\"swarrow\",\n\t\"to\",\n\t\"triangleleft\",\n\t\"triangleright\",\n\t\"twoheadleftarrow\",\n\t\"twoheadrightarrow\",\n\t\"uparrow\",\n\t\"vdash\",\n]);\n\nconst NEGATED_SYMBOLS: Readonly<Record<string, string>> = {\n\t\"<\": \"≮\",\n\t\">\": \"≯\",\n\t\"=\": \"≠\",\n\t\"∈\": \"∉\",\n\t\"∋\": \"∌\",\n\t\"∣\": \"∤\",\n\t\"∥\": \"∦\",\n\t\"∼\": \"≁\",\n\t\"≃\": \"≄\",\n\t\"≅\": \"≇\",\n\t\"≈\": \"≉\",\n\t\"≡\": \"≢\",\n\t\"≤\": \"≰\",\n\t\"≥\": \"≱\",\n\t\"≺\": \"⊀\",\n\t\"≻\": \"⊁\",\n\t\"⊂\": \"⊄\",\n\t\"⊃\": \"⊅\",\n\t\"⊆\": \"⊈\",\n\t\"⊇\": \"⊉\",\n\t\"⊢\": \"⊬\",\n\t\"⊨\": \"⊭\",\n\t\"↔\": \"↮\",\n\t\"←\": \"↚\",\n\t\"→\": \"↛\",\n\t\"⇒\": \"⇏\",\n\t\"⇐\": \"⇍\",\n\t\"⇔\": \"⇎\",\n\t\"≼\": \"⋠\",\n\t\"≽\": \"⋡\",\n};\n\nconst BLACKBOARD: Readonly<Record<string, string>> = {\n\tC: \"ℂ\",\n\tH: \"ℍ\",\n\tN: \"ℕ\",\n\tP: \"ℙ\",\n\tQ: \"ℚ\",\n\tR: \"ℝ\",\n\tZ: \"ℤ\",\n};\n\nconst SUPERSCRIPTS: Readonly<Record<string, string>> = {\n\t\"0\": \"⁰\",\n\t\"1\": \"¹\",\n\t\"2\": \"²\",\n\t\"3\": \"³\",\n\t\"4\": \"⁴\",\n\t\"5\": \"⁵\",\n\t\"6\": \"⁶\",\n\t\"7\": \"⁷\",\n\t\"8\": \"⁸\",\n\t\"9\": \"⁹\",\n\t\"+\": \"⁺\",\n\t\"-\": \"⁻\",\n\t\"=\": \"⁼\",\n\t\"(\": \"⁽\",\n\t\")\": \"⁾\",\n\ta: \"ᵃ\",\n\tb: \"ᵇ\",\n\tc: \"ᶜ\",\n\td: \"ᵈ\",\n\te: \"ᵉ\",\n\tf: \"ᶠ\",\n\tg: \"ᵍ\",\n\th: \"ʰ\",\n\ti: \"ⁱ\",\n\tj: \"ʲ\",\n\tk: \"ᵏ\",\n\tl: \"ˡ\",\n\tm: \"ᵐ\",\n\tn: \"ⁿ\",\n\to: \"ᵒ\",\n\tp: \"ᵖ\",\n\tr: \"ʳ\",\n\ts: \"ˢ\",\n\tt: \"ᵗ\",\n\tu: \"ᵘ\",\n\tv: \"ᵛ\",\n\tw: \"ʷ\",\n\tx: \"ˣ\",\n\ty: \"ʸ\",\n\tz: \"ᶻ\",\n};\n\nconst SUBSCRIPTS: Readonly<Record<string, string>> = {\n\t\"0\": \"₀\",\n\t\"1\": \"₁\",\n\t\"2\": \"₂\",\n\t\"3\": \"₃\",\n\t\"4\": \"₄\",\n\t\"5\": \"₅\",\n\t\"6\": \"₆\",\n\t\"7\": \"₇\",\n\t\"8\": \"₈\",\n\t\"9\": \"₉\",\n\t\"+\": \"₊\",\n\t\"-\": \"₋\",\n\t\"=\": \"₌\",\n\t\"(\": \"₍\",\n\t\")\": \"₎\",\n\ta: \"ₐ\",\n\te: \"ₑ\",\n\th: \"ₕ\",\n\ti: \"ᵢ\",\n\tj: \"ⱼ\",\n\tk: \"ₖ\",\n\tl: \"ₗ\",\n\tm: \"ₘ\",\n\tn: \"ₙ\",\n\to: \"ₒ\",\n\tp: \"ₚ\",\n\tr: \"ᵣ\",\n\ts: \"ₛ\",\n\tt: \"ₜ\",\n\tu: \"ᵤ\",\n\tv: \"ᵥ\",\n\tx: \"ₓ\",\n};\n\nconst SPACING_COMMANDS = new Set([\n\t\",\",\n\t\":\",\n\t\";\",\n\t\" \",\n\t\">\",\n\t\"enspace\",\n\t\"enskip\",\n\t\"medspace\",\n\t\"quad\",\n\t\"qquad\",\n\t\"thickspace\",\n\t\"thinspace\",\n]);\nconst NEGATIVE_SPACING_COMMANDS = new Set([\"!\", \"negmedspace\", \"negthickspace\", \"negthinspace\"]);\nconst NEGATIVE_SPACE = \"\\u0000\";\nconst FONT_SWITCH_COMMANDS = new Set([\"bf\", \"cal\", \"it\", \"rm\", \"sf\", \"sl\", \"tt\"]);\nconst IGNORED_COMMANDS = new Set([\n\t\"displaystyle\",\n\t\"limits\",\n\t\"nolimits\",\n\t\"scriptstyle\",\n\t\"scriptscriptstyle\",\n\t\"textstyle\",\n]);\nconst SIZE_COMMANDS = new Set([\n\t\"big\",\n\t\"Big\",\n\t\"bigg\",\n\t\"Bigg\",\n\t\"bigl\",\n\t\"Bigl\",\n\t\"biggl\",\n\t\"Biggl\",\n\t\"bigr\",\n\t\"Bigr\",\n\t\"biggr\",\n\t\"Biggr\",\n]);\nconst PLAIN_WRAPPERS = new Set([\n\t\"emph\",\n\t\"mathcal\",\n\t\"mathbf\",\n\t\"mathfrak\",\n\t\"mathit\",\n\t\"mathrm\",\n\t\"mathnormal\",\n\t\"mathscr\",\n\t\"mathsf\",\n\t\"mathtt\",\n\t\"mathup\",\n\t\"mbox\",\n\t\"overbrace\",\n\t\"pmb\",\n\t\"smash\",\n\t\"substack\",\n\t\"text\",\n\t\"textbf\",\n\t\"textit\",\n\t\"textmd\",\n\t\"textnormal\",\n\t\"textrm\",\n\t\"textsc\",\n\t\"textsf\",\n\t\"textsl\",\n\t\"texttt\",\n\t\"textup\",\n\t\"underbrace\",\n\t\"bm\",\n\t\"boldsymbol\",\n]);\nconst ACCENTS: Readonly<Record<string, string>> = {\n\tacute: \"\\u0301\",\n\tbar: \"\\u0305\",\n\tbreve: \"\\u0306\",\n\tcheck: \"\\u030c\",\n\tddot: \"\\u0308\",\n\tdot: \"\\u0307\",\n\tgrave: \"\\u0300\",\n\that: \"\\u0302\",\n\tmathring: \"\\u030a\",\n\toverleftarrow: \"\\u20d6\",\n\toverleftrightarrow: \"\\u20e1\",\n\toverline: \"\\u0305\",\n\toverrightarrow: \"\\u20d7\",\n\ttilde: \"\\u0303\",\n\tunderline: \"\\u0332\",\n\tvec: \"\\u20d7\",\n\twidehat: \"\\u0302\",\n\twidetilde: \"\\u0303\",\n};\n\nfunction replaceCharacters(value: string, replacements: Readonly<Record<string, string>>): string | undefined {\n\tlet result = \"\";\n\tfor (const character of value) {\n\t\tconst replacement = replacements[character];\n\t\tif (replacement === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\t\tresult += replacement;\n\t}\n\treturn result;\n}\n\nfunction normalizeScriptValue(value: string): string {\n\treturn value.trim().replace(/\\s*([=+-])\\s*/g, \"$1\");\n}\n\nfunction formatUnicodeScript(value: string, kind: \"sub\" | \"sup\"): string | undefined {\n\treturn replaceCharacters(normalizeScriptValue(value), kind === \"sub\" ? SUBSCRIPTS : SUPERSCRIPTS);\n}\n\nfunction formatScript(value: string, kind: \"sub\" | \"sup\"): string {\n\tvalue = normalizeScriptValue(value);\n\tconst unicode = formatUnicodeScript(value, kind);\n\tif (unicode !== undefined) {\n\t\treturn unicode;\n\t}\n\n\tconst prefix = kind === \"sub\" ? \"_\" : \"^\";\n\tif (Array.from(value).length === 1 || (kind === \"sub\" && /^[A-Za-z]+$/.test(value))) {\n\t\treturn `${prefix}${value}`;\n\t}\n\treturn `${prefix}(${value})`;\n}\n\nfunction formatFraction(numerator: string, denominator: string): string {\n\tnumerator = numerator.trim();\n\tdenominator = denominator.trim();\n\tconst simpleNumerator = /^[\\p{L}\\p{N}.]+$/u.test(numerator);\n\tconst simpleDenominator = /^[\\p{N}.]+$/u.test(denominator) || Array.from(denominator).length === 1;\n\treturn `${simpleNumerator ? numerator : `(${numerator})`}/${simpleDenominator ? denominator : `(${denominator})`}`;\n}\n\nfunction formatRoot(value: string, symbol = \"√\"): string {\n\tvalue = value.trim();\n\treturn /^[\\p{L}\\p{N}.]+$/u.test(value) ? `${symbol}${value}` : `${symbol}(${value})`;\n}\n\nconst NAMED_OPERATOR_START = \"\\u{f0004}\";\nconst NAMED_OPERATOR_END = \"\\u{f0005}\";\nconst NAMED_OPERATOR_LEFT_SPACING_PATTERN = /(?<=[\\p{L}\\p{N})\\]}\\u{f0001}])\\u{f0004}/gu;\nconst NAMED_OPERATOR_RIGHT_SPACING_PATTERN = /\\u{f0005}(?=[\\p{L}\\p{N}√\\u{f0000}])/gu;\n\nfunction normalizeOutput(value: string): string {\n\treturn value\n\t\t.replace(NAMED_OPERATOR_LEFT_SPACING_PATTERN, \" \")\n\t\t.replaceAll(NAMED_OPERATOR_START, \"\")\n\t\t.replace(NAMED_OPERATOR_RIGHT_SPACING_PATTERN, \" \")\n\t\t.replaceAll(NAMED_OPERATOR_END, \"\")\n\t\t.split(\"\\n\")\n\t\t.map((line) => line.replace(/[ \\t]+/g, \" \").trim())\n\t\t.filter((line, index, lines) => line.length > 0 || (index > 0 && index < lines.length - 1))\n\t\t.join(\"\\n\")\n\t\t.trim();\n}\n\ninterface FractionNode {\n\ttype: \"fraction\";\n\tnumerator: string;\n\tdenominator: string;\n}\n\ninterface OperatorNode {\n\ttype: \"operator\";\n\toperator: string;\n\tlower?: string;\n\tupper?: string;\n}\n\ninterface ScriptNode {\n\ttype: \"script\";\n\tlower?: string;\n\tupper?: string;\n}\n\ninterface MatrixNode {\n\ttype: \"matrix\";\n\tlines: string[];\n\tbaseline: number;\n}\n\ntype LayoutNode = FractionNode | OperatorNode | ScriptNode | MatrixNode;\n\ninterface Layout {\n\tlines: string[];\n\twidth: number;\n\tbaseline: number;\n}\n\nconst LAYOUT_MARKER_START = \"\\u{f0000}\";\nconst LAYOUT_MARKER_END = \"\\u{f0001}\";\nconst LAYOUT_MARKER_PATTERN = /\\u{f0000}(\\d+)\\u{f0001}/gu;\nconst TRAILING_LAYOUT_MARKER_PATTERN = /\\u{f0000}(\\d+)\\u{f0001}$/u;\nconst PROTECTED_SPACE = \"\\u{f0002}\";\n\nfunction padLayoutLine(line: string, width: number, centered = false): string {\n\tconst padding = Math.max(0, width - visibleWidth(line));\n\tconst left = centered ? Math.floor(padding / 2) : 0;\n\treturn `${\" \".repeat(left)}${line}${\" \".repeat(padding - left)}`;\n}\n\nfunction joinLayouts(layouts: readonly Layout[]): Layout {\n\tif (layouts.length === 0) {\n\t\treturn { lines: [\"\"], width: 0, baseline: 0 };\n\t}\n\tconst baseline = Math.max(...layouts.map((layout) => layout.baseline));\n\tconst below = Math.max(...layouts.map((layout) => layout.lines.length - layout.baseline - 1));\n\tconst lines: string[] = [];\n\tfor (let row = 0; row <= baseline + below; row++) {\n\t\tlet line = \"\";\n\t\tfor (const layout of layouts) {\n\t\t\tconst sourceRow = row - baseline + layout.baseline;\n\t\t\tline +=\n\t\t\t\tsourceRow >= 0 && sourceRow < layout.lines.length\n\t\t\t\t\t? padLayoutLine(layout.lines[sourceRow] ?? \"\", layout.width)\n\t\t\t\t\t: \" \".repeat(layout.width);\n\t\t}\n\t\tlines.push(line.trimEnd());\n\t}\n\treturn {\n\t\tlines,\n\t\twidth: layouts.reduce((width, layout) => width + layout.width, 0),\n\t\tbaseline,\n\t};\n}\n\nfunction renderLayout(source: string, nodes: readonly LayoutNode[]): Layout {\n\tconst renderedLines: string[] = [];\n\tlet firstBaseline = 0;\n\tfor (const sourceLine of source.split(\"\\n\")) {\n\t\tconst layouts: Layout[] = [];\n\t\tlet position = 0;\n\t\tlet previousNode: LayoutNode | undefined;\n\t\tfor (const match of sourceLine.matchAll(LAYOUT_MARKER_PATTERN)) {\n\t\t\tconst index = match.index;\n\t\t\tconst node = nodes[Number(match[1])];\n\t\t\tif (!node) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (index > position) {\n\t\t\t\tconst sliced = sourceLine.slice(position, index);\n\t\t\t\tconst trimmed = (previousNode ? sliced.trimStart() : sliced).trimEnd();\n\t\t\t\tconst preserveLeadingSpace = previousNode?.type === \"matrix\" && /^\\s/.test(sliced);\n\t\t\t\tconst preserveTrailingSpace = node.type === \"matrix\" && /\\s$/.test(sliced);\n\t\t\t\tconst text = trimmed\n\t\t\t\t\t? `${preserveLeadingSpace ? \" \" : \"\"}${trimmed}${preserveTrailingSpace ? \" \" : \"\"}`\n\t\t\t\t\t: preserveLeadingSpace || preserveTrailingSpace\n\t\t\t\t\t\t? \" \"\n\t\t\t\t\t\t: \"\";\n\t\t\t\tlayouts.push({ lines: [text], width: visibleWidth(text), baseline: 0 });\n\t\t\t}\n\t\t\tif (node.type === \"fraction\") {\n\t\t\t\tconst numerator = renderLayout(node.numerator, nodes);\n\t\t\t\tconst denominator = renderLayout(node.denominator, nodes);\n\t\t\t\tconst contentWidth = Math.max(numerator.width, denominator.width, 1);\n\t\t\t\tconst width = contentWidth + 2;\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines: [\n\t\t\t\t\t\t...numerator.lines.map((line) => padLayoutLine(line, width, true)),\n\t\t\t\t\t\t` ${\"─\".repeat(contentWidth)} `,\n\t\t\t\t\t\t...denominator.lines.map((line) => padLayoutLine(line, width, true)),\n\t\t\t\t\t],\n\t\t\t\t\twidth,\n\t\t\t\t\tbaseline: numerator.lines.length,\n\t\t\t\t});\n\t\t\t} else if (node.type === \"operator\") {\n\t\t\t\tconst contentWidth = Math.max(\n\t\t\t\t\tvisibleWidth(node.operator),\n\t\t\t\t\tnode.lower === undefined ? 0 : visibleWidth(node.lower),\n\t\t\t\t\tnode.upper === undefined ? 0 : visibleWidth(node.upper),\n\t\t\t\t);\n\t\t\t\tconst lines: string[] = [];\n\t\t\t\tif (node.upper !== undefined) {\n\t\t\t\t\tlines.push(`${padLayoutLine(node.upper, contentWidth, true)} `);\n\t\t\t\t}\n\t\t\t\tlines.push(`${padLayoutLine(node.operator, contentWidth, true)} `);\n\t\t\t\tif (node.lower !== undefined) {\n\t\t\t\t\tlines.push(`${padLayoutLine(node.lower, contentWidth, true)} `);\n\t\t\t\t}\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines,\n\t\t\t\t\twidth: contentWidth + 1,\n\t\t\t\t\tbaseline: node.upper === undefined ? 0 : 1,\n\t\t\t\t});\n\t\t\t} else if (node.type === \"script\") {\n\t\t\t\tconst upper = node.upper === undefined ? undefined : renderLayout(node.upper, nodes);\n\t\t\t\tconst lower = node.lower === undefined ? undefined : renderLayout(node.lower, nodes);\n\t\t\t\tconst width = Math.max(upper?.width ?? 0, lower?.width ?? 0);\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines: [\n\t\t\t\t\t\t...(upper?.lines.map((line) => padLayoutLine(line, width)) ?? []),\n\t\t\t\t\t\t\" \".repeat(width),\n\t\t\t\t\t\t...(lower?.lines.map((line) => padLayoutLine(line, width)) ?? []),\n\t\t\t\t\t],\n\t\t\t\t\twidth,\n\t\t\t\t\tbaseline: upper?.lines.length ?? 0,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst width = Math.max(0, ...node.lines.map((line) => visibleWidth(line)));\n\t\t\t\tlayouts.push({\n\t\t\t\t\tlines: node.lines.map((line) => padLayoutLine(line, width)),\n\t\t\t\t\twidth,\n\t\t\t\t\tbaseline: node.baseline,\n\t\t\t\t});\n\t\t\t}\n\t\t\tposition = index + match[0].length;\n\t\t\tpreviousNode = node;\n\t\t}\n\t\tif (position < sourceLine.length) {\n\t\t\tconst sliced = sourceLine.slice(position);\n\t\t\tconst trimmed = previousNode ? sliced.trimStart() : sliced;\n\t\t\tconst text = previousNode?.type === \"matrix\" && /^\\s/.test(sliced) ? ` ${trimmed}` : trimmed;\n\t\t\tlayouts.push({ lines: [text], width: visibleWidth(text), baseline: 0 });\n\t\t}\n\t\tconst lineLayout = joinLayouts(layouts);\n\t\tif (renderedLines.length === 0) {\n\t\t\tfirstBaseline = lineLayout.baseline;\n\t\t}\n\t\trenderedLines.push(...lineLayout.lines);\n\t}\n\treturn {\n\t\tlines: renderedLines,\n\t\twidth: Math.max(0, ...renderedLines.map((line) => visibleWidth(line))),\n\t\tbaseline: firstBaseline,\n\t};\n}\n\nclass LatexParser {\n\tprivate readonly source: string;\n\tprivate readonly layoutNodes: LayoutNode[];\n\tprivate readonly display: boolean;\n\tprivate position = 0;\n\tprivate supported = true;\n\tprivate stackFractions = true;\n\tprivate scriptDepth = 0;\n\n\tconstructor(source: string, layoutNodes: LayoutNode[], display: boolean) {\n\t\tthis.source = source;\n\t\tthis.layoutNodes = layoutNodes;\n\t\tthis.display = display;\n\t}\n\n\trender(): string | undefined {\n\t\tconst rendered = this.parseSequence();\n\t\tif (!this.supported || this.position !== this.source.length) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn normalizeOutput(rendered);\n\t}\n\n\tprivate parseSequence(endCharacter?: string): string {\n\t\tlet result = \"\";\n\t\twhile (this.position < this.source.length) {\n\t\t\tconst character = this.source[this.position];\n\t\t\tif (endCharacter && character === endCharacter) {\n\t\t\t\tthis.position++;\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (character === \"}\") {\n\t\t\t\tthis.supported = false;\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (character === \"{\") {\n\t\t\t\tthis.position++;\n\t\t\t\tresult += this.parseSequence(\"}\");\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"\\\\\") {\n\t\t\t\tconst command = this.parseCommand();\n\t\t\t\tif (command === NEGATIVE_SPACE) {\n\t\t\t\t\tresult = result.trimEnd();\n\t\t\t\t\tif (result.endsWith(NAMED_OPERATOR_END)) {\n\t\t\t\t\t\tresult = result.slice(0, -NAMED_OPERATOR_END.length);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tresult += command;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"^\" || character === \"_\") {\n\t\t\t\tthis.position++;\n\t\t\t\tresult = result.trimEnd();\n\t\t\t\tconst script = this.parseScripts(character);\n\t\t\t\tif (result.endsWith(NAMED_OPERATOR_END)) {\n\t\t\t\t\tresult = `${result.slice(0, -NAMED_OPERATOR_END.length)}${script}${NAMED_OPERATOR_END}`;\n\t\t\t\t} else {\n\t\t\t\t\tresult += script;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (/\\s/.test(character)) {\n\t\t\t\tresult += this.parseWhitespace();\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"=\" || character === \"<\" || character === \">\") {\n\t\t\t\tresult = `${result.trimEnd()} ${character} `;\n\t\t\t\tthis.position++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"&\") {\n\t\t\t\tthis.position++;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \"~\") {\n\t\t\t\tthis.position++;\n\t\t\t\tresult += \" \";\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (character === \".\") {\n\t\t\t\tconst marker = TRAILING_LAYOUT_MARKER_PATTERN.exec(result);\n\t\t\t\tconst node = marker ? this.layoutNodes[Number(marker[1])] : undefined;\n\t\t\t\tif (node?.type === \"matrix\") {\n\t\t\t\t\tconst lastLine = node.lines.length - 1;\n\t\t\t\t\tnode.lines[lastLine] = `${node.lines[lastLine] ?? \"\"}${character}`;\n\t\t\t\t\tthis.position++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tresult += character;\n\t\t\tthis.position++;\n\t\t}\n\n\t\tif (endCharacter) {\n\t\t\tthis.supported = false;\n\t\t}\n\t\treturn result;\n\t}\n\n\tprivate parseScripts(initialMarker: \"^\" | \"_\"): string {\n\t\tconst scripts: { sub?: string; sup?: string } = {};\n\t\tconst order: Array<\"sub\" | \"sup\"> = [];\n\t\tconst parse = (marker: \"^\" | \"_\"): void => {\n\t\t\tconst kind = marker === \"_\" ? \"sub\" : \"sup\";\n\t\t\tthis.scriptDepth++;\n\t\t\ttry {\n\t\t\t\tscripts[kind] = this.parseRequiredArgument(false);\n\t\t\t} finally {\n\t\t\t\tthis.scriptDepth--;\n\t\t\t}\n\t\t\torder.push(kind);\n\t\t};\n\n\t\tparse(initialMarker);\n\t\tlet nextPosition = this.position;\n\t\twhile (nextPosition < this.source.length && /\\s/.test(this.source[nextPosition] ?? \"\")) {\n\t\t\tnextPosition++;\n\t\t}\n\t\tconst nextMarker = this.source[nextPosition];\n\t\tif ((nextMarker === \"^\" || nextMarker === \"_\") && nextMarker !== initialMarker) {\n\t\t\tthis.position = nextPosition + 1;\n\t\t\tparse(nextMarker);\n\t\t}\n\n\t\tconst subUnicode = scripts.sub === undefined ? undefined : formatUnicodeScript(scripts.sub, \"sub\");\n\t\tconst supUnicode = scripts.sup === undefined ? undefined : formatUnicodeScript(scripts.sup, \"sup\");\n\t\tconst canUseLayout = ![scripts.sub, scripts.sup].some(\n\t\t\t(value) =>\n\t\t\t\tvalue !== undefined &&\n\t\t\t\t(value.includes(\"/\") ||\n\t\t\t\t\t(!value.includes(LAYOUT_MARKER_START) && Array.from(value).length > 1 && !/[A-Z*∗]/.test(value))),\n\t\t);\n\t\tconst needsLayout =\n\t\t\tthis.display &&\n\t\t\tcanUseLayout &&\n\t\t\t(this.scriptDepth > 0 ||\n\t\t\t\t(scripts.sub !== undefined && subUnicode === undefined) ||\n\t\t\t\t(scripts.sup !== undefined && supUnicode === undefined));\n\t\tif (!needsLayout) {\n\t\t\treturn order\n\t\t\t\t.map((kind) =>\n\t\t\t\t\tkind === \"sub\"\n\t\t\t\t\t\t? (subUnicode ?? formatScript(scripts.sub ?? \"\", kind))\n\t\t\t\t\t\t: (supUnicode ?? formatScript(scripts.sup ?? \"\", kind)),\n\t\t\t\t)\n\t\t\t\t.join(\"\");\n\t\t}\n\n\t\tconst index =\n\t\t\tthis.layoutNodes.push({\n\t\t\t\ttype: \"script\",\n\t\t\t\tlower: scripts.sub === undefined ? undefined : normalizeOutput(scripts.sub),\n\t\t\t\tupper: scripts.sup === undefined ? undefined : normalizeOutput(scripts.sup),\n\t\t\t}) - 1;\n\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t}\n\n\tprivate parseWhitespace(): string {\n\t\twhile (this.position < this.source.length && /\\s/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\treturn \" \";\n\t}\n\n\tprivate parseCommand(): string {\n\t\tthis.position++;\n\t\tif (this.position >= this.source.length) {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\n\t\tlet command = \"\";\n\t\tconst first = this.source[this.position] ?? \"\";\n\t\tif (first === \"\\n\" || first === \"\\r\") {\n\t\t\tthis.position++;\n\t\t\tif (first === \"\\r\" && this.source[this.position] === \"\\n\") {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\treturn \" \";\n\t\t}\n\t\tif (/[A-Za-z]/.test(first)) {\n\t\t\tconst start = this.position;\n\t\t\twhile (this.position < this.source.length && /[A-Za-z]/.test(this.source[this.position] ?? \"\")) {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\tcommand = this.source.slice(start, this.position);\n\t\t} else {\n\t\t\tcommand = first;\n\t\t\tthis.position++;\n\t\t}\n\n\t\tif (command === \"\\\\\") {\n\t\t\treturn \"\\n\";\n\t\t}\n\t\tif (SPACING_COMMANDS.has(command)) {\n\t\t\treturn \" \";\n\t\t}\n\t\tif (NEGATIVE_SPACING_COMMANDS.has(command)) {\n\t\t\treturn NEGATIVE_SPACE;\n\t\t}\n\t\tif (FONT_SWITCH_COMMANDS.has(command)) {\n\t\t\twhile (this.position < this.source.length && /\\s/.test(this.source[this.position] ?? \"\")) {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\treturn \"\";\n\t\t}\n\t\tif (IGNORED_COMMANDS.has(command)) {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (\n\t\t\tcommand === \"{\" ||\n\t\t\tcommand === \"}\" ||\n\t\t\tcommand === \"$\" ||\n\t\t\tcommand === \"%\" ||\n\t\t\tcommand === \"#\" ||\n\t\t\tcommand === \"_\" ||\n\t\t\tcommand === \"&\"\n\t\t) {\n\t\t\treturn command;\n\t\t}\n\t\tif (command === \"|\") {\n\t\t\treturn \"‖\";\n\t\t}\n\t\tif (command === \"not\") {\n\t\t\tconst value = this.parseRequiredArgument(false).trim();\n\t\t\tconst negated = NEGATED_SYMBOLS[value];\n\t\t\tif (negated !== undefined) {\n\t\t\t\treturn ` ${negated} `;\n\t\t\t}\n\t\t\tconst characters = Array.from(value);\n\t\t\tif (characters.length === 0) {\n\t\t\t\tthis.supported = false;\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\treturn ` ${characters[0]}\\u0338${characters.slice(1).join(\"\")} `;\n\t\t}\n\t\tif (LIMIT_OPERATORS.has(command)) {\n\t\t\treturn this.parseOperator(command, \"bracket\", true, true);\n\t\t}\n\n\t\tconst symbol = SYMBOLS[command];\n\t\tif (symbol !== undefined) {\n\t\t\tif (DISPLAY_LIMIT_SYMBOLS.has(command)) {\n\t\t\t\treturn this.parseOperator(symbol, \"script\", true);\n\t\t\t}\n\t\t\treturn command === \"cdot\" || command === \"times\" || RELATION_COMMANDS.has(command) ? ` ${symbol} ` : symbol;\n\t\t}\n\t\tif (NAMED_OPERATORS.has(command)) {\n\t\t\treturn `${NAMED_OPERATOR_START}${command}${NAMED_OPERATOR_END}`;\n\t\t}\n\t\tif (SIZE_COMMANDS.has(command)) {\n\t\t\treturn \"\";\n\t\t}\n\t\tif (command === \"left\" || command === \"middle\" || command === \"right\") {\n\t\t\tif (this.source[this.position] === \".\") {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\treturn \"\";\n\t\t}\n\t\tif (command === \"frac\" || command === \"dfrac\" || command === \"tfrac\") {\n\t\t\tconst shouldStack = this.display && this.stackFractions && command !== \"tfrac\";\n\t\t\tconst numerator = this.parseRequiredArgument(!shouldStack);\n\t\t\tconst denominator = this.parseRequiredArgument(!shouldStack);\n\t\t\tif (shouldStack) {\n\t\t\t\tconst index =\n\t\t\t\t\tthis.layoutNodes.push({\n\t\t\t\t\t\ttype: \"fraction\",\n\t\t\t\t\t\tnumerator: normalizeOutput(numerator),\n\t\t\t\t\t\tdenominator: normalizeOutput(denominator),\n\t\t\t\t\t}) - 1;\n\t\t\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t\t\t}\n\t\t\treturn formatFraction(numerator, denominator);\n\t\t}\n\t\tif (command === \"sqrt\") {\n\t\t\tconst degree = this.parseOptionalArgument()?.trim();\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\tif (degree === undefined || degree === \"2\") {\n\t\t\t\treturn formatRoot(value);\n\t\t\t}\n\t\t\tif (degree === \"3\") {\n\t\t\t\treturn formatRoot(value, \"∛\");\n\t\t\t}\n\t\t\tif (degree === \"4\") {\n\t\t\t\treturn formatRoot(value, \"∜\");\n\t\t\t}\n\t\t\treturn `${formatScript(degree, \"sup\")}${formatRoot(value)}`;\n\t\t}\n\t\tif (command === \"boxed\" || command === \"fbox\") {\n\t\t\treturn `[${this.parseRequiredArgument().trim()}]`;\n\t\t}\n\t\tif (command === \"binom\" || command === \"dbinom\" || command === \"tbinom\") {\n\t\t\treturn `(${this.parseRequiredArgument()} choose ${this.parseRequiredArgument()})`;\n\t\t}\n\t\tconst accent = ACCENTS[command];\n\t\tif (accent !== undefined) {\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\treturn Array.from(value).length === 1 ? `${value}${accent}` : `${command}(${value})`;\n\t\t}\n\t\tif (command === \"mathbb\") {\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\treturn Array.from(value, (character) => BLACKBOARD[character] ?? character).join(\"\");\n\t\t}\n\t\tif (command === \"operatorname\") {\n\t\t\tconst starred = this.source[this.position] === \"*\";\n\t\t\tif (starred) {\n\t\t\t\tthis.position++;\n\t\t\t}\n\t\t\tconst operator = normalizeOutput(this.parseRequiredArgument()).trim();\n\t\t\treturn this.parseOperator(operator, \"bracket\", starred, true);\n\t\t}\n\t\tif (command === \"mod\" || command === \"bmod\") {\n\t\t\treturn \" mod \";\n\t\t}\n\t\tif (command === \"pmod\" || command === \"pod\") {\n\t\t\tconst value = this.parseRequiredArgument().trim();\n\t\t\treturn command === \"pmod\" ? ` (mod ${value})` : ` (${value})`;\n\t\t}\n\t\tif (command === \"overset\" || command === \"stackrel\") {\n\t\t\tconst upper = this.parseRequiredArgument();\n\t\t\tconst value = this.parseRequiredArgument().trim();\n\t\t\treturn `${value}${formatScript(upper, \"sup\")}`;\n\t\t}\n\t\tif (command === \"underset\") {\n\t\t\tconst lower = this.parseRequiredArgument();\n\t\t\tconst value = this.parseRequiredArgument().trim();\n\t\t\treturn `${value}${formatScript(lower, \"sub\")}`;\n\t\t}\n\t\tif (PLAIN_WRAPPERS.has(command)) {\n\t\t\tconst value = this.parseRequiredArgument();\n\t\t\treturn command.startsWith(\"text\") || command === \"mbox\" ? value : value.trim();\n\t\t}\n\t\tif (command === \"begin\") {\n\t\t\treturn this.parseEnvironment();\n\t\t}\n\t\tif (command === \"end\") {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\n\t\tthis.supported = false;\n\t\treturn `\\\\${command}`;\n\t}\n\n\tprivate parseOperator(\n\t\toperator: string,\n\t\tinlineLowerStyle: \"bracket\" | \"script\",\n\t\tdisplayLimits: boolean,\n\t\tspaced = false,\n\t): string {\n\t\tlet useDisplayLimits = displayLimits;\n\t\tlet modifierPosition = this.position;\n\t\twhile (modifierPosition < this.source.length && /[ \\t]/.test(this.source[modifierPosition] ?? \"\")) {\n\t\t\tmodifierPosition++;\n\t\t}\n\t\tconst modifier = /^\\\\(limits|nolimits)(?![A-Za-z])/.exec(this.source.slice(modifierPosition));\n\t\tif (modifier) {\n\t\t\tuseDisplayLimits = modifier[1] === \"limits\";\n\t\t\tthis.position = modifierPosition + modifier[0].length;\n\t\t}\n\n\t\tlet lower: string | undefined;\n\t\tlet upper: string | undefined;\n\t\twhile (true) {\n\t\t\tlet scriptPosition = this.position;\n\t\t\twhile (scriptPosition < this.source.length && /[ \\t]/.test(this.source[scriptPosition] ?? \"\")) {\n\t\t\t\tscriptPosition++;\n\t\t\t}\n\t\t\tconst kind = this.source[scriptPosition];\n\t\t\tif (kind !== \"_\" && kind !== \"^\") {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tthis.position = scriptPosition + 1;\n\t\t\tconst value = normalizeOutput(this.parseRequiredArgument(false)).replaceAll(\" \", \"\");\n\t\t\tif (kind === \"_\") {\n\t\t\t\tif (lower !== undefined) {\n\t\t\t\t\tthis.supported = false;\n\t\t\t\t}\n\t\t\t\tlower = value;\n\t\t\t} else {\n\t\t\t\tif (upper !== undefined) {\n\t\t\t\t\tthis.supported = false;\n\t\t\t\t}\n\t\t\t\tupper = value;\n\t\t\t}\n\t\t}\n\n\t\tif (this.display && useDisplayLimits && (lower !== undefined || upper !== undefined)) {\n\t\t\tconst index = this.layoutNodes.push({ type: \"operator\", operator, lower, upper }) - 1;\n\t\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t\t}\n\n\t\tlet rendered = operator;\n\t\tif (lower !== undefined) {\n\t\t\trendered += inlineLowerStyle === \"bracket\" ? `[${lower}]` : formatScript(lower, \"sub\");\n\t\t}\n\t\tif (upper !== undefined) {\n\t\t\trendered += formatScript(upper, \"sup\");\n\t\t}\n\t\treturn spaced ? ` ${rendered} ` : rendered;\n\t}\n\n\tprivate parseRequiredArgument(stackFractions = true): string {\n\t\tconst previousStackFractions = this.stackFractions;\n\t\tthis.stackFractions = previousStackFractions && stackFractions;\n\t\tconst value = this.parseRequiredArgumentValue();\n\t\tthis.stackFractions = previousStackFractions;\n\t\treturn value;\n\t}\n\n\tprivate parseRequiredArgumentValue(): string {\n\t\twhile (this.position < this.source.length && /\\s/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\tif (this.position >= this.source.length) {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\t\tif (this.source[this.position] === \"{\") {\n\t\t\tthis.position++;\n\t\t\treturn this.parseSequence(\"}\");\n\t\t}\n\t\tif (this.source[this.position] === \"\\\\\") {\n\t\t\treturn this.parseCommand();\n\t\t}\n\t\tconst value = this.source[this.position] ?? \"\";\n\t\tthis.position++;\n\t\treturn value;\n\t}\n\n\tprivate parseOptionalArgument(): string | undefined {\n\t\twhile (this.position < this.source.length && /[ \\t]/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\tif (this.source[this.position] !== \"[\") {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst end = this.source.indexOf(\"]\", this.position + 1);\n\t\tif (end < 0) {\n\t\t\tthis.supported = false;\n\t\t\treturn undefined;\n\t\t}\n\t\tconst value = this.source.slice(this.position + 1, end);\n\t\tthis.position = end + 1;\n\t\treturn this.renderNested(value);\n\t}\n\n\tprivate readRawGroup(): string | undefined {\n\t\twhile (this.position < this.source.length && /[ \\t]/.test(this.source[this.position] ?? \"\")) {\n\t\t\tthis.position++;\n\t\t}\n\t\tif (this.source[this.position] !== \"{\") {\n\t\t\tthis.supported = false;\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst start = ++this.position;\n\t\tlet depth = 1;\n\t\twhile (this.position < this.source.length) {\n\t\t\tconst character = this.source[this.position];\n\t\t\tif (character === \"\\\\\") {\n\t\t\t\tthis.position += 2;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (character === \"{\") depth++;\n\t\t\tif (character === \"}\") depth--;\n\t\t\tif (depth === 0) {\n\t\t\t\tconst value = this.source.slice(start, this.position);\n\t\t\t\tthis.position++;\n\t\t\t\treturn value;\n\t\t\t}\n\t\t\tthis.position++;\n\t\t}\n\t\tthis.supported = false;\n\t\treturn undefined;\n\t}\n\n\tprivate splitEnvironmentRows(body: string): string[] {\n\t\treturn body.split(/\\\\\\\\(?:\\[[^\\]\\n]*\\])?/);\n\t}\n\n\tprivate parseEnvironment(): string {\n\t\tconst environment = this.readRawGroup();\n\t\tif (!environment) {\n\t\t\treturn \"\";\n\t\t}\n\t\tconst endMarker = `\\\\end{${environment}}`;\n\t\tconst end = this.source.indexOf(endMarker, this.position);\n\t\tif (end < 0) {\n\t\t\tthis.supported = false;\n\t\t\treturn \"\";\n\t\t}\n\t\tconst body = this.source.slice(this.position, end);\n\t\tthis.position = end + endMarker.length;\n\n\t\tif (environment === \"equation\" || environment === \"equation*\" || environment === \"displaymath\") {\n\t\t\treturn this.renderNested(body).trim();\n\t\t}\n\n\t\tif (\n\t\t\tenvironment === \"aligned\" ||\n\t\t\tenvironment === \"align\" ||\n\t\t\tenvironment === \"align*\" ||\n\t\t\tenvironment === \"alignedat\" ||\n\t\t\tenvironment === \"alignat\" ||\n\t\t\tenvironment === \"alignat*\" ||\n\t\t\tenvironment === \"gather\" ||\n\t\t\tenvironment === \"gathered\" ||\n\t\t\tenvironment === \"multline\" ||\n\t\t\tenvironment === \"multline*\" ||\n\t\t\tenvironment === \"split\"\n\t\t) {\n\t\t\tconst alignedAt = [\"alignedat\", \"alignat\", \"alignat*\"].includes(environment);\n\t\t\tconst alignedBody = alignedAt ? body.replace(/^\\s*\\{[^}]*\\}/, \"\") : body;\n\t\t\treturn this.splitEnvironmentRows(alignedBody)\n\t\t\t\t.map((row) => {\n\t\t\t\t\tconst cells = row.split(\"&\");\n\t\t\t\t\tconst source = alignedAt\n\t\t\t\t\t\t? Array.from({ length: Math.ceil(cells.length / 2) }, (_, index) =>\n\t\t\t\t\t\t\t\tcells.slice(index * 2, index * 2 + 2).join(\"\"),\n\t\t\t\t\t\t\t).join(\" \")\n\t\t\t\t\t\t: cells.join(\"\");\n\t\t\t\t\treturn this.renderNested(source).trim();\n\t\t\t\t})\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(\"\\n\");\n\t\t}\n\n\t\tif (environment === \"cases\" || environment === \"cases*\") {\n\t\t\treturn this.renderCases(body);\n\t\t}\n\n\t\tif (\n\t\t\t[\"array\", \"matrix\", \"smallmatrix\", \"pmatrix\", \"bmatrix\", \"Bmatrix\", \"vmatrix\", \"Vmatrix\"].includes(environment)\n\t\t) {\n\t\t\tconst matrixBody = environment === \"array\" ? body.replace(/^\\s*\\{[^}]*\\}/, \"\") : body;\n\t\t\treturn this.renderMatrix(environment, matrixBody);\n\t\t}\n\n\t\tthis.supported = false;\n\t\treturn body;\n\t}\n\n\tprivate renderCases(body: string): string {\n\t\tconst rows = this.splitEnvironmentRows(body)\n\t\t\t.map((row) => row.split(\"&\").map((cell) => this.renderNested(cell, false).trim()))\n\t\t\t.filter((row) => row.some(Boolean));\n\t\tconst valueWidth = Math.max(0, ...rows.map((row) => visibleWidth((row[0] ?? \"\").replace(/,\\s*$/, \"\"))));\n\t\tconst contents = rows.map((row) => {\n\t\t\tconst value = (row[0] ?? \"\").replace(/,\\s*$/, \"\");\n\t\t\tconst condition = row[1] ?? \"\";\n\t\t\tif (!condition) {\n\t\t\t\treturn value;\n\t\t\t}\n\t\t\tconst conditionPrefix = /^(?:if|when|for|otherwise)\\b/i.test(condition) ? \" \" : \" if \";\n\t\t\treturn `${value}${PROTECTED_SPACE.repeat(valueWidth - visibleWidth(value))}${conditionPrefix}${condition}`;\n\t\t});\n\t\tif (contents.length <= 1) {\n\t\t\treturn contents.length === 0 ? \"\" : `⎧ ${contents[0]}`;\n\t\t}\n\n\t\tconst middle = Math.floor(contents.length / 2);\n\t\tconst visualRows: Array<string | undefined> = [...contents];\n\t\tif (contents.length % 2 === 0) {\n\t\t\tvisualRows.splice(middle, 0, undefined);\n\t\t}\n\t\tconst lines = visualRows.map((content, index) => {\n\t\t\tconst delimiter = index === 0 ? \"⎧\" : index === visualRows.length - 1 ? \"⎩\" : \"⎨\";\n\t\t\treturn content === undefined ? delimiter : `${delimiter} ${content}`;\n\t\t});\n\t\tconst index = this.layoutNodes.push({ type: \"matrix\", lines, baseline: middle }) - 1;\n\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t}\n\n\tprivate renderMatrix(environment: string, body: string): string {\n\t\tconst matrix = this.splitEnvironmentRows(body)\n\t\t\t.map((row) => row.split(\"&\").map((cell) => this.renderNested(cell, false).trim()))\n\t\t\t.filter((row) => row.some(Boolean));\n\t\tconst columnCount = Math.max(0, ...matrix.map((row) => row.length));\n\t\tconst columnWidths = Array.from({ length: columnCount }, (_, column) =>\n\t\t\tMath.max(0, ...matrix.map((row) => visibleWidth(row[column] ?? \"\"))),\n\t\t);\n\t\tconst rows = matrix.map((row) =>\n\t\t\tArray.from({ length: columnCount }, (_, column) => {\n\t\t\t\tconst cell = row[column] ?? \"\";\n\t\t\t\treturn `${cell}${PROTECTED_SPACE.repeat(Math.max(0, (columnWidths[column] ?? 0) - visibleWidth(cell)))}`;\n\t\t\t}).join(\" │ \"),\n\t\t);\n\n\t\tlet lines: string[];\n\t\tif (environment === \"array\" || environment === \"matrix\" || environment === \"smallmatrix\") {\n\t\t\tlines = rows;\n\t\t} else {\n\t\t\tconst delimiters: Readonly<Record<string, readonly [string, string, string, string, string, string]>> = {\n\t\t\t\tpmatrix: [\"⎛\", \"⎞\", \"⎜\", \"⎟\", \"⎝\", \"⎠\"],\n\t\t\t\tbmatrix: [\"⎡\", \"⎤\", \"⎢\", \"⎥\", \"⎣\", \"⎦\"],\n\t\t\t\tBmatrix: [\"⎧\", \"⎫\", \"⎨\", \"⎬\", \"⎩\", \"⎭\"],\n\t\t\t\tvmatrix: [\"│\", \"│\", \"│\", \"│\", \"│\", \"│\"],\n\t\t\t\tVmatrix: [\"║\", \"║\", \"║\", \"║\", \"║\", \"║\"],\n\t\t\t};\n\t\t\tconst delimiter = delimiters[environment];\n\t\t\tif (!delimiter) {\n\t\t\t\tthis.supported = false;\n\t\t\t\treturn rows.join(\"\\n\");\n\t\t\t}\n\t\t\tlines = rows.map((row, index) => {\n\t\t\t\tconst left = index === 0 ? delimiter[0] : index === rows.length - 1 ? delimiter[4] : delimiter[2];\n\t\t\t\tconst right = index === 0 ? delimiter[1] : index === rows.length - 1 ? delimiter[5] : delimiter[3];\n\t\t\t\treturn `${left} ${row} ${right}`;\n\t\t\t});\n\t\t}\n\n\t\tif (lines.length <= 1) {\n\t\t\treturn lines[0] ?? \"\";\n\t\t}\n\t\tconst index = this.layoutNodes.push({ type: \"matrix\", lines, baseline: 0 }) - 1;\n\t\treturn `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;\n\t}\n\n\tprivate renderNested(source: string, stackFractions = true): string {\n\t\tconst rendered = new LatexParser(source, this.layoutNodes, this.display && stackFractions).render();\n\t\tif (rendered === undefined) {\n\t\t\tthis.supported = false;\n\t\t\treturn source;\n\t\t}\n\t\treturn rendered;\n\t}\n}\n\nexport interface RenderLatexOptions {\n\t/** Stack fractions and operator limits vertically for display math (default: false). */\n\tdisplay?: boolean;\n}\n\n/**\n * Render a basic LaTeX math expression as terminal-friendly Unicode text.\n * Returns undefined when the expression contains unsupported or malformed syntax.\n */\nexport function renderLatex(source: string, options: RenderLatexOptions = {}): string | undefined {\n\tconst layoutNodes: LayoutNode[] = [];\n\tconst rendered = new LatexParser(source, layoutNodes, options.display === true).render();\n\tif (rendered === undefined) {\n\t\treturn undefined;\n\t}\n\tif (layoutNodes.length === 0) {\n\t\treturn rendered.replaceAll(PROTECTED_SPACE, \" \");\n\t}\n\tconst lines = renderLayout(rendered, layoutNodes).lines;\n\tconst indentation = Math.min(\n\t\t...lines.filter((line) => line.trim()).map((line) => line.length - line.trimStart().length),\n\t);\n\treturn lines\n\t\t.map((line) => line.slice(indentation).trimEnd())\n\t\t.join(\"\\n\")\n\t\t.trimEnd()\n\t\t.replaceAll(PROTECTED_SPACE, \" \");\n}\n"]}
package/dist/latex.js CHANGED
@@ -513,6 +513,7 @@ const SPACING_COMMANDS = new Set([
513
513
  ]);
514
514
  const NEGATIVE_SPACING_COMMANDS = new Set(["!", "negmedspace", "negthickspace", "negthinspace"]);
515
515
  const NEGATIVE_SPACE = "\u0000";
516
+ const FONT_SWITCH_COMMANDS = new Set(["bf", "cal", "it", "rm", "sf", "sl", "tt"]);
516
517
  const IGNORED_COMMANDS = new Set([
517
518
  "displaystyle",
518
519
  "limits",
@@ -598,10 +599,15 @@ function replaceCharacters(value, replacements) {
598
599
  }
599
600
  return result;
600
601
  }
602
+ function normalizeScriptValue(value) {
603
+ return value.trim().replace(/\s*([=+-])\s*/g, "$1");
604
+ }
605
+ function formatUnicodeScript(value, kind) {
606
+ return replaceCharacters(normalizeScriptValue(value), kind === "sub" ? SUBSCRIPTS : SUPERSCRIPTS);
607
+ }
601
608
  function formatScript(value, kind) {
602
- value = value.trim();
603
- const replacements = kind === "sub" ? SUBSCRIPTS : SUPERSCRIPTS;
604
- const unicode = replaceCharacters(value.replace(/\s*([=+-])\s*/g, "$1"), replacements);
609
+ value = normalizeScriptValue(value);
610
+ const unicode = formatUnicodeScript(value, kind);
605
611
  if (unicode !== undefined) {
606
612
  return unicode;
607
613
  }
@@ -728,6 +734,20 @@ function renderLayout(source, nodes) {
728
734
  baseline: node.upper === undefined ? 0 : 1,
729
735
  });
730
736
  }
737
+ else if (node.type === "script") {
738
+ const upper = node.upper === undefined ? undefined : renderLayout(node.upper, nodes);
739
+ const lower = node.lower === undefined ? undefined : renderLayout(node.lower, nodes);
740
+ const width = Math.max(upper?.width ?? 0, lower?.width ?? 0);
741
+ layouts.push({
742
+ lines: [
743
+ ...(upper?.lines.map((line) => padLayoutLine(line, width)) ?? []),
744
+ " ".repeat(width),
745
+ ...(lower?.lines.map((line) => padLayoutLine(line, width)) ?? []),
746
+ ],
747
+ width,
748
+ baseline: upper?.lines.length ?? 0,
749
+ });
750
+ }
731
751
  else {
732
752
  const width = Math.max(0, ...node.lines.map((line) => visibleWidth(line)));
733
753
  layouts.push({
@@ -764,6 +784,7 @@ class LatexParser {
764
784
  position = 0;
765
785
  supported = true;
766
786
  stackFractions = true;
787
+ scriptDepth = 0;
767
788
  constructor(source, layoutNodes, display) {
768
789
  this.source = source;
769
790
  this.layoutNodes = layoutNodes;
@@ -809,7 +830,7 @@ class LatexParser {
809
830
  if (character === "^" || character === "_") {
810
831
  this.position++;
811
832
  result = result.trimEnd();
812
- const script = formatScript(this.parseRequiredArgument(false), character === "_" ? "sub" : "sup");
833
+ const script = this.parseScripts(character);
813
834
  if (result.endsWith(NAMED_OPERATOR_END)) {
814
835
  result = `${result.slice(0, -NAMED_OPERATOR_END.length)}${script}${NAMED_OPERATOR_END}`;
815
836
  }
@@ -854,6 +875,54 @@ class LatexParser {
854
875
  }
855
876
  return result;
856
877
  }
878
+ parseScripts(initialMarker) {
879
+ const scripts = {};
880
+ const order = [];
881
+ const parse = (marker) => {
882
+ const kind = marker === "_" ? "sub" : "sup";
883
+ this.scriptDepth++;
884
+ try {
885
+ scripts[kind] = this.parseRequiredArgument(false);
886
+ }
887
+ finally {
888
+ this.scriptDepth--;
889
+ }
890
+ order.push(kind);
891
+ };
892
+ parse(initialMarker);
893
+ let nextPosition = this.position;
894
+ while (nextPosition < this.source.length && /\s/.test(this.source[nextPosition] ?? "")) {
895
+ nextPosition++;
896
+ }
897
+ const nextMarker = this.source[nextPosition];
898
+ if ((nextMarker === "^" || nextMarker === "_") && nextMarker !== initialMarker) {
899
+ this.position = nextPosition + 1;
900
+ parse(nextMarker);
901
+ }
902
+ const subUnicode = scripts.sub === undefined ? undefined : formatUnicodeScript(scripts.sub, "sub");
903
+ const supUnicode = scripts.sup === undefined ? undefined : formatUnicodeScript(scripts.sup, "sup");
904
+ const canUseLayout = ![scripts.sub, scripts.sup].some((value) => value !== undefined &&
905
+ (value.includes("/") ||
906
+ (!value.includes(LAYOUT_MARKER_START) && Array.from(value).length > 1 && !/[A-Z*∗]/.test(value))));
907
+ const needsLayout = this.display &&
908
+ canUseLayout &&
909
+ (this.scriptDepth > 0 ||
910
+ (scripts.sub !== undefined && subUnicode === undefined) ||
911
+ (scripts.sup !== undefined && supUnicode === undefined));
912
+ if (!needsLayout) {
913
+ return order
914
+ .map((kind) => kind === "sub"
915
+ ? (subUnicode ?? formatScript(scripts.sub ?? "", kind))
916
+ : (supUnicode ?? formatScript(scripts.sup ?? "", kind)))
917
+ .join("");
918
+ }
919
+ const index = this.layoutNodes.push({
920
+ type: "script",
921
+ lower: scripts.sub === undefined ? undefined : normalizeOutput(scripts.sub),
922
+ upper: scripts.sup === undefined ? undefined : normalizeOutput(scripts.sup),
923
+ }) - 1;
924
+ return `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;
925
+ }
857
926
  parseWhitespace() {
858
927
  while (this.position < this.source.length && /\s/.test(this.source[this.position] ?? "")) {
859
928
  this.position++;
@@ -895,6 +964,12 @@ class LatexParser {
895
964
  if (NEGATIVE_SPACING_COMMANDS.has(command)) {
896
965
  return NEGATIVE_SPACE;
897
966
  }
967
+ if (FONT_SWITCH_COMMANDS.has(command)) {
968
+ while (this.position < this.source.length && /\s/.test(this.source[this.position] ?? "")) {
969
+ this.position++;
970
+ }
971
+ return "";
972
+ }
898
973
  if (IGNORED_COMMANDS.has(command)) {
899
974
  return "";
900
975
  }
@@ -1193,18 +1268,7 @@ class LatexParser {
1193
1268
  .join("\n");
1194
1269
  }
1195
1270
  if (environment === "cases" || environment === "cases*") {
1196
- const rows = this.splitEnvironmentRows(body)
1197
- .map((row) => row.split("&").map((cell) => this.renderNested(cell, false).trim()))
1198
- .filter((row) => row.some(Boolean));
1199
- return rows
1200
- .map((row, index) => {
1201
- const value = (row[0] ?? "").replace(/,\s*$/, "");
1202
- const condition = row[1] ?? "";
1203
- const delimiter = index === 0 ? "⎧" : index === rows.length - 1 ? "⎩" : "⎨";
1204
- const conditionPrefix = /^(?:if|when|for|otherwise)\b/i.test(condition) ? " " : " if ";
1205
- return `${delimiter} ${value}${condition ? `${conditionPrefix}${condition}` : ""}`;
1206
- })
1207
- .join("\n");
1271
+ return this.renderCases(body);
1208
1272
  }
1209
1273
  if (["array", "matrix", "smallmatrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix"].includes(environment)) {
1210
1274
  const matrixBody = environment === "array" ? body.replace(/^\s*\{[^}]*\}/, "") : body;
@@ -1213,6 +1277,35 @@ class LatexParser {
1213
1277
  this.supported = false;
1214
1278
  return body;
1215
1279
  }
1280
+ renderCases(body) {
1281
+ const rows = this.splitEnvironmentRows(body)
1282
+ .map((row) => row.split("&").map((cell) => this.renderNested(cell, false).trim()))
1283
+ .filter((row) => row.some(Boolean));
1284
+ const valueWidth = Math.max(0, ...rows.map((row) => visibleWidth((row[0] ?? "").replace(/,\s*$/, ""))));
1285
+ const contents = rows.map((row) => {
1286
+ const value = (row[0] ?? "").replace(/,\s*$/, "");
1287
+ const condition = row[1] ?? "";
1288
+ if (!condition) {
1289
+ return value;
1290
+ }
1291
+ const conditionPrefix = /^(?:if|when|for|otherwise)\b/i.test(condition) ? " " : " if ";
1292
+ return `${value}${PROTECTED_SPACE.repeat(valueWidth - visibleWidth(value))}${conditionPrefix}${condition}`;
1293
+ });
1294
+ if (contents.length <= 1) {
1295
+ return contents.length === 0 ? "" : `⎧ ${contents[0]}`;
1296
+ }
1297
+ const middle = Math.floor(contents.length / 2);
1298
+ const visualRows = [...contents];
1299
+ if (contents.length % 2 === 0) {
1300
+ visualRows.splice(middle, 0, undefined);
1301
+ }
1302
+ const lines = visualRows.map((content, index) => {
1303
+ const delimiter = index === 0 ? "⎧" : index === visualRows.length - 1 ? "⎩" : "⎨";
1304
+ return content === undefined ? delimiter : `${delimiter} ${content}`;
1305
+ });
1306
+ const index = this.layoutNodes.push({ type: "matrix", lines, baseline: middle }) - 1;
1307
+ return `${LAYOUT_MARKER_START}${index}${LAYOUT_MARKER_END}`;
1308
+ }
1216
1309
  renderMatrix(environment, body) {
1217
1310
  const matrix = this.splitEnvironmentRows(body)
1218
1311
  .map((row) => row.split("&").map((cell) => this.renderNested(cell, false).trim()))