Markdown
Renders markdown content using the marked parser. Sanitized via DomSanitizer. Streams gracefully — partial markdown still renders. Optional LaTeX (KaTeX) and Mermaid rendering, both opt-in.
Examples
Rich content
Headings, lists, links, emphasis, and inline code.
Release notes — v21.0.4
This is the latest patch of ngx-prompt-kit.
- 26 components plus the
cn()utility - Built on Spartan UI primitives
- Distributed via Angular schematics
Run ng add ngx-prompt-kit to get started.
<pk-markdown
class="prose prose-sm dark:prose-invert max-w-none"
[content]="markdownString"
/>Streaming snapshot
A mid-stream chunk where the assistant hasn't finished writing the bullet list yet.
Steps so far
- Read the file
- Parsed the AST
- Looking up the symbol now
<pk-markdown
class="prose prose-sm dark:prose-invert max-w-none"
[content]="partialMarkdown"
/> Math and diagram rendering are opt-in via [enableMath] and [enableDiagrams]. KaTeX (~280 KB) and Mermaid (~600 KB) lazy-load only when these flags are true — the default markdown bundle stays light. KaTeX additionally requires its stylesheet (node_modules/katex/dist/katex.min.css) in your angular.json styles array.
Math (LaTeX via KaTeX)
Set [enableMath]='true' to render inline ($…$) and block ($$…$$) math. Bad LaTeX renders as source text rather than throwing — safe for streamed model output.
The quadratic formula solves any second-degree polynomial:
$$x = \frac{-b \pm \sqrt{b^{2} - 4ac}}{2a}$$
Inline expressions work too — Euler's identity, $e^{i\pi} + 1 = 0$, sits naturally in a sentence.
Maxwell's first equation in differential form:
$$\nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_{0}}$$
<pk-markdown
class="prose prose-sm dark:prose-invert max-w-none"
[enableMath]="true"
[content]="markdownWithMath"
/>Mermaid diagram
Set [enableDiagrams]='true' to render Mermaid blocks. Theme detection is automatic against the .dark class on documentElement; pass [mermaidTheme] explicitly to override. Failed diagrams fall back to the source as a code block.
A typical streaming-response cycle in a chat app:
The diagram above re-renders only when its source changes — identical sources share a stable id.
<pk-markdown
class="prose prose-sm dark:prose-invert max-w-none"
[enableDiagrams]="true"
[content]="markdownWithMermaid"
/>Math + diagrams together
Both flags can be enabled on the same component. They run in independent post-render passes (~300ms debounce) so they coexist without interference.
Pricing rationale
For a single round-trip with $T_{in}$ input tokens and $T_{out}$ output tokens at rates $p_{in}$ and $p_{out}$ (USD per million):
$$\text{cost} = \frac{T_{in} \cdot p_{in} + T_{out} \cdot p_{out}}{10^{6}}$$
Cost flows through a typical session like this:
The two passes (math + diagram) run independently after marked emits HTML.
<pk-markdown
class="prose prose-sm dark:prose-invert max-w-none"
[enableMath]="true"
[enableDiagrams]="true"
[content]="markdownWithBoth"
/>Installation
Add the markdown component (and the cn() utility) to your project.
ng generate ngx-prompt-kit:markdownComponent API
PkMarkdown
| Prop | Type | Default | Description |
|---|---|---|---|
| content | string | '' | The markdown source to render. |
| class | string | — | Extra classes for the rendered container. |
| enableMath | boolean | false | Lazy-load KaTeX and render LaTeX math. Requires katex.min.css in your global styles. |
| enableDiagrams | boolean | false | Lazy-load Mermaid and render ```mermaid blocks. Mermaid initializes with securityLevel:strict. |
| mathInlineDelimiter | "$" | "\(" | "both" | "$" | Inline math delimiter style. "both" accepts either dollar or LaTeX-style. |
| mathBlockDelimiter | "$$" | "\[" | "both" | "$$" | Block math delimiter style. "both" accepts either dollar or LaTeX-style. |
| mermaidTheme | "auto" | "default" | "dark" | "auto" | Mermaid theme. "auto" detects via the .dark class on documentElement; pass explicitly for bespoke theme management. |