Skip to content

Render a cmd-clicked markdown file as a themed overlay page - #866

Merged
umputun merged 11 commits into
masterfrom
markdown-link-overlay
Nov 2, 2026
Merged

umputun merged 11 commits into
masterfrom
markdown-link-overlay

Conversation

@umputun

@umputun umputun commented Nov 2, 2026

Copy link
Copy Markdown
Owner

a ⌘-click on a .md or .markdown path in terminal output now opens the file rendered as a page in the terminal's theme, over its own session, the way a picture path opens since #844. The same page opens from agtermctl session overlay open --markdown FILE.

The page is sized like other clicked links: the link-open mode's size, or 95% when links go to the browser. Code blocks are highlighted, task lists show their boxes, and headings get GitHub-style anchors, so a README's table of contents scrolls in place. Pictures kept in the file's own folder are embedded, up to 256 of them. Unembedded references parsed as images show alt text, unsupported syntax stays text, and the page fetches no network resources. Clicks in local panes and the scratch terminal render the file; a refused open reveals it in Finder.

The markdown comes from an arbitrary clicked path, so the page treats its content as untrusted:

  • the file is read once through BoundedFile (regular file, 4 MB cap), and pictures go through the same descriptor, signature and pixel checks as an image page, within one byte and pixel budget per page
  • a picture reference must remain inside the markdown file's folder after normalization and symlink resolution; escaping paths, ~ and URL schemes are refused before file data is read
  • the source and the picture map travel as HTML-escaped text, and the CSP allows only data: pictures and inline style
  • the renderer (bundled markdown-it 14.1.0 and highlight.js 11.11.1) runs as a user script in its own content world with page JavaScript off. Raw HTML stays text, pictures are placeholders filled through the DOM, and every link except http(s) and #fragment is turned into plain text
  • a web link goes through the usual browser confirmation; the page gets no bridge, and Open in Browser and Show in Finder are refused
  • a markdown path printed in a pane attached from another Mac is never read: a bare path is ignored and a file:// link is revealed, as pictures are

Docs: site/docs.html, site/commands.html, the agent skill and its reference, .claude/rules/control-api.md. The plan is in docs/plans/completed/20261102-markdown-link-overlay.md.

MarkdownPage builds the document a bundled renderer fills: the source as
escaped text, local pictures inside the file's folder (symlinks resolved
before the containment check) embedded once each, and a CSP that loads only
data: pictures. MarkdownProbe reads the file through BoundedFile, the
descriptor check ImageProbe now shares, and embeds pictures within a total
byte and pixel budget.
HtmlSource.markdown is a themed page with no file grant or bridge. The
registry reads it through MarkdownProbe and renders it with the bundled
markdown-it and highlight.js, run as an app-owned user script in its own
content world with the page's JavaScript off. Pictures are emitted only as
the data URIs the app embedded, links keep only http(s) targets and
fragments, and headings get GitHub-style ids. Theme styling is separated
from bridge eligibility; browser hand-off is refused for a markdown page.
The tree reports htmlOverlays[].markdown.
session.overlay.open takes markdownFile, its own field since markdown is the
HUD's flag: an absolute .md or .markdown path, exclusive with every other
source and refused with the program and page flags, and refused as a page
companion. The app checks the file's descriptor before opening, and the CLI
sends --markdown FILE resolved to an absolute path.
A local .md or .markdown path, bare or file://, routes like an image: a pane
or the scratch terminal renders it over the owning session at the link-open
size (95 in browser mode, so cmd-shift outside mouse capture asks for that),
and every other origin, or a refused open, reveals the file. The click checks
the file's descriptor off the main actor and rechecks the HUD, zoom and slot
before opening. Tests whose .md paths stood for a revealed file use .txt.
…imit

The image and markdown checks of session.overlay.open move into their page
files (openImageFile, markdownFileRefusal), one line each at the call site.
The markdown read-back and request encoding tests move beside the projection
and CLI tests that build them.
The user guide's local-path paragraph covers markdown files, the command
reference gains overlay open --markdown and the markdown read-back field, the
bundled skill gets a trigger, a section and a reference entry, and the
control-api rules describe the page, its routing and the theme/bridge split.
…se file: references

a picture now renders as a placeholder whose data URI is set through the DOM, so a
repeated reference no longer copies the whole payload into the markup string, and at
most 256 pictures show. file: references were read but dropped by the renderer, so
they are refused before any read.
…r time

a quote fused with a combining mark is one Character that matched no escape case,
so a picture name could close the image-map attribute and add attributes of its
own. escaping now walks scalars. a run of identical headings probed every earlier
suffix for each heading; a per-base cursor keeps it linear while the used-id check
still catches an authored suffixed heading.
Copilot AI balanced review requested due to automatic review settings November 2, 2026 11:28
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Deploying agterm with  Cloudflare Pages  Cloudflare Pages

Latest commit: 88c429f
Status: ✅  Deploy successful!
Preview URL: https://b55a9403.agterm.pages.dev
Branch Preview URL: https://markdown-link-overlay.agterm.pages.dev

View logs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical check-to-open symlink race can embed images outside the document folder, alongside unresolved loose-task-list rendering, native probe-limit, and cached-image alias defects.

4 open issues
What changed in this PR

Extends the image-overlay behavior from #844 to local Markdown files, allowing users to preview themed documents within their session through ⌘-click or the control CLI.

Changes:

  • Adds Markdown link routing, --markdown FILE, validation, and source read-back.
  • Introduces themed rendering with highlighting, task lists, heading anchors, and bounded local-image embedding.
  • Adds automated coverage and updates user, agent, and engineering documentation.
File Description
site/​docs.html Documents Markdown link previews.
site/​commands.html Documents CLI options and read-back.
project.yml Bundles renderer assets and licenses.
plugins/​agterm/​skills/​agterm/​SKILL.md Adds Markdown discovery and usage guidance.
plugins/​agterm/​skills/​agterm/​reference.md Details Markdown command behavior and restrictions.
docs/​plans/​completed/​20261102-markdown-link-overlay.md Records implementation and verification plan.
agtermUITests/​ControlHtmlOverlayUITests.swift Tests rendering, read-back, and closing.
agtermTests/​MarkdownProbeTests.swift Tests file checks and image budgets.
agtermTests/​MarkdownAssetsTests.swift Checks bundled assets and dependency order.
agtermTests/​HtmlOverlayRegistryTests.swift Tests rendering, isolation, navigation, and reloads.
agtermTests/​GhosttySurfaceViewInputTests.swift Tests click routing and Finder fallback.
agtermTests/​ControlServerLinkOverlayTests.swift Tests Markdown opening and refusal paths.
agtermCore/​Tests/​agtermctlKitTests/​OverlayCommandsTests.swift Tests CLI validation and request encoding.
agtermCore/​Tests/​agtermCoreTests/​MarkdownPageTests.swift Tests escaping, image resolution, and containment.
agtermCore/​Tests/​agtermCoreTests/​LinkPolicyTests.swift Tests Markdown classification and routing.
agtermCore/​Tests/​agtermCoreTests/​HtmlOverlayTests.swift Tests Markdown identity and navigation policy.
agtermCore/​Tests/​agtermCoreTests/​ControlDispatcherOverlayTests.swift Tests dispatcher validation and routing.
agtermCore/​Tests/​agtermCoreTests/​AppStoreTreeProjectionTests.swift Tests Markdown source projection.
agtermCore/​Tests/​agtermCoreTests/​AppStoreRemotePageTests.swift Checks Markdown remains local-only.
agtermCore/​Sources/​agtermctlKit/​SessionCommands.swift Adds the --markdown CLI option.
agtermCore/​Sources/​agtermCore/​MarkdownPage.swift Builds escaped documents and resolves image references.
agtermCore/​Sources/​agtermCore/​LinkPolicy.swift Routes Markdown paths to session overlays.
agtermCore/​Sources/​agtermCore/​HtmlOverlay.swift Adds Markdown source and navigation behavior.
agtermCore/​Sources/​agtermCore/​ControlProtocol.swift Adds request field and validation errors.
agtermCore/​Sources/​agtermCore/​ControlProjection.swift Adds Markdown read-back field.
agtermCore/​Sources/​agtermCore/​ControlDispatcher+Overlay.swift Validates Markdown overlay requests.
agtermCore/​Sources/​agtermCore/​ControlDispatcher+Companions.swift Rejects Markdown page companions.
agtermCore/​Sources/​agtermCore/​AppStore+RemotePage.swift Excludes Markdown from remote presentation.
agtermCore/​Sources/​agtermCore/​AppStore+HtmlOverlay.swift Projects Markdown source paths.
agterm/​Views/​MarkdownProbe.swift Reads Markdown and embeds bounded images.
agterm/​Views/​MarkdownAssets.swift Loads bundled renderer and stylesheet.
agterm/​Views/​ImageProbe.swift Extracts shared descriptor-based file reading.
agterm/​Views/​HtmlOverlayView.swift Handles Markdown source display and toolbar behavior.
agterm/​Views/​HtmlOverlayRegistry.swift Hosts isolated rendering and separates styling from bridge access.
agterm/​Resources/​markdown/​render.js Renders Markdown, images, anchors, and tasks.
agterm/​Resources/​markdown/​page.css Styles rendered content using terminal colors.
agterm/​Resources/​markdown/​markdown-it-14.1.0.LICENSE Includes markdown-it license.
agterm/​Resources/​markdown/​highlight-11.11.1.LICENSE Includes highlight.js license.
agterm/​Ghostty/​LinkOpener.swift Dispatches Markdown clicks with fallback.
agterm/​Control/​ControlServer+SessionActions.swift Checks Markdown files before opening overlays.
agterm/​Control/​ControlServer+RemoteCompanions.swift Handles the new source in companion arguments.
agterm/​Control/​ControlServer+MarkdownPage.swift Adds file checks and asynchronous click opening.
agterm/​Control/​ControlServer+ImagePage.swift Extracts synchronous image-opening helper.
agterm/​agtermApp.swift Connects Markdown clicks to the control server.
.claude/​rules/​control-api.md Documents Markdown boundaries and script lifecycle.

Review effort: Balanced

continue
}
guard bytes < MarkdownPage.maxImageBytes, pixels < MarkdownPage.maxImagePixels else { break }
guard case .success(let (probe, data)) = ImageProbe.load(path: image.path, maxBytes: MarkdownPage.maxImageBytes - bytes),
Comment thread agterm/Resources/markdown/render.js Outdated
Comment on lines +66 to +67
const first = item.firstChild && item.firstChild.nodeType === Node.TEXT_NODE ? item.firstChild
: item.firstElementChild && item.firstElementChild.tagName === 'P' ? item.firstElementChild.firstChild : null;
Comment thread agterm/Views/MarkdownProbe.swift Outdated
if let known { embedded.references[image.reference] = known }
continue
}
guard bytes < MarkdownPage.maxImageBytes, pixels < MarkdownPage.maxImagePixels else { break }
Comment thread agterm/Views/MarkdownProbe.swift Outdated
Comment on lines +51 to +52
guard bytes < MarkdownPage.maxImageBytes, pixels < MarkdownPage.maxImagePixels else { break }
guard case .success(let (probe, data)) = ImageProbe.load(path: image.path, maxBytes: MarkdownPage.maxImageBytes - bytes),
a loose list item starts with a newline text node, so its task marker stayed text;
the renderer now reads the item's first paragraph. A budget used up exactly ended
the embed loop and dropped later aliases of an embedded picture, so a spent budget
now skips new files only. At most 256 distinct picture files are probed per page,
failed probes included. The renderer separately caps displayed picture occurrences
at 256.
@umputun
umputun merged commit fb614c9 into master Nov 2, 2026
9 checks passed
@umputun
umputun deleted the markdown-link-overlay branch November 2, 2026 16:54
@tonglil

tonglil commented Nov 3, 2026

Copy link
Copy Markdown

@umputun I'm having a few issues with this.

  1. Unable to get cmd-click to work, only the agtermctl command. Is there something I need to configure for it?
  2. The tab completion doesn't seem to work for this agtermctl session overlay open --markdown (does not show files) nor agtermctl session overlay open (nothing)
  3. Also it doesn't seem to respect the session overlay at X% setting?
gif

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.

3 participants