Skip to content

Fix(tui): keep code literal in the Glamour preview - #40

Merged
adibhanna merged 8 commits into
mainfrom
claude/festive-grothendieck-7e756e
Nov 4, 2026
Merged

adibhanna merged 8 commits into
mainfrom
claude/festive-grothendieck-7e756e

Conversation

@adibhanna

@adibhanna adibhanna commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The default Glamour preview rewrites Markdown before rendering, and those rewrites ran inside code. Inline code holding [[Bookmarks|bookmarks]] displayed as bookmarks with stray spaces. Fences had the same problem, and more:

  • Embeds, task states and callouts were rewritten inside code, and ⟦x⟧ lost its brackets.
  • Links inside code could be followed, and a - [ ] inside a fence could be toggled, which would have edited the code.
  • splitBlocks closed a fence on any fence line, so a four-backtick block holding a three-backtick fence broke apart and its contents rendered as normal text.
  • The default style padded inline code for a background it does not have.

Fix

  • Code regions come from goldmark, the parser Glamour uses, so they hold in quotes, list items, indented blocks and spans that cross lines. goldmark moves from an indirect to a direct dependency, at the same version.
  • Placeholder characters hide the markup in code from the rewrites and row styling, and come off only after every step has passed over a row.
  • Links are scanned on the source text, and a match that starts or ends in code is dropped. A link written around a code span, like [`name`](url), keeps its label and target.
  • splitBlocks follows the CommonMark fence rules: a closer matches its opener, a one-line ```inline``` is not a fence, and a fence indent is up to three spaces, no tab.
  • List blocks now hold what is indented under an item, past blank lines and through fences. Each block is parsed alone, so split off, that content read as an indented code block.
  • An embed, video link or math line under an item stays its own block so it keeps its card, without reading as code.
  • Paragraphs in one list item now show on separate rows. Glamour would otherwise run them together with no space. Each paragraph row leads to its own source line and offers its own links; x there still toggles the item's task.
  • Task rows stay on their source lines past code that looks like a list item, so x toggles the task on that row.
  • i and a click in split mode now land on the row's own line. They landed one line above it, on main too, since GotoLine counts from one and srcLine from zero.
  • Code spans keep their pipes in any table goldmark finds: in a quote or list item, without outer pipes, or below a preamble row.

For review

  • Differs from the desktop on table pipes: a | inside code in a table no longer splits the cell, so `[[Note|alias]]` stays whole. From its code, the desktop protects pipes in math only and looks like it would still split this; I did not run it.
  • Drift from stripCodeContent: this follows what Glamour renders, not the desktop's link scan, so preview and desktop scan can disagree on these shapes:
    • Multi-line spans and double-backtick spans.
    • Escaped backticks: \`[[R|r]]\` is a live link here.
    • Fences indented four or more spaces or by a tab: indented code here, a fence there.
    • Fences in a quote: code here, not recognized there.
  • Rename is unchanged: internal/vault is not touched by this PR, so wikiCodeMask and renames behave as before.
  • Known gaps:
    • A fence nested in a list or quote can still get tag or metadata colors on text like #tag. The text stays exact, and blocks that are only code are not affected.
    • x on a code row inside a task item toggles that item, like any of its other rows.
    • A table under a list item has no blank row above it, where main has one. A quote keeps its blank row.
    • A paragraph line starting with *, _, ~, a bare URL or www. carries no row marker, because one would change how it reads. Its links show on the row before it, and that row leads to the earlier line.
    • Prose that wraps with a marker at the start of a row can still shift task rows. That is older behavior, not changed here.
    • A note holding the noncharacters U+FDD0 to U+FDDB shows the character each stands for.
  • Cost: 402ms vs a 372ms baseline on a 7,600-line mixed note. Pathological notes (thousands of open brackets, deep quote nesting) are slow in goldmark, as they are on main.

The zen preview style is a separate renderer and is not changed here.

Testing

  • New tests in internal/tui/previewcode_test.go. They failed before the fix.
  • go test ./... and go vet ./... pass.
  • A throwaway fuzz of 8,000 random notes gave no panics, no placeholders on screen and no task row on a non-task line. It is not part of this PR.
  • This head merged with PR 41 at 89f5f31 in a scratch worktree, using the resolution below, passes go vet and go test ./internal/tui/. Earlier commits on this PR also passed pairwise with PR 42 at a6a0a9a and PR 43 at 4262240; those two have not been re-run on this head, and all the PRs merged together has not been run here.
  • Ran the built TUI in tmux on a scratch vault. On earlier commits: code displayed its exact source, an embed under a list item kept its card, and x toggled the right task in the saved file. On this change: item paragraphs stayed apart, Enter on an ordered item's paragraph row (See alias) opened its link, x on a paragraph row under a task checked that task, and i on that row landed on its own line.

Merging with PR 39 and 41

previewglamour.go conflicts in four hunks. Call guardCode before tokenHider in prepareMarkdown, and keep unguardRow last in mapRows, after finishTokens and postStyleTableRow, because those two strip ⟦⟧ and code's are still guarded at that point. Both mapRows hunks take this PR's side.

The preview rewrote wikilinks, embeds, task states and callouts inside
code, so `[[Note|alias]]` displayed as its alias. Links in code could be
followed and a task in a fence could be toggled.

Code regions now come from goldmark, the parser Glamour uses, so they
hold in quotes, list items and indented blocks. splitBlocks follows the
CommonMark fence rules, so a fence nested in a longer one stays whole.
Inline code loses its padding in the default style, which has no
background to fill. Pipes in a table's code spans no longer split cells.
Each block is parsed alone, so content indented under a list item after
a blank line read as code and lost its links, embed card, math or task.
List blocks now hold that content, past blank lines and through fences.
An embed, video link or math line stays its own block and keeps its
card. An item's paragraphs stay on separate rows, since Glamour runs
them together, and links anywhere in an item are offered on its row.

A code line that looked like a list item shifted the rows after it, so
x could toggle the wrong task. Items now skip code, and a guard keeps
such a row from passing for an item.

Guards come off last in mapRows, so later row steps can't touch code.
Links are scanned on the source text and dropped if they start or end
in code. Code pipes hold in any table goldmark finds. Fence indents are
up to three spaces. vault.CodeMask is gone, as nothing needs it now.
Merging a list into one block mapped a paragraph under an item to the
item's first row: Enter there said "No link on this row", and the link
only worked from the item. Each paragraph line now carries a guard
through rendering, so its row leads to its own source line and offers
its own links. x on such a row still toggles the item's task.

i and a click in split mode landed one line above a row, on main too,
since GotoLine counts from one and srcLine from zero.

A blank line before a quote under an item keeps its blank row.
…th (#39)

Mermaid text diagrams were cut off at the pane's edge, and the library's text layout also dropped edge labels and edges back to earlier nodes. zn keeps the library's parser and now draws flowcharts and state diagrams itself: a row of boxes when one fits, otherwise a column with wrapped labels. Any edge not drawn between neighbouring boxes is listed under its source.

A statement the parser cannot read in full shows the block's source with a note, so nothing goes missing without saying so. IDs in any script, comments, entities and the common edge spellings are read.

Tables in the reading view also change. Aliased links keep columns in line, and a wrapped alias shows no token brackets. In the zen style an alias stays in one cell, wide cells wrap, and tables with no outer pipes render.
Note text now shows as text and does not act on the terminal. The filter lives in internal/termsafe and runs on every TUI frame and, when stdout or stderr is a terminal, on CLI output. Pipes and files keep every byte.

The editor, both reading views and the views that list note text draw a control rune as a one-cell picture such as a visible ESC, so the cursor stays on its rune and a note's own colours stay out of the terminal.

Opening a note with bytes that are not valid UTF-8, or with CRLF line ends, no longer marks it changed, so quitting leaves the file as it is until a real edit. Very long words are broken before Glamour, whose word wrap is quadratic in a word's length.
…#41)

Sequence, class and ER diagrams came from the library's text renderer, which clipped long messages and dropped relationships even when the diagram fit. A sequence diagram is now drawn with every message whole, or shows its source with a note when it cannot be, and a class or ER diagram shows its source with a note rather than a drawing that loses part of it.
…6e' into HEAD

# Conflicts:
#	internal/tui/previewglamour.go
@adibhanna
adibhanna merged commit 6227dea into main Nov 4, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant