Skip to content

feat(text): custom HTML block extension point (html_block_parser / html_block_renderer) - #4005

Open
spartajet wants to merge 3 commits into
longbridge:mainfrom
spartajet:mailview-ext
Open

spartajet wants to merge 3 commits into
longbridge:mainfrom
spartajet:mailview-ext

Conversation

@spartajet

Copy link
Copy Markdown

Summary

Mirrors the existing Markdown block parser/renderer pattern for the HTML format:

  • TextView::html_block_parser — a parser sees every RcDom element before built-in tag handling; returning a MarkdownNode claims the element as a BlockNode::Custom, None falls through to built-in handling.
  • TextView::html_block_renderer — registers a renderer by node name; custom HTML blocks share the name-keyed registry (and BlockState selection/copy plumbing) with custom Markdown blocks.
  • Html documents re-parse only when html block parsers change; render-handle refreshes remain no-op (Arc::ptr_eq comparison).

Motivation

HTML email (and similar rich-HTML sources) needs blocks the built-in HTML model does not cover (e.g. tables with colspan/rowspan). The markdown side already solves this with markdown_block_parser/markdown_block_renderer; this gives HTML sources the same extension path without touching built-in rendering.

Testing

  • End-to-end test html_block_parser_claims_elements_before_builtin_handling: parser claims <div data-mb>, renderer draws a marker, typed payload survives (node.data::<usize>()).
  • Full gpui-base suite passes (1735 tests) — the hook is inert unless a parser is registered.

…ck extension point

Mirror the markdown block parser/renderer pattern for the HTML format: a parser sees every RcDom element before built-in tag handling and claims it as a BlockNode::Custom; renderers share the name-keyed registry with markdown custom blocks. Html documents re-parse when html block parsers change (render-handle refreshes remain no-op via ptr_eq comparison). End-to-end test: parser claims <div data-mb>, renderer draws a marker, typed data survives.
…tensions pre-seeds the initial parse

Render methods rebuild equivalent parser closures every frame — a ptr_eq comparison would force a re-parse per frame. Mirrors the markdown block-parser shape check (length-only). html_with_extensions registers extensions before the initial parse so custom HTML block parsers apply to the first parse without an element-driven re-parse (no first-frame flash).
@huacnlee

Copy link
Copy Markdown
Member

❌ Changes requested: the use case is reasonable, but the reparse check reparses HTML on every render, and the public API surface should be smaller.

Reviewed SHA: c29c7fb

Findings

  1. Reparse on every render. crates/base/src/text/markdown_ext.rs:487 (has_same_html_block_parsers) compares parsers with Arc::ptr_eq. TextView is normally rebuilt in render(), as your own test does (text_view.rs:2951), so each frame wraps a new closure in a new Arc. That makes state.rs:418 report a change and call increment_update on every draw, so the whole HTML document is reparsed every frame. That is a performance regression for large HTML such as mail. The Markdown path avoids this by comparing counts and parser_revision (markdown_ext.rs:435). Please use the same approach here, or fold HTML parsers into has_same_parser_configuration. Please also add a test that renders twice and asserts the document is parsed only once.
  2. Redundant public method. TextView::html_block_renderer (text_view.rs:702) only forwards to markdown_block_renderer and writes to the same registry. Two names for one registry are a parallel vocabulary. Please drop it and document that markdown_block_renderer also renders custom HTML blocks. The same applies to the second pub entry, MarkdownExtensions::push_html_block_parser (markdown_ext.rs:477): keep only the TextView builder unless plugins really need it.
  3. Third-party type in public API. HtmlBlockParserFn and html_block_parser take a &markup5ever_rcdom::Node, which is not re-exported. Callers must depend on the exact markup5ever_rcdom 0.3 version, and any future bump becomes a breaking change for gpui-kit. Markdown has the same pattern with mdast, so this is not blocking alone. Still, please re-export the crate (or Node), or note the coupling in the docs.
  4. Parser runs on non-content elements. format/html.rs:475 offers every element to the parser, including html, head, body and table internals. If a parser claims body, the whole document is replaced. Please document that parsers should only match the elements they own. Better, skip the document and head nodes.
  5. PR title. This repo uses component: Summary, not feat(text):. Please rename it to something like text: Add custom HTML block parser. The description matches the diff.

Ran

  • cargo check -p gpui-component: ok
  • cargo test -p gpui-base --lib text::: 457 passed, including the new html_block_parser_claims_elements_before_builtin_handling

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee

Copy link
Copy Markdown
Member

❌ The per-render reparse is fixed, but the other points from the last review are still open.

Reviewed at fa3ef2e (delta from c29c7fb).

  • Fixed: has_same_html_block_parsers is now length-only like the Markdown block-parser check, so rebuilt closures no longer reparse every frame.
  • New public API: TextViewState::html_with_extensions. Please avoid a second constructor; register extensions through the existing builder path and parse lazily, or keep this pub(crate).
  • Still open: the redundant html_block_renderer alias, the extra pub push_html_block_parser, markup5ever_rcdom::Node exposed in the public signature without a re-export, parsers being able to claim body/head, and the PR title (use text: ..., not feat(text):).
  • Ran: cargo test -p gpui-base --lib text:: 457 passed.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee huacnlee 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.

check review

Incorporate the existing command rendering and other CI fixes without changing the extension API.

Agent: rust-maintainer
@huacnlee

Copy link
Copy Markdown
Member

Merged current main in 1a166550 to incorporate the existing CI fixes. The merge is conflict-free and leaves the custom HTML extension API unchanged.

Linux validation: check passes for Base, Component and Shell; all 465 Base text tests and all 124 Shell integration tests pass, including the previously failing retained_command_typed_entries_query_and_confirm_callbacks_are_native; strict all-target Clippy for Base and Component with test-support passes. The outstanding API review requests remain open.

🤖 Noop (rust-maintainer) · AI agent acting for @huacnlee

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