Skip to content

Align <div>/<p> blocks in the Markdown viewer (#13735) - #13961

Draft
fbartho wants to merge 14 commits into
warpdotdev:masterfrom
fbartho:fb/13735-markdown-align-blocks-impl
Draft

fbartho wants to merge 14 commits into
warpdotdev:masterfrom
fbartho:fb/13735-markdown-align-blocks-impl

Conversation

@fbartho

@fbartho fbartho commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #13735

Adds horizontal alignment for <div align>/<p align> blocks in the Markdown viewer — shipping enabled, like the sibling raw-HTML features. This PR contains the approved spec (merged from #13906's branch, specs/GH13735/align/) plus the full implementation, per the spec→impl workflow; the spec PR #13906 is superseded by this one.

Recognizes align="left|center|right" and style="text-align:…" on own-line <div>/<p> blocks and single-line <p> captions, through one shared attribute-matching contract (case-insensitive names, quoting variants, style-wins conflict, a small text-align style subset, unrelated-attrs-ignored, malformed→literal, unrecognized-value→unaligned). Alignment is modeled as paired content-model markers (the ordered-list precedent from the #13345 review), orthogonal to block style, so "aligned + code block" composes and copy/export round-trips canonically. A feature flag was considered and intentionally not added: flags guard genuinely complex/risky subsystems (like GFM tables), and this is a bounded parser+paint change with deterministic fallbacks at every boundary.

Rendering: the GUI paints aligned regions via per-line horizontal offsets at paint time (the same lever tables use for cell alignment) — the offset reads the already-shaped line width and never touches the platform shaper's attribute round-trip. The TUI reads the same parser boundary stream and renders best-effort within terminal width with a left-aligned fallback.

Tested: parser (22 detector/contract cases), buffer lowering + round-trip, TUI arms, paint-offset math, and layout threading — markdown_parser 157, ipynb_parser 24, warp_editor 485, warp_tui 400; clippy -D warnings + fmt clean across all six touched crates.

Known limitations, deliberate: selection-highlight/caret rects inside aligned regions are not offset yet (visual-only in the read-only viewer; follow-up), and a partial re-layout starting mid-region degrades to unaligned until the next full rebuild (doesn't arise in the read-only viewer's whole-document rebuilds).

Verification document used for the screenshot
# `align` blocks verification

Each labeled case below. (Captured during development with the then-flag forced on; the feature now ships enabled.)

HERO (issue's case — centered div, heading + literal img):

<div align="center">

## Project Hero

<img src="assets/logo.svg" alt="logo">

</div>

CAPTION (single-line centered p):

<p align="center">A centered caption under a figure.</p>

RIGHT-ALIGNED block:

<div align="right">

This paragraph should hug the right edge.

</div>

STYLE VARIANT (text-align via style):

<p style="text-align: center">Centered via style attribute.</p>

MALFORMED (broken quote — must stay literal):

<p align="center>broken attribute quoting stays literal</p>

CONTROL (no alignment):

Plain left-aligned paragraph for comparison.
Screenshot of basic block alignment

fbartho added 13 commits July 14, 2026 14:44
…w precedent (GH13735)

Model block alignment as start/end BufferText markers plus a SumTree
dimension (ordered-list precedent), not a FormattedTextLine::AlignedBlock
wrapper. The wrapper collapses into a BufferBlockStyle when lowered into
the editor content model, which violates bnavetta's warpdotdev#13345 constraint that
only one BufferBlockStyle is active per character (aligned + code block
can't compose). Demote the wrapper to a considered-and-rejected alternative.

Also: add TUI surface disposition (best-effort within terminal width,
left-aligned fallback) as an explicit scope decision; mark warpui_core::Align
as a GUI-only paint strategy rather than the content model; fix the feature-
gating path to crates/editor/src/content/buffer.rs:850-855; rewrite the Risks
blast radius honestly for the marker design (bounded BufferText match sites)
versus the rejected wrapper (20 files matching FormattedTextLine, incl. the
non-obvious ipynb_parser and warp_tui); cite bnavetta's canonical round-trip
ruling on invariant 6.
Specify the concrete parser-to-buffer contract left open in the align spec:
region boundaries ride the FormattedTextLine stream as two content-less
variants (AlignRegionStart(BlockAlignment)/AlignRegionEnd) that the core.rs
edit lowering loop translates into the paired top-level BufferText align
markers, mirroring the ordered-list precedent end to end. Boundary lines stay
orthogonal to BufferBlockStyle, preserving the composition constraint.

Resolve the TUI inconsistency: because boundaries are in the FormattedTextLine
stream, the TUI reads them directly from the exhaustive match it already
consumes, with no buffer/SumTree involvement. Correct the blast-radius note to
own the added FormattedTextLine arms honestly.
Specify the single-line <p align> detection path and its literal
fallback for mixed same-line text; gate the feature at the parser
crate (mirroring the MarkdownTables bool-threading mechanism through
parse_markdown_impl) so GUI and TUI share one gate; define the
text-align style micro-grammar precisely.
…blocks (GH13735)

Address round-3 review findings on PR warpdotdev#13906.

Feature-gating: the existing parse_markdown / parse_markdown_with_gfm_tables
entry-point pair encodes a single binary choice and cannot express the state
where GFM tables and block alignment are both enabled. Replace the second-boolean
evolution with a MarkdownParseOptions { gfm_tables, block_align } struct threaded
through one shared parse_markdown_impl, thin public wrappers, and a
parse_markdown_with_options entry point; every call site populates both fields
from its FeatureFlag checks so all four combinations are well-defined.

Attribute contract: add a single Attribute-matching contract subsection that both
detector paths (own-line and single-line) cite, covering case-insensitive
attribute names, quoted/unquoted values, align= and style= extraction, the
align-vs-style conflict winner, ignoring unrelated attributes, and malformed
syntax falling back to a literal tag. Fix the stale design-question-7 reference,
tie the Security section to the contract, and add tests for quoting variants,
name case-insensitivity, unrelated attributes, and the malformed-vs-unrecognized
boundary.
Introduce content-less FormattedTextLine::AlignRegionStart(BlockAlignment)
/ AlignRegionEnd boundary variants and a detector for own-line
<div align>/<p align> and single-line <p align> blocks. Both detector
paths funnel through one shared attribute-matching contract (case-
insensitive names, quoting variants, style-wins conflict rule, style
text-align micro-grammar, unrelated attrs ignored, malformed-to-literal,
unrecognized-value-to-unaligned).

Evolve parse_markdown_impl to take a MarkdownParseOptions { gfm_tables,
block_align } struct so tables and block-align compose, keeping the
existing parse_markdown / parse_markdown_with_gfm_tables wrappers and
adding parse_markdown_with_options. The detector only runs when
block_align is set, so the boundary variants are never emitted otherwise.
…dev#13735)

Add FeatureFlag::MarkdownBlockAlign (ships dark) and re-export
MarkdownParseOptions / parse_markdown_with_options from the crate root.
Widen ipynb_to_formatted_text to take MarkdownParseOptions instead of a
gfm_tables bool, re-checking both feature flags the same way, so the
Jupyter and Markdown surfaces stay in lockstep on both features.
…otdev#13735)

Add paired top-level BufferText::Align(AlignMarker) markers modeled on
the BufferText::Link precedent: a StyleSummary align counter, an
AlignCount SumTree dimension, and an alignment_at_align_count query so a
character's enclosing alignment is a SumTree lookup orthogonal to its
block style. The core.rs edit loop translates AlignRegionStart/End
boundary lines into the paired markers; find.rs ignores them (zero-width)
and validation.rs balances them.

Serialization brackets the region on the way out: StyledBufferBlocks
yields a zero-width AlignBoundary block that re-emits AlignRegionStart/End
lines (semantic round-trip), a <div align> wrapper in the markdown string
form, and a <div align> element in HTML export. to_markdown now starts at
offset 0 so a leading region marker is not clipped. Round-trip test
asserts alignment survives markdown -> buffer -> markdown.
Read AlignRegionStart/AlignRegionEnd boundary lines directly from the
FormattedTextLine stream, tracking a stack of active alignments so the
nearest enclosing region governs each interior line (innermost wins).
Center/Right lines are wrapped in a full-width flex row with flex spacers
that absorb the leftover pane width; when content fills the pane the
spacers collapse and it degrades to left-aligned, the spec's best-effort
positioning. Tests cover center, right, and the left-align fallback.
…3735)

Thread the MarkdownBlockAlign flag into the plan-view parse entry point
(the remaining call site that selects a Markdown parser), and apply
rustfmt to the widened ipynb cell_lines signature.
Thread the enclosing region's alignment through the layout pipeline onto
the render-model Paragraph, then apply it as a per-line horizontal offset
in draw_text. The layout pass tracks the alignment by walking the
AlignBoundary blocks in the styled-block stream (innermost region wins),
attaches it to each laid-out block via BlockItem::set_alignment, and the
paint path shifts each already-shaped line within the block's available
width (align_line_offset). Left is a zero offset, so unaligned content is
bit-identical to before; content that fills the pane degrades to
left-aligned, matching the TUI's best-effort behavior.

The offset rides block-level paint positioning: it reads the already-
shaped line's width and shifts the paint origin, never crossing the mac
text shaper's CFAttributedString encode/decode (that happens upstream
during layout), so it is immune to the attribute round-trip class of bug.

Since alignment is resolved by stream-walking rather than a positional
query, drop the speculative SumTree align dimension and cursor query
(AlignCount, alignment_at_align_count, is_aligned, and the StyleSummary
align counters) added with the buffer layer; the zero-width markers only
need the text-summary and validation bookkeeping that remains.

Tested: align_line_offset offset math (left/center/right/no-slack) and a
layout-level assertion that set_alignment threads onto the paragraph.
@cla-bot cla-bot Bot added the cla-signed label Jul 19, 2026
@github-actions github-actions Bot added the external-contributor Indicates that a PR has been opened by someone outside the Warp team. label Jul 19, 2026
…tdev#13735)

Remove FeatureFlag::MarkdownBlockAlign and all gating: the align-region
detector now always runs inside the parser, so every entry point emits
align regions. A flag is reserved for genuinely complex or risky features
like MarkdownTables; block alignment is a bounded parser-plus-paint change
with deterministic fallbacks, matching the unflagged raw-HTML-subset
siblings (br/kbd/sub/sup) that ship enabled.

Revert MarkdownParseOptions back to the plain gfm_tables bool. The struct
existed only to compose two independent feature flags; with block_align
ungated there is no second flag to compose, so it would be a single-field
wrapper around a bool — the plain boolean is the right shape by the code's
grain. Drop parse_markdown_with_options; restore the parse_markdown /
parse_markdown_with_gfm_tables pair and the gfm_tables: bool threading
through buffer.rs, ipynb_parser, and the TUI plan view.

Drop the flag-gating tests (flag-off emits-no-region, the override_enabled
in the round-trip test); every other test held since the always-on
behavior was already the tested path. Update the spec's design question 7
and risks to state the feature ships enabled, with the rationale.
@fbartho

fbartho commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Pausing alignment until the img tag feature #13721 ships, as that was a core object I wanted to align.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed external-contributor Indicates that a PR has been opened by someone outside the Warp team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown viewer: support the align attribute on <p>/<div> blocks (center/right/left)

1 participant