Skip to content

Add support for HTML <details> blocks - #66

Open
shmuelzon wants to merge 2 commits into
b451c:mainfrom
shmuelzon:feat/html-details
Open

shmuelzon wants to merge 2 commits into
b451c:mainfrom
shmuelzon:feat/html-details

Conversation

@shmuelzon

Copy link
Copy Markdown
Contributor

feat: <details> blocks collapse and expand

<details> / <details open> with a <summary> (on its own line, on the
opening tag's line, or over several lines) is a collapsible section: the
summary row has a disclosure triangle (GitHub's filled one), and a click
on the triangle or the caption shows or hides the Markdown blocks inside.
Both show the pointing hand and say "Expand" or "Collapse". Nesting works;
a <details> without a summary reads "Details"; an unclosed one runs to
the end of the document; a stray </details> draws nothing. Simple inline
tags in a summary (<b>, <i>, <code>, ...) become Markdown, others
are dropped. Print and PDF show every section open.

Design, so more HTML tags can follow:

  • HTMLBlockSyntax has a generic tag lexer and a registry of container
    tags (containerTags + HTMLContainerKind); <details> is its first
    entry.
  • The parser keeps one flat block list and tags each block with the
    containers it is inside (MarkdownBlock.containers); the summary row
    is a text block with opensContainer, so it is measured, searched,
    selected and copied like any text. Its caption carries a toggle link.
  • The row is [triangle][caption]: the measurer wraps the caption at the
    column less the triangle's (BlockLayout.ContainerSummary), and the
    triangle is a small AppKit view beside the text view, never over it,
    so it owns its click, cursor and tooltip.
  • The view hands the list a ContainerPresentation: the blocks of
    closed containers left out, the height table cut to match and the
    spacing after a hidden run derived again. Search, the table of contents
    and anchors still see every block; jumping to a hidden one opens the
    containers around it first. Toggles survive a re-parse.

A line starting with `<!--` (CommonMark HTML block kind 2) and every line
up to the one with `-->` draws nothing, as on GitHub; an unclosed comment
runs to the end of the document. Definitions inside a comment are not
taken. Inline `<!-- … -->` in a paragraph, heading or cell is dropped too
(one space kept between the words around it); an unclosed one stays text.

HTMLBlockSyntax.swift is the new home of block-level HTML recognizers.
`<details>` / `<details open>` with a `<summary>` (on its own line, on the
opening tag's line, or over several lines) is a collapsible section: the
summary row has a disclosure triangle (GitHub's filled one), and a click
on the triangle or the caption shows or hides the Markdown blocks inside.
Both show the pointing hand and say "Expand" or "Collapse". Nesting works;
a `<details>` without a summary reads "Details"; an unclosed one runs to
the end of the document; a stray `</details>` draws nothing. Simple inline
tags in a summary (`<b>`, `<i>`, `<code>`, ...) become Markdown, others
are dropped. Print and PDF show every section open.

Design, so more HTML tags can follow:
- HTMLBlockSyntax has a generic tag lexer and a registry of container
  tags (`containerTags` + `HTMLContainerKind`); `<details>` is its first
  entry.
- The parser keeps one flat block list and tags each block with the
  containers it is inside (`MarkdownBlock.containers`); the summary row
  is a text block with `opensContainer`, so it is measured, searched,
  selected and copied like any text. Its caption carries a toggle link.
- The row is [triangle][caption]: the measurer wraps the caption at the
  column less the triangle's (`BlockLayout.ContainerSummary`), and the
  triangle is a small AppKit view beside the text view, never over it,
  so it owns its click, cursor and tooltip.
- The view hands the list a `ContainerPresentation`: the blocks of
  closed containers left out, the height table cut to match and the
  spacing after a hidden run derived again. Search, the table of contents
  and anchors still see every block; jumping to a hidden one opens the
  containers around it first. Toggles survive a re-parse.
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