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

Grid: small simplifications in the first-line and phase helpers

razorback16733710e7710d
diff --git a/hooks/lib/draw.tsx b/hooks/lib/draw.tsx
index e3c13a2..40d6060 100644
--- a/hooks/lib/draw.tsx
+++ b/hooks/lib/draw.tsx
@@ -104,8 +104,9 @@ const FIRST_LINE: Record<FirstLine, { centre: number; inkTop: number }> = {
}
const HEADING = /^ {0,3}(#{1,6})\s/
+const firstTextLine = (md: string) => md.split('\n').find(l => l.trim()) ?? ''
export function firstLineOf(md: string): FirstLine {
- const line = md.split('\n').find(l => l.trim()) ?? ''
+ const line = firstTextLine(md)
const heading = HEADING.exec(line)
if (heading) return (['h1', 'h2', 'h3'] as const)[heading[1]!.length - 1] ?? 'h4'
if (/^ {0,3}>/.test(line)) return 'quote'
@@ -130,7 +131,7 @@ export function alignFirstLine(kind: FirstLine, rowPt: number): { topPt: number;
export function placeFirstLine(md: string, rowPt: number, grid?: { metrics: GridMetrics; code: number }): { topPt: number; bulletRow: number } {
const kind = firstLineOf(md)
if (!grid) return alignFirstLine(kind, rowPt)
- const level = HEADING.exec(md.split('\n').find(l => l.trim()) ?? '')?.[1]!.length ?? 0
+ const level = HEADING.exec(firstTextLine(md))?.[1]!.length ?? 0
return { topPt: 0, bulletRow: gridFirstRow(kind, level, rowPt, grid.metrics, grid.code) }
}
diff --git a/hooks/lib/grid.ts b/hooks/lib/grid.ts
index fd36a39..ce3786f 100644
--- a/hooks/lib/grid.ts
+++ b/hooks/lib/grid.ts
@@ -54,7 +54,7 @@ export function gridFirstRow(kind: FirstLine, level: number, rowPt: number, g: G
case 'h2':
case 'h3':
case 'h4':
- return middle(g.heading[Math.min(6, Math.max(1, level)) - 1] ?? g.body)
+ return middle(g.heading[level - 1] ?? g.body)
case 'quote':
return rowsNear(g.pad.quote, rowPt) + body
case 'code':
diff --git a/hooks/lib/typst.ts b/hooks/lib/typst.ts
index 7c5861d..3c0b421 100644
--- a/hooks/lib/typst.ts
+++ b/hooks/lib/typst.ts
@@ -279,7 +279,7 @@ export async function runKillable(
}
// The first page's top margin and bullet row of a job's text.
-export function firstLinePlace(job: Job, segments: readonly string[]) {
+function firstLinePlace(job: Job, segments: readonly string[]) {
const fit = fontFit(job.env.metrics, job.size)
const { rowPt } = sizing(job.columns, job.cell, fit.columnPt)
const grid = job.grid ? { metrics: job.env.metrics.grid ?? DEFAULT_GRID, code: fit.code } : undefined
diff --git a/render/md.typ b/render/md.typ
index b217bcc..b67a681 100644
--- a/render/md.typ
+++ b/render/md.typ
@@ -191,14 +191,13 @@
// block in it with lines of the same parity does not move again, and one
// with the other parity moves back.
#let half-cost = 103%
-#let is-half(costs) = costs.widow == half-cost
#let half-costs(half) = (widow: if half { half-cost } else { 100% })
// The rows of the line box here, from the grid's fixed edges (needs context).
#let line-rows() = if type(text.top-edge) == length and type(text.bottom-edge) == length {
calc.max(1, int(calc.round((text.top-edge - text.bottom-edge).to-absolute() / row)))
} else { 1 }
// Half a row when lines of parity `half` are in a flow of the other phase.
-#let phase-pad(half) = if half != is-half(text.costs) { row / 2 } else { 0pt }
+#let phase-pad(half) = if half != (text.costs.widow == half-cost) { row / 2 } else { 0pt }
// `body`, lines of the text here, in their phase.
#let in-phase(body) = context {
let half = calc.even(line-rows())