Skip to content

Let each thematic-break marker carry its own look - #160

Merged
luca-chen198 merged 1 commit into
mainfrom
feat/star-thematic-break
Aug 21, 2026
Merged

luca-chen198 merged 1 commit into
mainfrom
feat/star-thematic-break

Conversation

@Nicolas-Py

Copy link
Copy Markdown
Member

What

MarkdownEditorConfiguration.thematicBreak (ThematicBreakStyle) maps each of the three thematic-break markers to an optional mark string. Nil — the default for all three — draws today's full-width rule, so every existing embedder is byte-identical to before. A non-nil mark is drawn centred in the text container in the body font.

config.thematicBreak.asteriskMark = "* * *"   // novel-style scene break on ***
// --- and ___ stay rules

Presentation only: the source text is untouched, the caret still reveals the raw ***, and the construct still exports as <hr>. A document written this way reads correctly in any other editor — which is the whole point of not inventing syntax for it.

Why this isn't a new grammar case

CONTRIBUTING.md says new constructs are extensions, not core grammar. This is neither a new construct nor new grammar: *** is already a CommonMark thematic break, already classified, already styled, already drawn. The marker character was always parsed by isThematicBreak and then discarded. This keeps it.

The extension seam cannot express it, for three independent reasons: BlockParser classifies thematic breaks at line 312, long before extension block entries are consulted at 345 (and "built-in constructs always classify first" is a stated invariant); BlockSyntax is a fence with an opening and closing line, while a break is one line with no content; and the protocol has no drawing hook at all. The directive seam is inline-only, so it cannot claim a bare block line either. That leaves the documented exception for overlay-rendered constructs.

Three things this deliberately does NOT do

No marker payload on BlockKind/BlockNode. Ctx already carries both the string and the configuration, and the marker is by construction the line's first non-whitespace character. Threading a payload would have forced edits through the parser, the AST, the tokenizer and two test files to move information its only consumer can already see. All 344 pre-existing tests compile unmodified as a result.

The .thematicBreak attribute keeps its Bool value. All three readers type-test it as as? Bool off Any (MarkdownTextLayoutFragment :192, :494, :526). A changed value type compiles clean, returns nil at every one, and silently blanks every rule in the document — with no compiler diagnostic and no test to catch it. The mark rides on a second key instead, keeping every attribute value ObjC-bridgeable like the other fifteen.

The grammar is untouched. A spaced * * * remains a list item, as before. That is a real deviation from CommonMark (which calls it a thematic break) and worth fixing, but it changes how existing documents render, so it belongs in its own change.

Testing

drawThematicBreaks splits into thematicBreakDecorations(at:) plus a thin draw loop, mirroring blockBackgroundFills(at:), so the geometry is assertable headlessly.

12 new tests in ThematicBreakMarkTests: marker resolution per slot, indented breaks, longer runs, caret suppression, trailing-newline bounds, attribute value types, and geometry (rule spans the container; mark is narrower and centred within 0.01pt). This is net-new coverage — nothing previously read what the styler emits for a thematic break.

459 tests in 66 suites pass on this branch. Verified against a real embedder (Nodes) building and running its own suite of 558 against this working tree.

Generated with Claude Code

@Nicolas-Py
Nicolas-Py force-pushed the feat/star-thematic-break branch from f6eee03 to 78945f1 Compare August 18, 2026 07:56
CommonMark gives `---`, `***` and `___` one meaning and one rendering, so an
embedder who wants a novel-style star divider on `***` has had to invent
syntax for it -- and any syntax they invent stops being a thematic break
everywhere else. The marker character was already parsed and then discarded;
keeping it costs nothing and buys the distinction for free.

`MarkdownEditorConfiguration.thematicBreak` maps each of the three markers to
an optional mark string. Nil, the default for all three, draws today's
full-width rule, so every existing embedder is unaffected. A non-nil mark is
drawn centred in the text container in the body font. Presentation only: the
source text is untouched, the caret still reveals the raw `***`, and the
construct still exports as `<hr>`.

Three things this deliberately does NOT do. The marker is recovered in the
styler from the line's first non-whitespace character rather than carried as
an associated value on `BlockKind`/`BlockNode` -- `Ctx` already holds both the
string and the configuration, and the payload would have forced edits through
the parser, the AST, the tokenizer and two test files to move information its
only consumer can already see. The `.thematicBreak` attribute keeps its `Bool`
value and the mark rides on a second key, because all three readers type-test
it as `as? Bool` off `Any`: a changed value type compiles clean, returns nil
everywhere, and silently blanks every rule in the document with no test to
catch it. And the grammar is untouched, so a spaced `* * *` remains a list
item as before -- correcting that is a separate change with its own blast
radius.

`drawThematicBreaks` splits into `thematicBreakDecorations(at:)` plus a thin
draw loop, mirroring `blockBackgroundFills(at:)`, so the geometry is
assertable headlessly. That is new coverage: nothing previously read what the
styler emits for a thematic break.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Nicolas-Py
Nicolas-Py force-pushed the feat/star-thematic-break branch from 78945f1 to 6f9b98f Compare August 18, 2026 08:27

@luca-chen198 luca-chen198 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

really nice

@luca-chen198
luca-chen198 merged commit 682501e into main Aug 21, 2026
1 check passed
wildthink pushed a commit to wildthink/swift-markdown-engine that referenced this pull request Sep 24, 2026
…nt .image sizing

The rest-of-the-call run's kern was computed for the whole run's width but
applied per-character by AppKit, over-negating an n-character run by
(n-1) widths — the same poisoning of usageBoundsForTextContainer the
table work hit and fixed. Reuse that fix's hiddenRunKern helper.

Also document that .image, unlike .symbol/.text, is drawn at its own
size rather than fit to the inherited font, so an oversized image is the
supplying directive's responsibility — existing tests (DirectiveGlyphTests)
already rely on a directive controlling its own image size (@Swatch), so
clamping or growing the line here would break an intentional feature
rather than a bug.

Also merges upstream/main (0.13.0 release + nodes-app#146/nodes-app#152/nodes-app#153/nodes-app#160-nodes-app#162) so
the changelog entry lands under a fresh [Unreleased] section instead of
inside the already-released [0.13.0].

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
luca-chen198 pushed a commit that referenced this pull request Sep 29, 2026
* Draw a self-contained directive as a glyph

Phase 3 of the directive seam. A self-contained call has no body, so until now
it parsed, claimed its span, and then rendered as its own literal source —
`@pagebreak` looked exactly like the text `@pagebreak`. This gives it
something to draw.

A directive returns a `DirectivePresentation` — an SF Symbol, replacement
text, or an NSImage — and the styler collapses the source behind it. The
mechanism is the one inline LaTeX already uses, not a new one: the characters
stay in the storage, the first carries the image plus enough kern to occupy
its width, the rest collapse to zero width via clear colour and the shrunk
marker font. `MarkdownTextLayoutFragment` draws it.

That is what keeps "markers shrink, they don't disappear" true here. Selection,
find, copy, and undo all still see the real characters, and the caret entering
the call reveals the source muted — the same flip every other construct does.

Failure is visible rather than silent: `.literal`, or a symbol name the system
doesn't know, leaves the source on screen instead of collapsing it to a gap the
user can neither see nor fix.

Rasterised glyphs are cached in an NSCache keyed by everything that determines
the pixels — presentation re-runs for every visible directive on every
keystroke, and rasterising text each time is the one part of this path
expensive enough to matter.

The parser is untouched: this is styling only, and the 4000-input corpus
fingerprint is unchanged. `DirectiveScanner`'s diff is comment-only — it
already emitted the geometry this needs, and its comments pointed forward to
this change.

DirectiveStylingTests asserted that a self-contained call renders as literal
text with nothing collapsing it. That was Phase 1 stating its own limit, and
it is exactly what this changes, so it becomes a collapse/reveal pair rather
than being deleted.

`FontDirective` and `ColorDirective` are both containers and draw no glyph, so
the engine still ships no self-contained directive. Demo/ gains `@icon`,
`@flag`, `@emoji`, and `@pagebreak` as embedder-side examples — curated data
and print semantics are app concerns. `@flag` computes its glyph from
regional-indicator scalars and carries no dataset.

463 tests pass, demo builds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(directives): per-character kern on the collapsed rest-run, document .image sizing

The rest-of-the-call run's kern was computed for the whole run's width but
applied per-character by AppKit, over-negating an n-character run by
(n-1) widths — the same poisoning of usageBoundsForTextContainer the
table work hit and fixed. Reuse that fix's hiddenRunKern helper.

Also document that .image, unlike .symbol/.text, is drawn at its own
size rather than fit to the inherited font, so an oversized image is the
supplying directive's responsibility — existing tests (DirectiveGlyphTests)
already rely on a directive controlling its own image size (@Swatch), so
clamping or growing the line here would break an intentional feature
rather than a bug.

Also merges upstream/main (0.13.0 release + #146/#152/#153/#160-#162) so
the changelog entry lands under a fresh [Unreleased] section instead of
inside the already-released [0.13.0].

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Jason Jobe <box2019@jasonjobe.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
luca-chen198 pushed a commit that referenced this pull request Oct 4, 2026
* Draw a self-contained directive as a glyph

Phase 3 of the directive seam. A self-contained call has no body, so until now
it parsed, claimed its span, and then rendered as its own literal source —
`@pagebreak` looked exactly like the text `@pagebreak`. This gives it
something to draw.

A directive returns a `DirectivePresentation` — an SF Symbol, replacement
text, or an NSImage — and the styler collapses the source behind it. The
mechanism is the one inline LaTeX already uses, not a new one: the characters
stay in the storage, the first carries the image plus enough kern to occupy
its width, the rest collapse to zero width via clear colour and the shrunk
marker font. `MarkdownTextLayoutFragment` draws it.

That is what keeps "markers shrink, they don't disappear" true here. Selection,
find, copy, and undo all still see the real characters, and the caret entering
the call reveals the source muted — the same flip every other construct does.

Failure is visible rather than silent: `.literal`, or a symbol name the system
doesn't know, leaves the source on screen instead of collapsing it to a gap the
user can neither see nor fix.

Rasterised glyphs are cached in an NSCache keyed by everything that determines
the pixels — presentation re-runs for every visible directive on every
keystroke, and rasterising text each time is the one part of this path
expensive enough to matter.

The parser is untouched: this is styling only, and the 4000-input corpus
fingerprint is unchanged. `DirectiveScanner`'s diff is comment-only — it
already emitted the geometry this needs, and its comments pointed forward to
this change.

DirectiveStylingTests asserted that a self-contained call renders as literal
text with nothing collapsing it. That was Phase 1 stating its own limit, and
it is exactly what this changes, so it becomes a collapse/reveal pair rather
than being deleted.

`FontDirective` and `ColorDirective` are both containers and draw no glyph, so
the engine still ships no self-contained directive. Demo/ gains `@icon`,
`@flag`, `@emoji`, and `@pagebreak` as embedder-side examples — curated data
and print semantics are app concerns. `@flag` computes its glyph from
regional-indicator scalars and carries no dataset.

463 tests pass, demo builds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(directives): per-character kern on the collapsed rest-run, document .image sizing

The rest-of-the-call run's kern was computed for the whole run's width but
applied per-character by AppKit, over-negating an n-character run by
(n-1) widths — the same poisoning of usageBoundsForTextContainer the
table work hit and fixed. Reuse that fix's hiddenRunKern helper.

Also document that .image, unlike .symbol/.text, is drawn at its own
size rather than fit to the inherited font, so an oversized image is the
supplying directive's responsibility — existing tests (DirectiveGlyphTests)
already rely on a directive controlling its own image size (@Swatch), so
clamping or growing the line here would break an intentional feature
rather than a bug.

Also merges upstream/main (0.13.0 release + #146/#152/#153/#160-#162) so
the changelog entry lands under a fresh [Unreleased] section instead of
inside the already-released [0.13.0].

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* Complete directive names and argument values

Phase 4, the last of the seam. Autocomplete for directive NAMES (`@fo`) and
for their ARGUMENT VALUES (`@icon(sta`, `@flag(jap`), riding the seam the
[[wiki-link]] picker already established: the engine detects the trigger,
ranks the candidates, reports the anchor rect, and routes up/down/return/
escape. The embedder draws the list — no picker UI ships in the engine.

Autocomplete cannot read the AST. Mid-typing, `@ico` and `@icon(sta` are
exactly what the parser REJECTS — no body, no closing paren — which is correct
for styling and useless for completion. So `DirectiveCompletionScanner` is a
separate, forgiving backwards scan over the current line, bounded to 256
characters per caret move. It reuses the parser's boundary rule, so it can
never offer a directive the parser would then refuse.

The engine owns the candidates because it owns the registry. Values come from
`MarkdownDirective.valueCompletions(for:prefix:)`, whose default already
answers anything the declared schema can — closed keyword sets and booleans —
so a directive implements it only when its domain is dynamic or too large to
declare. That is what makes a `@flag`-style command clean rather than special:
the schema was already there. A newly registered directive appears in the
picker with no embedder change.

The commit path is deliberately NOT `applyInlineReplacement` — that runs the
wiki-link storage/display transform and stamps `.wikiLinkID`, neither of which
means anything here.

Pickers stay shut where they must: inside code spans and fenced blocks, on a
selection rather than a caret, when not typing, mid-IME composition, in raw
source mode, and after an email address.

Changes to existing files are 47 lines across 3; detection, commit, scanning,
and the completion types live in their own files. Demo gains a caret-anchored
picker (~60 lines) serving both names and values through one context type.

488 tests pass, demo builds.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(directives): caret-bounded replacement range, Enter capture, gate coverage

Three real issues from review, plus smaller ones flagged alongside:

1. Replacement range only covered marker-to-caret (name) or valueStart-to-
   caret (argument value), so a pick made mid-token left the characters
   after the caret behind: `@fo|nt` picking "font" produced
   `@font(size: ){}nt`. Both ranges now extend to the actual end of the
   token, scanning forward past the caret the same way the existing
   backward scan already does.

2. Enter was captured as "confirm" outside anything a user would
   recognize as mid-completion: a bare `@` before ordinary prose opened
   a picker with every registered directive (this branch's own test
   asserted that), and a fully-typed self-contained call like
   `@pagebreak` kept its context open once finished. Both now return no
   context — a bare marker until at least one character is typed, and an
   exact match to a directive needing no further arguments. Updated
   DirectiveCompletionTests accordingly (the old "bare marker offers
   every directive" assertion was itself testing the bug).

3. Added coordinator-level tests for every gate the PR description
   claimed was tested: code span, fenced block, non-empty selection, not
   typing, mid-IME composition, raw source mode. Only the email-address
   boundary rule (a scanner-level concern) actually had one before.

Smaller:
- Name candidates now come from the registry's winning entries per
  marker, not the raw configured directive list, so a directive dropped
  for an empty name or a name that lost "first registration wins" can no
  longer be offered by the picker.
- The snippet's `|` caret marker is now measured in UTF-16 units instead
  of Characters, since it's added onto an NSRange location — a snippet
  carrying a non-BMP character before the marker previously landed the
  caret mid-surrogate.
- A wiki-link/image-embed context claiming the caret this pass now
  suppresses the directive picker instead of letting both go live at
  once with no way for an embedder's key handler to tell them apart.

Full suite (561 tests) and the Demo target build both clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(directives): stop name picks duplicating existing calls, tighten the forward name scan, gate on a wired handler, unwrap quoted values

Review round 2 on #159, trial-merged against current main:

- A name pick on `@fo|nt(size: 18){x}` was appending its full snippet
  after the existing call instead of just replacing the name, duplicating
  the arguments and body. When a `(` or `{` already follows the name,
  insert just the marker and name.
- The forward name scan accepted `.` unconditionally, unlike the parser
  (which only continues a name across a dot when an identifier-start
  character follows). This both absorbed a trailing sentence period into
  the replacement range and, for an exact match, kept the picker open
  since the exact-match check only fires when the scan ends at the caret.
- `updateDirectiveCompletion` set `isDirectiveCompletionActive` whenever a
  context existed, even with no `onDirectiveCompletion` handler wired up —
  an embedder that registered directives but never adopted completion
  could have its wiki-link keys silently swallowed. Gate on the handler
  being set.
- Smaller: a `.container` directive with only optional parameters
  (`@font`) closed the picker on an exact match even though its body is
  still required; a quoted value (`@glyph("sta|r")`) filtered against the
  quote character itself and matched nothing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Jason Jobe <box2019@jasonjobe.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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.

2 participants