Skip to content

Html Inline semantics#151

Open
dillonkearns wants to merge 15 commits intohtml-fallbackfrom
html-inlines
Open

Html Inline semantics#151
dillonkearns wants to merge 15 commits intohtml-fallbackfrom
html-inlines

Conversation

@dillonkearns
Copy link
Copy Markdown
Owner

Inline HTML children are now typed as Inline

Previously, HtmlInline contained Html Block, which meant inline HTML children were block-level. This caused things like <sup>2</sup> to wrap its content in a <p> tag.

  type Inline
-     = HtmlInline (Html Block)
+     = HtmlInline (Html Inline)

So The area is 5km<sup>2</sup>. now renders as you would expect:

- <p>The area is 5km<sup><p>2</p></sup>.</p>
+ <p>The area is 5km<sup>2</sup>.</p>

If you pattern match on HtmlInline, the children change from List Block to List Inline.

Raw body capture on HtmlElement

HtmlElement now includes the raw source text between the opening and closing tags as a String field. This lets you preserve content verbatim for tags like <style> or <script>.

  type Html children
-     = HtmlElement String (List HtmlAttribute) (List children)
+     = HtmlElement String (List HtmlAttribute) (List children) String

You can access it in a renderer with withRawContent:

Markdown.Html.tag "style"
    (\rawBody children ->
        Html.node "style" [] [ Html.text rawBody ]
    )
    |> Markdown.Html.withRawContent

If you pattern match on HtmlElement, add the extra String field:

  case html of
-     HtmlElement tag attrs children ->
+     HtmlElement tag attrs children raw ->

Positional heuristic for inline vs block HTML

Single-line HTML tags at the start of a line now become inline when they are adjacent to paragraph text. Multi-line tags (closing tag on a different line) always become blocks.

Examples:

She speaks.
<acerola>Hello!</acerola>
-- Before: two blocks
[ Paragraph [ Text "She speaks." ]
, HtmlBlock (HtmlElement "acerola" [] ...)
]

-- After: one paragraph
[ Paragraph
    [ Text "She speaks.\n"
    , HtmlInline (HtmlElement "acerola" [] [ Text "Hello!" ] "Hello!")
    ]
]

A blank line keeps it as a block:

<foo>bar</foo>

baz
[ HtmlBlock (HtmlElement "foo" [] ...)
, Paragraph [ Text "baz" ]
]

Multi-line HTML always becomes a block, even if it interrupts a paragraph:

She speaks.
<foo>
Hello!
</foo>
[ Paragraph [ Text "She speaks." ]
, HtmlBlock (HtmlElement "foo" [] [ Paragraph [ Text "Hello!" ] ] "\nHello!\n")
]

Inline HTML is single-line only. A tag that starts mid-line and spans multiple lines will not parse as an HTML element (just as the raw text of the HTML). This avoids the confusing case where adding a newline inside a tag silently breaks parsing, similar to how MDX handles this with a parser error.

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