From 7a0032c7d38568c2bc4c944caee30392d71720f5 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Fri, 4 Sep 2026 15:21:46 -0400 Subject: [PATCH] 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 --- src/Djot.hs | 5 +++-- src/IR.hs | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Djot.hs b/src/Djot.hs index a7847be..323aeb1 100644 --- a/src/Djot.hs +++ b/src/Djot.hs @@ -893,9 +893,8 @@ image = do url <- inlineDestination pure $ Image {altText, url, title = Nothing, misc_attrs = mempty}, do - -- the IR has no reference image, so the label stands in for the url 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. @@ -1000,6 +999,7 @@ plainOf = T.concat . map go go (Link {linkText}) = plainOf linkText go (ReferenceLink {linkText}) = plainOf linkText go (Image {altText}) = altText + go (ReferenceImage {altText}) = altText go (HTMLInline {inline_html_content}) = inline_html_content go (Superscript 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} 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} + ReferenceImage {altText, label, attrs} -> ReferenceImage {altText, label, attrs = attrs <> extra} FootnoteReference {label, attrs} -> FootnoteReference {label, attrs = attrs <> extra} -- anything else (plain text in particular) becomes a span carrying the attrs other -> Span [other] extra diff --git a/src/IR.hs b/src/IR.hs index 894e6f5..b85b19b 100644 --- a/src/IR.hs +++ b/src/IR.hs @@ -90,6 +90,13 @@ data InlineText title :: Maybe Text, 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 HTMLInline {inline_html_content :: Text} | Superscript [InlineText] Attrs