diff --git a/news/changelog-1.8.md b/news/changelog-1.8.md index 69d8774ffed..f563f1261d2 100644 --- a/news/changelog-1.8.md +++ b/news/changelog-1.8.md @@ -136,9 +136,10 @@ All changes included in 1.8: - ([#13031](https://github.com/quarto-dev/quarto-cli/pull/13031)): Add `.quarto_ipynb` files to `.gitignore` by default. - ([#13085](https://github.com/quarto-dev/quarto-cli/pull/13085)): Avoid `kbd` shortcode crashes on unknown OS keys. - ([#13164](https://github.com/quarto-dev/quarto-cli/pull/13164)): add `julia` to execute schema to allow autocomplete suggestions. (@mcanouil) +- ([#13121](https://github.com/quarto-dev/quarto-cli/issues/13121)): Allow `contents` shortcode to find inline elements. ## Quarto Internals - ([#13155](https://github.com/quarto-dev/quarto-cli/pull/13155)): Process `pandoc-reader-FORMAT` raw blocks through `pandoc.read(FORMAT)`. -- ([#13255](https://github.com/quarto-dev/quarto-cli/pull/13255)): Move some Lua code to use Pandoc's Lua API. \ No newline at end of file +- ([#13255](https://github.com/quarto-dev/quarto-cli/pull/13255)): Move some Lua code to use Pandoc's Lua API. diff --git a/src/resources/filters/quarto-pre/contentsshortcode.lua b/src/resources/filters/quarto-pre/contentsshortcode.lua index 6e21e30bb08..174d9ddfc69 100644 --- a/src/resources/filters/quarto-pre/contentsshortcode.lua +++ b/src/resources/filters/quarto-pre/contentsshortcode.lua @@ -6,6 +6,27 @@ function contents_shortcode_filter() local divs = {} local spans = {} + local function handle_inline_with_attr(el) + if ids_used[el.attr.identifier] then + spans[el.attr.identifier] = el + return {} + end + + -- remove 'cell-' from identifier, try again + local truncated_id = el.attr.identifier:match("^cell%-(.+)$") + if ids_used[truncated_id] then + spans[truncated_id] = el + -- FIXME: this is a workaround for the fact that we don't have a way to + -- distinguish between divs that appear as the output of code cells + -- (which have a different id creation mechanism) + -- and "regular" divs. + -- We need to fix https://github.com/quarto-dev/quarto-cli/issues/7062 first. + return {} + else + return nil + end + end + return { Pandoc = function(doc) _quarto.ast.walk(doc.blocks, { @@ -43,13 +64,10 @@ function contents_shortcode_filter() return nil end end, - Span = function(el) - if not ids_used[el.attr.identifier] then - return nil - end - spans[el.attr.identifier] = el - return {} - end + Code = handle_inline_with_attr, + Image = handle_inline_with_attr, + Span = handle_inline_with_attr, + Link = handle_inline_with_attr }) local handle_block = function(el) @@ -75,14 +93,22 @@ function contents_shortcode_filter() return {} end local div = divs[data] - if div == nil then - warn( - "[Malformed document] Found `contents` shortcode without a corresponding div with id: " .. tostring(data) .. ".\n" .. - "This might happen because the shortcode is used in div context, while the id corresponds to a span.\n" .. - "Removing from document.") - return {} + if div ~= nil then + -- if we have a div, return it + return div + end + -- if we don't have a div, try to find a span + -- and wrap it in a div + local span = spans[data] + if span ~= nil then + -- if we have a span, return it wrapped in a div + return pandoc.Div(pandoc.Plain({span})) end - return div + quarto.log.warning( + "[Malformed document] Found `contents` shortcode without a corresponding div with id: " .. tostring(data) .. ".\n" .. + "This might happen because the shortcode is used in div context, while the id corresponds to a span.\n" .. + "Removing from document.") + return {} end -- replace div-context entries doc.blocks = _quarto.ast.walk(doc.blocks, { diff --git a/tests/docs/smoke-all/2025/07/23/13121.qmd b/tests/docs/smoke-all/2025/07/23/13121.qmd new file mode 100644 index 00000000000..b9677dcea18 --- /dev/null +++ b/tests/docs/smoke-all/2025/07/23/13121.qmd @@ -0,0 +1,34 @@ +--- +format: typst +title: Another section +_quarto: + tests: + typst: + noErrorsOrWarnings: true +--- + +## A section + +Here we define a plot. + +::: {.cell execution_count=1} + +::: {.cell-output .cell-output-display} +`code`{#a-cell} +::: +::: + + + +Here we use the plot, inside a callout: + + +::: callout-note + +## Note the following plot + +{{< contents a-cell >}} + +::: + + diff --git a/tests/smoke/smoke-all.test.ts b/tests/smoke/smoke-all.test.ts index d04a0f3feb6..03efa8413f3 100644 --- a/tests/smoke/smoke-all.test.ts +++ b/tests/smoke/smoke-all.test.ts @@ -180,6 +180,9 @@ function resolveTestSpecs( } else if (key === "noErrors") { checkWarnings = false; verifyFns.push(noErrors); + } else if (key === "noErrorsOrWarnings") { + checkWarnings = false; + verifyFns.push(noErrorsOrWarnings); } else { // See if there is a project and grab it's type const projectPath = findRootTestsProjectDir(input) diff --git a/tests/verify.ts b/tests/verify.ts index f871dfe22c3..bd8689bec0a 100644 --- a/tests/verify.ts +++ b/tests/verify.ts @@ -153,6 +153,11 @@ export const noErrorsOrWarnings: Verify = { const isErrorOrWarning = (output: ExecuteOutput) => { return output.levelName.toLowerCase() === "warn" || output.levelName.toLowerCase() === "error"; + // I'd like to do this but many many of our tests + // would fail right now because we're assuming noErrorsOrWarnings + // doesn't include warnings from the lua subsystem + // || + // output.msg.startsWith("(W)"); // this is a warning from quarto.log.warning() }; const errorsOrWarnings = outputs.some(isErrorOrWarning);