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

Pages: the gap at a break between two list items

razorback16f7cf21f4a6bc
diff --git a/hooks/lib/markdown.ts b/hooks/lib/markdown.ts
index 9f3c12b..b164b68 100644
--- a/hooks/lib/markdown.ts
+++ b/hooks/lib/markdown.ts
@@ -122,10 +122,25 @@ export function splitSegments(md: string, columns: number, budget = SEGMENT_ROWS
return texts.map(t => `${t}\n\n${defs.filter(d => !t.includes(d)).join('\n')}`.trimEnd())
}
+// A list item's first line, and its indent.
+const ITEM = /^( {0,3})(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$)/
+
+// The first line of a list block's last item (`last`) or first item; null
+// when the block is no list, or that line opens a fence or math.
+function itemLine(block: Block, last: boolean): string | null {
+ const head = ITEM.exec(block.lines[0] ?? '')
+ if (!head || block.fence || block.isMath) return null
+ const items = block.lines.filter(line => ITEM.exec(line)?.[1] === head[1])
+ const line = (last ? items.at(-1) : items[0])!
+ return /`{3}|~{3}|\$\$/.test(line) ? null : line
+}
+
// The blocks at each break between segments, for the gap the template puts
// there: the last block above the break and the first below it (a fence as
// a short one of its kind, a typst fence with no code; link definitions are
-// no block). Null for the first segment and where a side has no block.
+// no block). Two lists: only the first lines of the items at the break (the
+// two in one flow make a loose list, which would add gaps inside them). Null
+// for the first segment and where a side has no block.
export function seamsOf(segments: readonly string[]): ([string, string] | null)[] {
const short = (block: Block) => {
const { fence } = block
@@ -135,7 +150,10 @@ export function seamsOf(segments: readonly string[]): ([string, string] | null)[
const ends = segments.map(seg => blocksOf(seg).filter(b => b.fence || !b.lines.every(line => LINK_DEF.test(line))))
return ends.map((blocks, i) => {
const above = ends[i - 1]?.at(-1)
- return above && blocks.length ? [short(above), short(blocks[0]!)] : null
+ const below = blocks[0]
+ if (!above || !below) return null
+ const [a, b] = [itemLine(above, true), itemLine(below, false)]
+ return a !== null && b !== null ? [a, b] : [short(above), short(below)]
})
}
diff --git a/tests/gap-sh-test.sh b/tests/gap-sh-test.sh
index 894a52f..209b1ca 100644
--- a/tests/gap-sh-test.sh
+++ b/tests/gap-sh-test.sh
@@ -21,12 +21,17 @@ command -v typst >/dev/null 2>&1 || { echo "gap-sh-test: skipped (no typst)"; ex
# Doc JSONs from the Markdown on stdin (blocks between blank lines, no
# fence with a blank line): $tmp/<name>-one.json (one segment) and
-# $tmp/<name>-cut.json (one segment for each block, with its seams).
+# $tmp/<name>-cut.json (one segment for each block, with its seams; as
+# seamsOf, two lists send the first lines of the items at the break).
docs() {
perl -e 'local $/; my $m = <STDIN>; $m =~ s/\n+$//; my @b = split /\n\n+/, $m;
my $q = sub { my $s = shift; $s =~ s/(["\\])/\\$1/g; $s =~ s/\n/\\n/g; "\"$s\"" };
+ my $item = qr/^( {0,3})(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$)/;
+ my $line = sub { my @l = split /\n/, $_[0]; my ($i) = $l[0] =~ $item or return; my @s = grep { /$item/ && $1 eq $i } @l;
+ my $x = $_[1] ? $s[-1] : $s[0]; $x =~ /`{3}|~{3}|\$\$/ ? undef : $x };
+ my $seam = sub { my ($x, $y) = ($line->($_[0], 1), $line->($_[1], 0)); defined $x && defined $y ? ($x, $y) : @_ };
open my $one, ">", "$ARGV[0]-one.json"; print $one "{\"base\": \"\", \"segments\": [" . $q->($m) . "]}";
- my @seams = ("null", map { "[" . $q->($b[$_ - 1]) . ", " . $q->($b[$_]) . "]" } 1 .. $#b);
+ my @seams = ("null", map { "[" . join(", ", map { $q->($_) } $seam->($b[$_ - 1], $b[$_])) . "]" } 1 .. $#b);
open my $cut, ">", "$ARGV[0]-cut.json"; print $cut "{\"base\": \"\", \"segments\": [" . join(", ", map { $q->($_) } @b) . "], \"seams\": [" . join(", ", @seams) . "]}"' "$tmp/$1"
}
# The inputs of doc $1 with row $2, grid $3 (1 on, 0 off).
@@ -76,6 +81,32 @@ done
same list 13.2 0
same mixed 13.2 0
+# The gap at each break of $1 (row $2, grid $3): from the last line above
+# it to the first line below, as in one page (on the grid to 0.15 pt; off
+# the grid at most a row more). Only there: a list cut at a blank line is
+# loose in one page, but each part alone is tight.
+breaks() {
+ lines "$tmp/$1-one" "$2" "$3" && lines "$tmp/$1-cut" "$2" "$3" || return
+ out=$(paste -d ' ' "$tmp/$1-one.lines" "$tmp/$1-cut.lines" | perl -ne 'my ($y1, $p1, $r1, $t1, $y2, $p2, $r2, $t2) = split;
+ print "$t1 / $t2\n" if $t1 ne $t2;
+ if (defined $p and $p2 != $p) { my ($g1, $g2) = ($y1 - $o, $y2 - $c); my $d = $g2 - $g1;
+ print "$t1: gap $g1 in one page, $g2 cut\n" if ('"$3"' ? abs($d) > 0.15 : ($d < -0.15 or $d > '"$2"' + 0.15)) }
+ ($p, $o, $c) = ($p2, $y1, $y2)')
+ [ "$(wc -l < "$tmp/$1-one.lines")" = "$(wc -l < "$tmp/$1-cut.lines")" ] || out="lines: $(wc -l < "$tmp/$1-one.lines") and $(wc -l < "$tmp/$1-cut.lines") $out"
+ [ -z "$out" ] && ok || bad "$1 at its breaks (row $2, grid $3): $out"
+}
+
+# Two lists at a break: one loose list in one page. Items with lists in
+# them (the gap is not the items' own loose gaps), and plain items.
+printf '1. **Install** the tool\n - run the script\n - check the path\n\n2. **Configure** it\n - edit the file\n\n3. **Run** it\n' | docs loose
+printf -- '- a\n- b\n- c\n- d\n\n- e\n' | docs mixedlist
+for row in 13.2 11.6331 7; do
+ breaks loose "$row" 1
+ breaks mixedlist "$row" 1
+done
+breaks loose 13.2 0
+breaks mixedlist 13.2 0
+
# The page under the break starts with the blank row (grid, one row a
# line): the list's first line is a row down, not at the page's top.
lines "$tmp/list-cut" 13.2 1 && {
diff --git a/tests/pure.test.ts b/tests/pure.test.ts
index 8916f87..3de99da 100644
--- a/tests/pure.test.ts
+++ b/tests/pure.test.ts
@@ -125,6 +125,16 @@ test('a seam is the block above a break and the block below it', () => {
expect(seamsOf(['Text.\n\n[d]: https://e.com', 'More.\n\n[d]: https://e.com'])).toEqual([null, ['Text.', 'More.']])
})
+test('two lists at a break: the seam is the first lines of the items that meet (one loose list in one flow)', () => {
+ const loose = ['1. **Install** the tool\n - run the script\n - check the path', '2. **Configure** it\n - edit the file', '3. **Run** it']
+ expect(seamsOf(loose)).toEqual([null, ['1. **Install** the tool', '2. **Configure** it'], ['2. **Configure** it', '3. **Run** it']])
+ expect(seamsOf(['- a\n- b\n- c\n- d', '- e'])).toEqual([null, ['- d', '- e']])
+ // A list and a paragraph: the blocks as they are.
+ expect(seamsOf(['- a\n- b', 'After.'])).toEqual([null, ['- a\n- b', 'After.']])
+ // An item that starts a fence or math: the blocks as they are.
+ expect(seamsOf(['- a\n- ```sh\n x\n ```', '- b'])).toEqual([null, ['- a\n- ```sh\n x\n ```', '- b']])
+})
+
test('the seams of a split answer are the blocks that meet at its breaks', () => {
const md = Array.from({ length: 120 }, (_, i) => (i % 3 ? para(i) : `- item ${i}\n- more ${i}`)).join('\n\n')
const segments = splitSegments(md, 80, 30)