Skip to content

Commit f9bb1eb

Browse files
authored
Render callouts to GFM (#6942)
* format callout for gfm output. Closes #6935 * detect gfm robustly * changelog
1 parent aadb190 commit f9bb1eb

6 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎news/changelog-1.4.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@
162162
- ([#6211](https://github.com/quarto-dev/quarto-cli/pull/6211)): Improve error message when a JSON filter (or a potentially misspelled Lua filter from an extension) is not found.
163163
- ([#6215](https://github.com/quarto-dev/quarto-cli/issues/6215)): Add `quarto.utils.string_to_inlines` and `quarto.utils.string_to_blocks` to Lua API to convert a string to a list of inlines or blocks taking into account quarto's AST structure.
164164
- ([#6289](https://github.com/quarto-dev/quarto-cli/issues/6289)): allow `markdownToInlines` to take empty string.
165+
- ([#6935](https://github.com/quarto-dev/quarto-cli/issues/6935)): render callouts to `gfm` using GitHub's syntax.
166+
- ([#6935](https://github.com/quarto-dev/quarto-cli/issues/6935)): Add isGithubMarkdownOutput() to quarto.format API.
165167

166168
## Debian Installer
167169

‎src/command/render/filters.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
kFigEnv,
2121
kFigPos,
2222
kFigResponsive,
23+
kFormatIdentifier,
2324
kHeaderIncludes,
2425
kHtmlMathMethod,
2526
kIncludeAfter,
@@ -156,6 +157,7 @@ export async function filterParamsJson(
156157
crossref: crossrefFilterActive(options),
157158
jats_subarticle: options.format.metadata[kJatsSubarticle],
158159
},
160+
[kFormatIdentifier]: options.format.identifier,
159161
};
160162
return JSON.stringify(params);
161163
}

‎src/config/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,5 @@ export const kSourceMappingRegexes = [
737737
/^\/\/#\s*sourceMappingURL\=.*\.map$/gm,
738738
/\/\*\# sourceMappingURL=.* \*\//g,
739739
];
740+
741+
export const kFormatIdentifier = "format-identifier";

‎src/resources/filters/customnodes/callout.lua‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,3 +574,24 @@ end, calloutDiv)
574574
_quarto.ast.add_renderer("Callout", function(_)
575575
return _quarto.format.isEpubOutput() or _quarto.format.isRevealJsOutput()
576576
end, epubCallout)
577+
578+
_quarto.ast.add_renderer("Callout", function(_)
579+
return _quarto.format.isGithubMarkdownOutput()
580+
end, function(callout)
581+
local result = pandoc.Blocks({})
582+
local header = "[!" .. callout.type:upper() .. "]"
583+
result:insert(pandoc.RawBlock("markdown", header))
584+
local tt = pandoc.utils.type(callout.title)
585+
if tt ~= "nil" then
586+
result:insert(pandoc.Header(3, quarto.utils.as_inlines(callout.title)))
587+
end
588+
local ct = pandoc.utils.type(callout.content)
589+
if ct == "Block" then
590+
result:insert(callout.content)
591+
elseif ct == "Blocks" then
592+
result:extend(callout.content)
593+
else
594+
internal_error()
595+
end
596+
return pandoc.BlockQuote(result)
597+
end)

‎src/resources/pandoc/datadir/_format.lua‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ local function isMarkdownOutput()
128128
return tcontains(formats, FORMAT) or is_docusaurus_output()
129129
end
130130

131+
local function isGithubMarkdownOutput()
132+
return param("format-identifier", {})["base-format"] == "gfm"
133+
end
134+
131135
-- check for markdown with raw_html enabled
132136
local function isMarkdownWithHtmlOutput()
133137
return (isMarkdownOutput() and tcontains(PANDOC_WRITER_OPTIONS.extensions, "raw_html")) or is_docusaurus_output()
@@ -240,7 +244,6 @@ local function isTypstOutput()
240244
return FORMAT == "typst"
241245
end
242246

243-
244247
return {
245248
isAsciiDocOutput = isAsciiDocOutput,
246249
isRawHtml = isRawHtml,
@@ -256,6 +259,7 @@ return {
256259
isRevealJsOutput = isRevealJsOutput,
257260
isSlideOutput = isSlideOutput,
258261
isEpubOutput = isEpubOutput,
262+
isGithubMarkdownOutput = isGithubMarkdownOutput,
259263
isMarkdownOutput = isMarkdownOutput,
260264
isMarkdownWithHtmlOutput = isMarkdownWithHtmlOutput,
261265
isIpynbOutput = isIpynbOutput,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: issue-6935
3+
_quarto:
4+
tests:
5+
gfm:
6+
ensureFileRegexMatches:
7+
-
8+
- "[>] ### With title" # Note that all headers are re-rendered to level 3
9+
- "[>] This is a callout"
10+
- "[>] \\[[!]NOTE\\]"
11+
- "[>] \\[[!]IMPORTANT\\]"
12+
- ["<div>"]
13+
---
14+
15+
::: {.callout-note}
16+
17+
## With title
18+
19+
This is a callout
20+
21+
:::
22+
23+
24+
::: {.callout-important}
25+
26+
This is a callout without a title.
27+
28+
:::

0 commit comments

Comments
 (0)