Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

Pictures: SVG files that Claude sends draw under their row, made PNGs by Typst at their CSS px size

razorback1617aed5228f9a
diff --git a/docs/usage.md b/docs/usage.md
index d71468f..6d82285 100644
--- a/docs/usage.md
+++ b/docs/usage.md
@@ -20,13 +20,13 @@ Intelligent UI draws Claude's answers as rendered Markdown pictures. Only the dr
A `theme` dictionary (`theme.fg`, `theme.muted`, `theme.blue`, `theme.palette`, `theme.width`, …) is in scope, so drawings match the terminal's colours. The mod tells Claude about this in a short part of the system prompt. `theme.width` and `theme.height` give the space for a drawing. The mod keeps the size that a message had when it was drawn the first time. When the terminal size changes, the mod makes the drawings larger or smaller by the same amount.
- Files that Claude sends to you (the SendUserFile tool). The mod draws each file under the row that shows the files, in the same order:
- - PNG, JPEG and WebP images. Typst changes a JPEG or WebP image into a PNG image in the cache folder. The PNG image is the size of the space for the picture in the transcript, or smaller. Typst does this one time for each version of the file and each size of the space. When the space changes, the mod shows the old PNG image until Typst makes the new one.
+ - PNG, JPEG, WebP and SVG images. Typst changes a JPEG, WebP or SVG image into a PNG image in the cache folder. The PNG image is the size of the space for the picture in the transcript, or smaller. Typst does this one time for each version of the file and each size of the space. When the space changes, the mod shows the old PNG image until Typst makes the new one.
- Markdown files (`.md` or `.markdown`, 256 KiB or less). The mod draws the file as rendered pictures, the same as an answer. A relative image path in the file starts from the folder of the file.
- Other files stay as the normal row. JPEG images, WebP images and Markdown files need Typst.
+ Other files stay as the normal row. JPEG images, WebP images, SVG images and Markdown files need Typst.
- Images that you paste into the prompt (`[Image #N]`). The mod draws each image under your prompt.
- These pictures are not larger than the image. They are not wider than the transcript. The height setting (`/iui height`) sets the most rows that they can use (Typst drawings use it too): by default, half the screen and not more than 24 rows. The mod draws PNG, JPEG and WebP images only. Other image types (for example GIF and SVG) stay as the normal row.
+ These pictures are not larger than the image. They are not wider than the transcript. The height setting (`/iui height`) sets the most rows that they can use (Typst drawings use it too): by default, half the screen and not more than 24 rows. The mod draws PNG, JPEG, WebP and SVG images only. Other image types (for example GIF) stay as the normal row. The size of an SVG image is its width and height in CSS px.
- The plan in plan mode (the ExitPlanMode tool). The mod draws the plan as a picture in the transcript, the same as an answer. Then the mod asks you to approve the plan. Refer to [Plan approval](#plan-approval).
## Commands
diff --git a/hooks/lib/epoch.ts b/hooks/lib/epoch.ts
index 4313344..7b292da 100644
--- a/hooks/lib/epoch.ts
+++ b/hooks/lib/epoch.ts
@@ -108,7 +108,7 @@ export function jobDone($: EngineInterface) {
const holds = (job: Job) => job.epoch === S.epoch && job.onScreen !== false
// Commits the open epoch when no burst runs and it is overdue, or it is
-// ready and no job of it on screen waits or runs and no JPEG or WebP
+// ready and no job of it on screen waits or runs and no JPEG, WebP or SVG
// converts.
function maybeCommit($: EngineInterface) {
if (S.resizing || !epochOpen()) return
diff --git a/hooks/lib/images.ts b/hooks/lib/images.ts
index e9c6381..e953284 100644
--- a/hooks/lib/images.ts
+++ b/hooks/lib/images.ts
@@ -33,26 +33,34 @@ export function pictureCells(size: { width: number; height: number }, columns: n
}
// The kinds of file a SendUserFile result can draw: an image the engine's
-// Image takes (PNG), an image Typst makes a PNG of (JPEG, WebP), and a
+// Image takes (PNG), an image Typst makes a PNG of (JPEG, WebP, SVG), and a
// Markdown file (drawn as an answer is).
-export type SentKind = 'png' | 'jpeg' | 'webp' | 'markdown'
+export type SentKind = 'png' | 'jpeg' | 'webp' | 'svg' | 'markdown'
export type SentFile = { path: string; kind: SentKind }
// An image's type: by its media type, else by its file name. Null for any
-// other type (GIF, SVG, …). An image with neither is a PNG to try (the
-// header read tells for sure).
-export function imageKindOf(a: { path: string; media_type?: unknown }): 'png' | 'jpeg' | 'webp' | null {
+// other type (GIF, …). An image with neither is a PNG to try (the header
+// read tells for sure).
+export function imageKindOf(a: { path: string; media_type?: unknown }): 'png' | 'jpeg' | 'webp' | 'svg' | null {
if (typeof a.media_type === 'string') {
const type = a.media_type.trim().toLowerCase()
if (type === 'image/png') return 'png'
if (type === 'image/jpeg' || type === 'image/jpg') return 'jpeg'
+ if (type === 'image/svg+xml') return 'svg'
return type === 'image/webp' ? 'webp' : null
}
if (/\.jpe?g$/i.test(a.path)) return 'jpeg'
if (/\.webp$/i.test(a.path)) return 'webp'
+ if (isSvgPath(a.path)) return 'svg'
return 'png'
}
+// An SVG file, by its name. Claude Code sends one with isImage false (its
+// images are PNG, JPEG, GIF and WebP) and the media type image/svg+xml.
+export const isSvgPath = (path: string) => /\.svg$/i.test(path)
+const isSvg = (a: { path: string; media_type?: unknown }) =>
+ typeof a.media_type === 'string' ? a.media_type.trim().toLowerCase() === 'image/svg+xml' : isSvgPath(a.path)
+
// A Markdown file, by its name: .md or .markdown.
export const isMarkdownPath = (path: string) => /\.(md|markdown)$/i.test(path)
@@ -64,19 +72,19 @@ export function sentFiles(output: unknown): SentFile[] {
for (const a of list) {
if (!a || typeof a !== 'object' || typeof a.path !== 'string' || a.path === '') continue
if (found.some(f => f.path === a.path)) continue
- const kind = a.isImage === true ? imageKindOf(a) : isMarkdownPath(a.path) ? 'markdown' : null
+ const kind = a.isImage === true ? imageKindOf(a) : isSvg(a) ? 'svg' : isMarkdownPath(a.path) ? 'markdown' : null
if (kind) found.push({ path: a.path, kind })
}
return found
}
-// The image paths of a SendUserFile result that can draw (PNG, JPEG, WebP).
+// The image paths of a SendUserFile result that can draw (PNG, JPEG, WebP, SVG).
export const sentImages = (output: unknown) => sentFiles(output).filter(f => f.kind !== 'markdown').map(f => f.path)
// The folder of a file, for the relative images of a Markdown file.
export const folderOf = (path: string) => path.replace(/\/+[^/]*$/, '') || '/'
-// The box in pixels a JPEG or WebP is made to fit: the space a picture
+// The box in pixels a JPEG, WebP or SVG is made to fit: the space a picture
// takes (as pictureCells), each side at most CONVERT_MAX_SIDE.
export type PixelBox = { width: number; height: number }
export function convertBox(columns: number, rows: number, px: Cell): PixelBox {
@@ -84,7 +92,7 @@ export function convertBox(columns: number, rows: number, px: Cell): PixelBox {
return { width: side(columns * px.width), height: side(rows * px.height) }
}
-// The PNG Typst makes of a JPEG or WebP: in a key folder of the cache (as a
+// The PNG Typst makes of a JPEG, WebP or SVG: in a key folder of the cache (as a
// compile's, dropped after 2 days), by the file, its stat stamp and the box,
// so a changed file or a new box is made again.
export const convertedPath = (cache: string, file: string, stamp: string, box: PixelBox) =>
diff --git a/hooks/lib/pictures.tsx b/hooks/lib/pictures.tsx
index f508554..ee4012b 100644
--- a/hooks/lib/pictures.tsx
+++ b/hooks/lib/pictures.tsx
@@ -1,6 +1,6 @@
// Pictures under rows: the files Claude sends (SendUserFile) and the images
// the user pastes are drawn under their row. An image is drawn at its own
-// size or smaller (a JPEG or WebP as the PNG Typst makes of it); a Markdown
+// size or smaller (a JPEG, WebP or SVG as the PNG Typst makes of it); a Markdown
// file as an answer is. The pure parts are in images.ts.
import type { EngineInterface, FsStat, ImageSource, RenderElement, RenderInput } from 'claude-code'
@@ -118,7 +118,7 @@ export async function shownOne($: EngineInterface, e: RowInput, ready: Env, file
const nameOf = (file: string) => file.split('/').pop() || 'image'
-// A JPEG or WebP as a PNG that Typst makes (render/convert.typ) in the
+// A JPEG, WebP or SVG as a PNG that Typst makes (render/convert.typ) in the
// cache, to fit `box`: once per stat stamp and box, one run at a time per
// key. The first PNG of a file is waited for; for a new box, the last PNG
// of the file draws at once while the new one is made, then jobDone draws
@@ -210,7 +210,7 @@ export async function sentTextOf($: EngineInterface, file: string, at?: StatAt):
// relative images by its own folder). `markdown`: the env for the Markdown
// files (null: they stay out); `id`: the call's tool_use_id; `at`: as
// statOf. Images size from the Frame on screen; while an epoch is open, the
-// PNG of a JPEG or WebP for the target is made at once (not waited for), so
+// PNG of a JPEG, WebP or SVG for the target is made at once (not waited for), so
// it is there when the epoch commits. With no PNG of that file yet (a
// resumed session) only the target's is made, and nothing draws for it
// until the commit.
@@ -248,7 +248,7 @@ export async function sentOf(
}
// The space a picture takes in `frame` with the height setting `height`
-// (as in shownOne), which a JPEG or WebP is made to fit. Over ssh it stays
+// (as in shownOne), which a JPEG, WebP or SVG is made to fit. Over ssh it stays
// INLINE_MAX_SIDE pixels wide at most.
const boxOf = (frame: Frame, inline: boolean, height: Height): PixelBox =>
convertBox(pictureColumns(frame, inline), pictureRowsFor(frame.rows, height), frame.cell)
diff --git a/hooks/lib/settings.ts b/hooks/lib/settings.ts
index c5c9f1d..6049782 100644
--- a/hooks/lib/settings.ts
+++ b/hooks/lib/settings.ts
@@ -135,7 +135,7 @@ export const DESIGNS_KEPT_DAYS = 180
// A Markdown file Claude sends (SendUserFile) draws when it is at most this
// many bytes.
export const SENT_TEXT_MAX_BYTES = 256 * 1024
-// The PNG Typst makes of a JPEG or WebP fits the space of the picture, and
+// The PNG Typst makes of a JPEG, WebP or SVG fits the space of the picture, and
// each side is at most this many pixels (never made larger).
export const CONVERT_MAX_SIDE = 4096
diff --git a/hooks/lib/state.ts b/hooks/lib/state.ts
index 196afec..9497087 100644
--- a/hooks/lib/state.ts
+++ b/hooks/lib/state.ts
@@ -233,7 +233,7 @@ export const INLINE_KEPT = 64
// PNG), and the pasted-images folder of each session once found.
export const pictures = new Map<string, { stamp: string; size: { width: number; height: number } | null }>()
export const pasteDirs = new Map<string, string>()
-// SendUserFile: the PNG Typst made of each JPEG or WebP by its file, stat
+// SendUserFile: the PNG Typst made of each JPEG, WebP or SVG by its file, stat
// stamp and box (null: it failed), the ones Typst makes now (same keys), the
// last PNG made of each file (any box: it draws while a new box is made),
// and the text of each Markdown file by its stat stamp (null: it does not
diff --git a/hooks/lib/typst.ts b/hooks/lib/typst.ts
index ee9749e..da8faf4 100644
--- a/hooks/lib/typst.ts
+++ b/hooks/lib/typst.ts
@@ -44,7 +44,7 @@ export function typstCall(o: {
// The arguments that load only the fonts of a lean folder (and Typst's own).
export const fontArgs = (fonts?: string) => (fonts ? ['--ignore-system-fonts', '--font-path', fonts] : [])
-// The typst command that makes a PNG of a JPEG or WebP (render/convert.typ)
+// The typst command that makes a PNG of a JPEG, WebP or SVG (render/convert.typ)
// that fits `box` (in pixels), never larger than its own pixel size: at 72
// ppi, 1 pt is 1 pixel.
export function convertCall(o: {
diff --git a/render/convert.typ b/render/convert.typ
index cfd25b5..a5354c2 100644
--- a/render/convert.typ
+++ b/render/convert.typ
@@ -1,12 +1,13 @@
-// Intelligent UI: makes a PNG of a JPEG or WebP (the engine's Image draws PNG
-// only). Compiled at 72 ppi, where 1 pt is 1 pixel.
+// Intelligent UI: makes a PNG of a JPEG, WebP or SVG (the engine's Image
+// draws PNG only). Compiled at 72 ppi, where 1 pt is 1 pixel.
//
// Inputs: f = the image's absolute path (with --root /); maxw, maxh = the
// box in pixels the PNG must fit (0 or absent: no limit on that side).
//
// Typst sizes an image by its own DPI (a 300 dpi JPEG is drawn at a quarter
// of its pixels), so the pixel size is read from the file and set as the
-// width. A file it can not read keeps Typst's size.
+// width. An SVG's size is its CSS px (Typst takes 1px as 0.75pt). A file it
+// can not read keeps Typst's size.
#set page(width: auto, height: auto, margin: 0pt)
#let f = sys.inputs.at("f", default: "")
@@ -40,6 +41,7 @@
} else if has-tag(12, "VP8X") { (1 + le24(24), 1 + le24(27)) }
}
+#let svg = lower(f).ends-with(".svg")
#let size = if has-tag(0, "RIFF") and has-tag(8, "WEBP") { webp-size() } else if at(0) == 0xff and at(1) == 0xd8 { jpeg-size() }
// The sides as drawn: the pixel size, swapped when Typst turns the image by
@@ -47,6 +49,7 @@
// aspect kept; an image is never made larger.
#context {
let natural = measure(image(data))
+ let natural = if svg { (width: natural.width / 0.75, height: natural.height / 0.75) } else { natural }
let (w, h) = if size == none or size.at(0) == 0 or size.at(1) == 0 { (natural.width / 1pt, natural.height / 1pt) } else { size }
let turned = w != h and (natural.width > natural.height) != (w > h)
let (dw, dh) = if turned { (h, w) } else { (w, h) }
diff --git a/tests/images.test.ts b/tests/images.test.ts
index 1aa5dd6..08cd278 100644
--- a/tests/images.test.ts
+++ b/tests/images.test.ts
@@ -52,7 +52,7 @@ test('the height command: half, full, 6 to 200 rows, or nothing to show it', ()
expect(parseCommand('height 12 rows')).toEqual({ kind: 'bad' })
})
-test('only PNG, JPEG and WebP image attachments are drawn', () => {
+test('only PNG, JPEG, WebP and SVG image attachments are drawn', () => {
expect(sentImages({
attachments: [
{ path: '/t/a.png', size: 1, isImage: true, media_type: 'image/png' },
@@ -63,8 +63,12 @@ test('only PNG, JPEG and WebP image attachments are drawn', () => {
{ path: '/t/e.webp', size: 1, isImage: true, media_type: 'image/webp' },
{ path: '/t/f.gif', size: 1, isImage: true, media_type: 'image/gif' },
{ path: '/t/g.svg', size: 1, isImage: true, media_type: 'image/svg+xml' },
+ // As Claude Code sends an SVG: not an image, by its media type.
+ { path: '/t/h.svg', size: 1, isImage: false, media_type: 'image/svg+xml' },
+ { path: '/t/i.SVG', size: 1, isImage: false },
+ { path: '/t/j.svg', size: 1, isImage: false, media_type: 'text/plain' },
],
- })).toEqual(['/t/a.png', '/t/b.png', '/t/c.jpg', '/t/e.webp'])
+ })).toEqual(['/t/a.png', '/t/b.png', '/t/c.jpg', '/t/e.webp', '/t/g.svg', '/t/h.svg', '/t/i.SVG'])
expect(sentImages(undefined)).toEqual([])
expect(sentImages('error text')).toEqual([])
expect(sentImages({ attachments: 'x' })).toEqual([])
@@ -78,7 +82,8 @@ test('an image type: by its media type, else by its name; with neither, a PNG to
// The media type wins over the name.
expect(imageKindOf({ path: '/t/a.jpg', media_type: 'image/png' })).toBe('png')
expect(imageKindOf({ path: '/t/a.png', media_type: 'image/gif' })).toBe(null)
- expect(imageKindOf({ path: '/t/a.png', media_type: 'image/svg+xml' })).toBe(null)
+ expect(imageKindOf({ path: '/t/a.png', media_type: 'image/svg+xml' })).toBe('svg')
+ expect(imageKindOf({ path: '/t/a.Svg' })).toBe('svg')
expect(imageKindOf({ path: '/t/a.PNG' })).toBe('png')
expect(imageKindOf({ path: '/t/a.JPG' })).toBe('jpeg')
expect(imageKindOf({ path: '/t/a.jpeg' })).toBe('jpeg')
@@ -392,22 +397,23 @@ test('a file that is not an image, a JPEG and a missing file leave the row as it
}
})
-test('a JPEG and a WebP Claude sends are made PNGs by Typst once, and draw under the row', async ($, on) => {
- const { converts, moves, heads } = world(on, { typst: true, files: { '/t/photo.jpg': JPEG, '/t/shot.webp': '' } })
+test('a JPEG, a WebP and an SVG Claude sends are made PNGs by Typst once, and draw under the row', async ($, on) => {
+ const { converts, moves, heads } = world(on, { typst: true, files: { '/t/photo.jpg': JPEG, '/t/shot.webp': '', '/t/chart.svg': '<svg/>' } })
await ready($)
const output = { attachments: [
{ path: '/t/photo.jpg', size: 1000, isImage: true, media_type: 'image/jpeg' },
{ path: '/t/shot.webp', size: 1000, isImage: true, media_type: 'image/webp' },
+ { path: '/t/chart.svg', size: 1000, isImage: false, media_type: 'image/svg+xml' },
] }
const ui = await $.ui.mount(sent(output))
const tree: any = await ui.drawn()
expect(tree.children[0]).toMatchObject(ENGINE)
const shown = images(tree)
- expect(shown.map(i => i.props.alt)).toEqual(['photo.jpg', 'shot.webp'])
+ expect(shown.map(i => i.props.alt)).toEqual(['photo.jpg', 'shot.webp', 'chart.svg'])
expect(shown[0].props).toMatchObject({ columns: 40, rows: 10 })
expect(shown[0].props.source.file).toMatch(/^\/c\/[0-9a-f]{16}\/image\.png$/)
expect(shown[0].props.source.file).not.toBe(shown[1].props.source.file)
- expect(converts.map(argv => argv.find(a => a.startsWith('f=')))).toEqual(['f=/t/photo.jpg', 'f=/t/shot.webp'])
+ expect(converts.map(argv => argv.find(a => a.startsWith('f=')))).toEqual(['f=/t/photo.jpg', 'f=/t/shot.webp', 'f=/t/chart.svg'])
// Made to fit the space of the picture: 98 x 20 cells of 16 x 32 px.
expect(converts[0]).toContain('maxw=1568')
expect(converts[0]).toContain('maxh=640')
@@ -417,8 +423,8 @@ test('a JPEG and a WebP Claude sends are made PNGs by Typst once, and draw under
expect(moves[0]).toEqual(['mv', '-f', converts[0]!.at(-1), out])
// A redraw converts nothing again, and the JPEG's own bytes are never read as a PNG.
await ui.redraw(sent(output).props)
- expect(images(await ui.drawn()).length).toBe(2)
- expect(converts.length).toBe(2)
+ expect(images(await ui.drawn()).length).toBe(3)
+ expect(converts.length).toBe(3)
expect(heads).not.toContain('/t/photo.jpg')
})