Files
windmill/frontend/src/lib/components/MarkdownCodeBlock.svelte
T
Ruben Fiszelandwindmill-internal-app[bot] 6ba400239b feat: charts and tables in AI chat replies and artifacts (#12374)
* feat: render charts and tables in AI chat replies and artifacts

* fix: count display blocks when the chat commits a reply

* fix: keep display blocks in artifact PDF and HTML exports

* chore: update ee-repo-ref to a5d1e052df3e920d3f0b50bdb148c37bf8eec2c4

This commit updates the EE repository reference after PR #978 was merged in windmill-ee-private.

Previous ee-repo-ref: 4ee687c09398105fd7ffe1702493ced097ef6359

New ee-repo-ref: a5d1e052df3e920d3f0b50bdb148c37bf8eec2c4

Automated by sync-ee-ref workflow.

---------

Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
2026-11-12 10:24:41 +02:00

171 lines
4.7 KiB
Svelte

<script lang="ts">
import { getAstNode } from 'svelte-exmarkdown'
import HighlightCode from './HighlightCode.svelte'
import {
csharp,
go,
graphql,
javascript,
php,
plaintext,
python,
rust,
shell,
sql,
typescript,
yaml
} from 'svelte-highlight/languages'
import type { ClipboardCopy } from 'lucide-svelte'
import MermaidDisplay from './copilot/chat/script/MermaidDisplay.svelte'
// Shared `pre` renderer for fenced code blocks in svelte-exmarkdown markdown.
// Chat-agnostic: gives every markdown surface the highlighted panel + subtle
// hover copy button + custom horizontal scrollbar. The AI chat wraps this to
// add its Apply button (via the apply props) and mermaid diagrams.
let {
showApplyButton = false,
onApplyCode = undefined,
applyButtonIcon = undefined,
// Render ```mermaid blocks as diagrams. Off by default so untrusted
// markdown surfaces don't execute mermaid — the chat opts in.
renderMermaid = false,
// Draw diagrams in the light theme whatever the app's theme (printing).
lightMermaid = false,
// Render ```wm-display blocks (charts, tables) through DisplayResult. The fence only
// admits inert kinds (`fenceSchema`), so any surface may opt in.
renderDisplay = false,
// Draw display blocks for paper: plain tables rather than the paginated grid.
staticDisplay = false
}: {
showApplyButton?: boolean
onApplyCode?: () => void
applyButtonIcon?: typeof ClipboardCopy
renderMermaid?: boolean
lightMermaid?: boolean
renderDisplay?: boolean
staticDisplay?: boolean
} = $props()
const astNode = getAstNode()
// Map a fence language to a known highlighter key, or undefined when the fence
// is unlabeled/unrecognized — those render as plaintext rather than being
// mis-colored as TypeScript (this renderer runs on all user markdown).
function getSmartLang(lang: string | undefined) {
switch (lang) {
case 'python':
case 'python3':
case 'py':
return 'python'
case 'deno':
case 'nativets':
case 'bun':
case 'bunnative':
case 'typescript':
case 'ts':
case 'tsx':
return 'typescript'
case 'go':
case 'golang':
return 'go'
case 'shell':
case 'bash':
case 'sh':
case 'zsh':
case 'console':
return 'shell'
case 'frontend':
case 'javascript':
case 'js':
case 'jsx':
case 'mjs':
case 'cjs':
return 'javascript'
case 'graphql':
return 'graphql'
case 'mysql':
case 'snowflake':
case 'bigquery':
case 'oracledb':
case 'postgresql':
case 'postgres':
case 'sql':
return 'sql'
case 'php':
return 'php'
case 'rust':
case 'rs':
return 'rust'
case 'csharp':
case 'cs':
return 'csharp'
case 'ansible':
case 'yaml':
case 'yml':
return 'yaml'
default:
return undefined
}
}
const SMART_LANG_TO_HIGHLIGHT_LANG = {
python: python,
typescript: typescript,
go: go,
shell: shell,
javascript: javascript,
graphql: graphql,
sql: sql,
php: php,
rust: rust,
csharp: csharp,
yaml: yaml
}
// Fenced code is `<pre><code>text</code></pre>` (text nested under <code>).
// Sanitized raw HTML through rehypeRaw is `<pre>text</pre>` — the text is a
// direct child of <pre> with no <code>, so fall back to it or the block would
// render empty now that this renderer handles every <pre>.
let code = $derived(
astNode.current.children?.[0]?.children?.[0]?.value ?? astNode.current.children?.[0]?.value
)
let language = $derived(
(astNode.current.children?.[0]?.properties?.class as string | undefined)
?.split(' ')
.find((c) => c.startsWith('language-'))
?.slice('language-'.length)
)
let smartLang = $derived(getSmartLang(language))
let highlightLanguage = $derived(smartLang ? SMART_LANG_TO_HIGHLIGHT_LANG[smartLang] : plaintext)
</script>
<div class="flex flex-col gap-0.5 rounded-md relative not-prose !text-xs">
<div class="relative w-full bg-surface-secondary rounded-md overflow-hidden">
{#if renderMermaid && language === 'mermaid'}
<MermaidDisplay code={code ?? ''} forceLight={lightMermaid} />
{:else if renderDisplay && language === 'wm-display'}
<!-- Keep this import lazy: DisplayResult's imports reach back into the chat, and a static
import here left Home's session composer stuck on its loading placeholder. -->
{#await import('./display/DisplayFence.svelte')}
<div data-display-pending="true"></div>
{:then { default: DisplayFence }}
<DisplayFence code={code ?? ''} staticRender={staticDisplay} />
{/await}
{:else}
<HighlightCode
className="px-3 py-2.5 !text-xs"
code={code ?? ''}
{highlightLanguage}
language={undefined}
{onApplyCode}
{showApplyButton}
{applyButtonIcon}
buttonsOnHover
customScrollbarX
/>
{/if}
</div>
</div>