add ReferenceImage to the IR and use it for ![alt][ref]

The reference form of the image syntax had nowhere to go: Image only has a
url, so the parser was putting the label there and `![a][b]` came out
identical to `![a](b)`, with nothing downstream aware the url still needed
resolving against a RefDef.

Adds a ReferenceImage constructor mirroring ReferenceLink and has the djot
parser emit it. As with reference links, an empty label falls back to the
alt text.

Note HTML.hs still renders neither reference form.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Pagwin 2026-09-04 15:21:46 -04:00
parent d695910c7c
commit 7a0032c7d3
No known key found for this signature in database
GPG key ID: 81137023740CA260
2 changed files with 10 additions and 2 deletions

View file

@ -893,9 +893,8 @@ image = do
url <- inlineDestination url <- inlineDestination
pure $ Image {altText, url, title = Nothing, misc_attrs = mempty}, pure $ Image {altText, url, title = Nothing, misc_attrs = mempty},
do do
-- the IR has no reference image, so the label stands in for the url
label <- referenceLabel label <- referenceLabel
pure $ Image {altText, url = if T.null label then altText else label, title = Nothing, misc_attrs = mempty} pure $ ReferenceImage {altText, label = if T.null label then altText else label, attrs = mempty}
] ]
-- | @(url)@, with line endings inside the url removed as djot specifies. -- | @(url)@, with line endings inside the url removed as djot specifies.
@ -1000,6 +999,7 @@ plainOf = T.concat . map go
go (Link {linkText}) = plainOf linkText go (Link {linkText}) = plainOf linkText
go (ReferenceLink {linkText}) = plainOf linkText go (ReferenceLink {linkText}) = plainOf linkText
go (Image {altText}) = altText go (Image {altText}) = altText
go (ReferenceImage {altText}) = altText
go (HTMLInline {inline_html_content}) = inline_html_content go (HTMLInline {inline_html_content}) = inline_html_content
go (Superscript content _) = plainOf content go (Superscript content _) = plainOf content
go (Subscript content _) = plainOf content go (Subscript content _) = plainOf content
@ -1034,6 +1034,7 @@ applyAttrs element extra = case element of
Link {linkText, url, title, misc_attrs} -> Link {linkText, url, title, misc_attrs = misc_attrs <> extra} Link {linkText, url, title, misc_attrs} -> Link {linkText, url, title, misc_attrs = misc_attrs <> extra}
ReferenceLink {linkText, label, attrs} -> ReferenceLink {linkText, label, attrs = attrs <> extra} ReferenceLink {linkText, label, attrs} -> ReferenceLink {linkText, label, attrs = attrs <> extra}
Image {altText, url, title, misc_attrs} -> Image {altText, url, title, misc_attrs = misc_attrs <> extra} Image {altText, url, title, misc_attrs} -> Image {altText, url, title, misc_attrs = misc_attrs <> extra}
ReferenceImage {altText, label, attrs} -> ReferenceImage {altText, label, attrs = attrs <> extra}
FootnoteReference {label, attrs} -> FootnoteReference {label, attrs = attrs <> extra} FootnoteReference {label, attrs} -> FootnoteReference {label, attrs = attrs <> extra}
-- anything else (plain text in particular) becomes a span carrying the attrs -- anything else (plain text in particular) becomes a span carrying the attrs
other -> Span [other] extra other -> Span [other] extra

View file

@ -90,6 +90,13 @@ data InlineText
title :: Maybe Text, title :: Maybe Text,
misc_attrs :: Attrs misc_attrs :: Attrs
} }
| -- the image equivalent of ReferenceLink, url comes from a RefDef with a
-- matching label
ReferenceImage
{ altText :: Text,
label :: Text,
attrs :: Attrs
}
| -- Markdown only DJOT uses RawInline | -- Markdown only DJOT uses RawInline
HTMLInline {inline_html_content :: Text} HTMLInline {inline_html_content :: Text}
| Superscript [InlineText] Attrs | Superscript [InlineText] Attrs