Skip to content

Commit 93650bc

Browse files
committed
wrap inline in div if one is found (#13121)
1 parent 9cfe15f commit 93650bc

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

src/resources/filters/quarto-pre/contentsshortcode.lua

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ function contents_shortcode_filter()
66
local divs = {}
77
local spans = {}
88

9+
local function handle_inline_with_attr(el)
10+
if ids_used[el.attr.identifier] then
11+
spans[el.attr.identifier] = el
12+
return {}
13+
end
14+
15+
-- remove 'cell-' from identifier, try again
16+
local truncated_id = el.attr.identifier:match("^cell%-(.+)$")
17+
if ids_used[truncated_id] then
18+
spans[truncated_id] = el
19+
-- FIXME: this is a workaround for the fact that we don't have a way to
20+
-- distinguish between divs that appear as the output of code cells
21+
-- (which have a different id creation mechanism)
22+
-- and "regular" divs.
23+
-- We need to fix https://github.com/quarto-dev/quarto-cli/issues/7062 first.
24+
return {}
25+
else
26+
return nil
27+
end
28+
end
29+
930
return {
1031
Pandoc = function(doc)
1132
_quarto.ast.walk(doc.blocks, {
@@ -43,13 +64,10 @@ function contents_shortcode_filter()
4364
return nil
4465
end
4566
end,
46-
Span = function(el)
47-
if not ids_used[el.attr.identifier] then
48-
return nil
49-
end
50-
spans[el.attr.identifier] = el
51-
return {}
52-
end
67+
Code = handle_inline_with_attr,
68+
Image = handle_inline_with_attr,
69+
Span = handle_inline_with_attr,
70+
Link = handle_inline_with_attr
5371
})
5472

5573
local handle_block = function(el)
@@ -75,14 +93,22 @@ function contents_shortcode_filter()
7593
return {}
7694
end
7795
local div = divs[data]
78-
if div == nil then
79-
warn(
80-
"[Malformed document] Found `contents` shortcode without a corresponding div with id: " .. tostring(data) .. ".\n" ..
81-
"This might happen because the shortcode is used in div context, while the id corresponds to a span.\n" ..
82-
"Removing from document.")
83-
return {}
96+
if div ~= nil then
97+
-- if we have a div, return it
98+
return div
99+
end
100+
-- if we don't have a div, try to find a span
101+
-- and wrap it in a div
102+
local span = spans[data]
103+
if span ~= nil then
104+
-- if we have a span, return it wrapped in a div
105+
return pandoc.Div(pandoc.Plain({span}))
84106
end
85-
return div
107+
warn(
108+
"[Malformed document] Found `contents` shortcode without a corresponding div with id: " .. tostring(data) .. ".\n" ..
109+
"This might happen because the shortcode is used in div context, while the id corresponds to a span.\n" ..
110+
"Removing from document.")
111+
return {}
86112
end
87113
-- replace div-context entries
88114
doc.blocks = _quarto.ast.walk(doc.blocks, {

0 commit comments

Comments
 (0)