markdown.mdx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ---
  2. title: Markdown
  3. description: Render markdown with syntax-aware styling
  4. order: 11
  5. ---
  6. # Markdown
  7. Render markdown content with syntax-aware styling and optional Tree-sitter highlighting for code blocks.
  8. ## Basic usage
  9. ### Renderable API
  10. ````typescript
  11. import { MarkdownRenderable, SyntaxStyle, RGBA, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const syntaxStyle = SyntaxStyle.fromStyles({
  14. "markup.heading.1": { fg: RGBA.fromHex("#58A6FF"), bold: true },
  15. "markup.list": { fg: RGBA.fromHex("#FF7B72") },
  16. "markup.raw": { fg: RGBA.fromHex("#A5D6FF") },
  17. default: { fg: RGBA.fromHex("#E6EDF3") },
  18. })
  19. const markdown = new MarkdownRenderable(renderer, {
  20. id: "readme",
  21. width: 60,
  22. content: "# Hello\n\n- One\n- Two\n\n```ts\nconst x = 1\n```",
  23. syntaxStyle,
  24. })
  25. renderer.root.add(markdown)
  26. ````
  27. ## Fenced language normalization
  28. Code fence info strings are normalized before Tree-sitter highlighting.
  29. - `tsx` -> `typescriptreact`
  30. - `.jsx` -> `javascriptreact`
  31. - `TSX title=Button.tsx` -> `typescriptreact`
  32. - `Dockerfile` -> `dockerfile`
  33. Normalization uses `infoStringToFiletype()`. You can extend or override mappings at runtime:
  34. ```typescript
  35. import { extensionToFiletype, basenameToFiletype } from "@opentui/core"
  36. extensionToFiletype.set("templ", "html")
  37. basenameToFiletype.set("mytoolrc", "yaml")
  38. ```
  39. ## Concealment
  40. Hide markdown markers (backticks, emphasis markers, etc.) when `conceal` is true:
  41. ```typescript
  42. const markdown = new MarkdownRenderable(renderer, {
  43. content: "**bold** and `code`",
  44. syntaxStyle,
  45. conceal: true,
  46. })
  47. ```
  48. Use `concealCode` to control concealment inside fenced code blocks independently (`false` by default).
  49. ## Streaming updates
  50. Enable streaming mode for incremental updates. Keep it `true` while appending chunks, then set `markdown.streaming = false` when complete to finalize trailing block parsing. Tables include trailing partial rows, with missing cells rendered empty.
  51. ```typescript
  52. const markdown = new MarkdownRenderable(renderer, {
  53. content: "",
  54. syntaxStyle,
  55. streaming: true,
  56. })
  57. markdown.content += "# Live log\n"
  58. markdown.content += "- line 1\n"
  59. markdown.streaming = false
  60. ```
  61. ### Stable block prefix
  62. `parseMarkdownIncremental` reports how many parsed tokens at the head of the stream are stable — that is, unlikely to change if you append more content. The renderable surfaces this as a count of **top-level render blocks** you can safely commit elsewhere while the unstable tail keeps updating.
  63. To use it, render with `internalBlockMode: "top-level"`. The renderable keeps each top-level markdown block (heading, paragraph, list, table, fenced code) as its own child renderable, and exposes `markdown._stableBlockCount` — the number of blocks at the head of the tree that are stable right now. This matches the shape [`ScrollbackSurface`](/docs/core-concepts/renderer#writing-to-scrollback) expects when you commit streamed output row-by-row.
  64. ```typescript
  65. import { MarkdownRenderable, RGBA, SyntaxStyle } from "@opentui/core"
  66. const syntaxStyle = SyntaxStyle.fromStyles({
  67. default: { fg: RGBA.fromHex("#E6EDF3") },
  68. })
  69. const md = new MarkdownRenderable(renderer, {
  70. content: "",
  71. syntaxStyle,
  72. streaming: true,
  73. internalBlockMode: "top-level",
  74. })
  75. md.content = "# Title\n\nPara 1"
  76. // md._stableBlockCount might be 1 here — "# Title" is settled, "Para 1" could still grow
  77. md.content = "# Title\n\nPara 1\n\nPara 2"
  78. // md._stableBlockCount rises as earlier blocks are sealed by a blank line
  79. ```
  80. `internalBlockMode` is an experimental, opt-in flag that powers the built-in scrollback streaming demo. For non-streaming rendering, leave it at the default (`"coalesced"`), which folds sibling blocks together and matches the historical layout.
  81. ## Markdown tables
  82. Markdown tables support `tableOptions` for style, sizing, wrapping, borders, and selection.
  83. ```typescript
  84. const markdown = new MarkdownRenderable(renderer, {
  85. content: "| Service | Status |\n| --- | --- |\n| api | ok |",
  86. syntaxStyle,
  87. tableOptions: {
  88. style: "grid",
  89. widthMode: "full",
  90. columnFitter: "balanced",
  91. wrapMode: "word",
  92. cellPadding: 1,
  93. borders: true,
  94. outerBorder: true,
  95. borderStyle: "rounded",
  96. borderColor: "#6b7280",
  97. selectable: true,
  98. },
  99. })
  100. ```
  101. ### `tableOptions`
  102. | Option | Type | Default | Description |
  103. | -------------- | ------------------------------ | ----------------------- | ------------------------------------------------- |
  104. | `style` | `"grid" \| "columns"` | depends on block mode | Visual preset (see [Table styles](#table-styles)) |
  105. | `widthMode` | `"content" \| "full"` | depends on style | `"full"` expands columns to fill available width |
  106. | `columnFitter` | `"proportional" \| "balanced"` | `"proportional"` | How columns shrink when space is constrained |
  107. | `wrapMode` | `"none" \| "char" \| "word"` | `"word"` | Wrapping mode inside each table cell |
  108. | `cellPadding` | `number` | `0` | Padding on all sides of each cell |
  109. | `borders` | `boolean` | depends on style | Enable inner and outer borders |
  110. | `outerBorder` | `boolean` | `borders` | Override outer border visibility |
  111. | `borderStyle` | `BorderStyle` | `"single"` | Table border character set |
  112. | `borderColor` | `ColorInput` | conceal fg or `#888888` | Border color for markdown tables |
  113. | `selectable` | `boolean` | `true` | Enable table cell text selection |
  114. ### Table styles
  115. `tableOptions.style` picks a preset that tunes the defaults for `borders`, `outerBorder`, and `widthMode` together:
  116. - **`"grid"`**: boxed table with visible borders. Defaults to `borders: true`, `widthMode: "full"`. This is the normal markdown-in-a-box rendering.
  117. - **`"columns"`**: borderless columns with a 2-column gap, defaults to `widthMode: "content"`. Useful for append-only output where a full-width grid feels heavy.
  118. If you do not pass `style`, it defaults to `"columns"` when `internalBlockMode` is `"top-level"`, and `"grid"` otherwise. You can still override individual fields (for example, `borders: true`) to pull toward a different look.
  119. ## Custom node rendering
  120. Override rendering for a token and fall back to default rendering:
  121. ```typescript
  122. const markdown = new MarkdownRenderable(renderer, {
  123. content: "# Title\n\nHello",
  124. syntaxStyle,
  125. renderNode: (token, context) => {
  126. if (token.type === "heading") {
  127. return context.defaultRender()
  128. }
  129. return undefined
  130. },
  131. })
  132. ```
  133. ## Construct API
  134. > Not available yet. Use `MarkdownRenderable` for now.
  135. ## Properties
  136. | Property | Type | Default | Description |
  137. | ------------------- | ------------------------------------------------------------------------------- | ------------- | ------------------------------------------------------------- |
  138. | `content` | `string` | `""` | Markdown source |
  139. | `syntaxStyle` | `SyntaxStyle` | required | Style definitions for tokens |
  140. | `fg` | `ColorInput` | - | Base foreground color (flows into inner code blocks) |
  141. | `bg` | `ColorInput` | - | Base background color (flows into inner code blocks) |
  142. | `conceal` | `boolean` | `true` | Hide markdown markers in markdown text |
  143. | `concealCode` | `boolean` | `false` | Hide markers inside fenced code blocks |
  144. | `streaming` | `boolean` | `false` | Incremental mode; set `false` to finalize |
  145. | `tableOptions` | `MarkdownTableOptions` | - | Options for markdown table rendering |
  146. | `internalBlockMode` | `"coalesced" \| "top-level"` | `"coalesced"` | Experimental: expose top-level blocks as separate renderables |
  147. | `treeSitterClient` | `TreeSitterClient` | - | Custom Tree-sitter client for code blocks |
  148. | `renderNode` | `(token: Token, context: RenderNodeContext) => Renderable \| null \| undefined` | - | Custom render hook per markdown block |