Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua
12923 views
1
-- docusaurus_utils.lua
2
-- Copyright (C) 2020-2023 Posit Software, PBC
3
4
-- a slightly different version of render_folded_block
5
-- that avoids emitting rawhtml blocks, since these are handled
6
-- specially by the docusaurus filter
7
function render_folded_block(block)
8
local make_code_fold_html = function(fold, summary)
9
local div = pandoc.Div({}, pandoc.Attr("", {
10
"quarto-scaffold"
11
}))
12
quarto_global_state.codeFoldingCss = _quarto.format.isHtmlOutput()
13
local open = ""
14
if fold == "show" then
15
open = " open"
16
end
17
local style = ""
18
local clz = 'code-fold'
19
if block.attr.classes:includes("hidden") then
20
clz = clz .. " hidden"
21
end
22
23
style = ' class="' .. clz .. '"'
24
local beginPara = pandoc.Plain({
25
pandoc.RawInline("markdown", "<details" .. open .. style .. ">\n<summary>"),
26
})
27
28
if not isEmpty(summary) then
29
local inlines = process_shortcodes(string_to_quarto_ast_inlines(summary))
30
tappend(beginPara.content, inlines)
31
end
32
beginPara.content:insert(pandoc.RawInline("markdown", "</summary>"))
33
div.content:insert(beginPara)
34
div.content:insert(block)
35
div.content:insert(pandoc.RawInline("markdown", "</details>"))
36
return div
37
end
38
local make_code_cell_scaffold = function(div)
39
return pandoc.Div({ block }, pandoc.Attr("", { "quarto-scaffold" }))
40
end
41
if not block.attr.classes:includes("cell-code") then
42
return nil
43
end
44
if not (_quarto.format.isHtmlOutput() or _quarto.format.isMarkdownWithHtmlOutput()) then
45
return make_code_cell_scaffold(block)
46
end
47
local fold = foldAttribute(block)
48
local summary = summaryAttribute(block)
49
if fold ~= nil or summary ~= nil then
50
block.attr.attributes["code-fold"] = nil
51
block.attr.attributes["code-summary"] = nil
52
if fold ~= "none" then
53
return make_code_fold_html(fold, summary)
54
else
55
return block
56
end
57
else
58
return block
59
end
60
end
61
62
local function max_backticks(el)
63
local v = 0
64
for w in el.text:gmatch("`+") do
65
v = math.max(v, #w)
66
end
67
return v
68
end
69
70
function code_block(code_block_slot, filename)
71
function process(el)
72
local lang = el.attr.classes[1]
73
local title = filename or el.attr.attributes["filename"] or el.attr.attributes["title"]
74
local showLineNumbers = el.attr.classes:includes('number-lines')
75
local codeLineNumbers = el.attr.attributes["code-line-numbers"]
76
if lang or title or showLineNumbers then
77
if not lang then
78
lang = 'text'
79
end
80
local backticks = string.rep("`", math.max(3, max_backticks(el) + 1))
81
local code = "\n" .. backticks .. lang
82
if codeLineNumbers then
83
code = code .. " {" .. codeLineNumbers .. "}"
84
end
85
if showLineNumbers then
86
code = code .. " showLineNumbers"
87
end
88
if title then
89
code = code .. " title=\"" .. title .. "\""
90
end
91
code = code .. "\n" .. el.text .. "\n" .. backticks .. "\n"
92
93
-- docusaures code block attributes don't conform to any syntax
94
-- that pandoc natively understands, so return the CodeBlock as
95
-- "raw" markdown (so it bypasses pandoc processing entirely)
96
return pandoc.RawBlock("markdown", code)
97
98
elseif #el.attr.classes == 0 then
99
el.attr.classes:insert('text')
100
return el
101
else
102
return el
103
end
104
end
105
106
code_block_slot = render_folded_block(code_block_slot) or code_block_slot
107
108
if code_block_slot.t == "CodeBlock" then
109
return process(code_block_slot)
110
else
111
return _quarto.ast.walk(code_block_slot, {
112
CodeBlock = process
113
})
114
end
115
end
116
117
return {
118
code_block = code_block
119
}
120