Skip to content

Commit 2a47994

Browse files
authored
Merge pull request #13122 from quarto-dev/bugfix/contents-span-div-mismatch
Allow `{{< contents >}}` to use inline elements
2 parents ec10654 + 7af6275 commit 2a47994

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

news/changelog-1.8.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ All changes included in 1.8:
136136
- ([#13031](https://github.com/quarto-dev/quarto-cli/pull/13031)): Add `.quarto_ipynb` files to `.gitignore` by default.
137137
- ([#13085](https://github.com/quarto-dev/quarto-cli/pull/13085)): Avoid `kbd` shortcode crashes on unknown OS keys.
138138
- ([#13164](https://github.com/quarto-dev/quarto-cli/pull/13164)): add `julia` to execute schema to allow autocomplete suggestions. (@mcanouil)
139+
- ([#13121](https://github.com/quarto-dev/quarto-cli/issues/13121)): Allow `contents` shortcode to find inline elements.
139140

140141

141142
## Quarto Internals
142143

143144
- ([#13155](https://github.com/quarto-dev/quarto-cli/pull/13155)): Process `pandoc-reader-FORMAT` raw blocks through `pandoc.read(FORMAT)`.
144-
- ([#13255](https://github.com/quarto-dev/quarto-cli/pull/13255)): Move some Lua code to use Pandoc's Lua API.
145+
- ([#13255](https://github.com/quarto-dev/quarto-cli/pull/13255)): Move some Lua code to use Pandoc's Lua API.

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+
quarto.log.warning(
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, {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
format: typst
3+
title: Another section
4+
_quarto:
5+
tests:
6+
typst:
7+
noErrorsOrWarnings: true
8+
---
9+
10+
## A section
11+
12+
Here we define a plot.
13+
14+
::: {.cell execution_count=1}
15+
16+
::: {.cell-output .cell-output-display}
17+
`code`{#a-cell}
18+
:::
19+
:::
20+
21+
22+
23+
Here we use the plot, inside a callout:
24+
25+
26+
::: callout-note
27+
28+
## Note the following plot
29+
30+
{{< contents a-cell >}}
31+
32+
:::
33+
34+

tests/smoke/smoke-all.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ function resolveTestSpecs(
180180
} else if (key === "noErrors") {
181181
checkWarnings = false;
182182
verifyFns.push(noErrors);
183+
} else if (key === "noErrorsOrWarnings") {
184+
checkWarnings = false;
185+
verifyFns.push(noErrorsOrWarnings);
183186
} else {
184187
// See if there is a project and grab it's type
185188
const projectPath = findRootTestsProjectDir(input)

tests/verify.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ export const noErrorsOrWarnings: Verify = {
153153
const isErrorOrWarning = (output: ExecuteOutput) => {
154154
return output.levelName.toLowerCase() === "warn" ||
155155
output.levelName.toLowerCase() === "error";
156+
// I'd like to do this but many many of our tests
157+
// would fail right now because we're assuming noErrorsOrWarnings
158+
// doesn't include warnings from the lua subsystem
159+
// ||
160+
// output.msg.startsWith("(W)"); // this is a warning from quarto.log.warning()
156161
};
157162

158163
const errorsOrWarnings = outputs.some(isErrorOrWarning);

0 commit comments

Comments
 (0)