-
Notifications
You must be signed in to change notification settings - Fork 331
/
docusaurus.lua
195 lines (169 loc) · 5.64 KB
/
docusaurus.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
-- docusaurus.lua
-- Copyright (C) 2023 Posit Software, PBC
local kQuartoRawHtml = "quartoRawHtml"
local rawHtmlVars = pandoc.List()
local code_block = require('docusaurus_utils').code_block
local reactPreamble = pandoc.List()
local function addPreamble(preamble)
if not reactPreamble:includes(preamble) then
reactPreamble:insert(preamble)
end
end
local function Pandoc(doc)
-- insert exports at the top if we have them
if #rawHtmlVars > 0 then
local exports = ("export const %s =\n[%s];"):format(kQuartoRawHtml,
table.concat(
rawHtmlVars:map(function(var) return '`'.. var .. '`' end),
","
)
)
doc.blocks:insert(1, pandoc.RawBlock("markdown", exports .. "\n"))
end
-- insert react preamble if we have it
if #reactPreamble > 0 then
local preamble = table.concat(reactPreamble, "\n")
doc.blocks:insert(1, pandoc.RawBlock("markdown", preamble .. "\n"))
end
return doc
end
-- strip image attributes (which may result from
-- fig-format: retina) as they will result in an
-- img tag which won't hit the asset pipeline
local function Image(el)
el.attr = pandoc.Attr()
return el
end
-- header attributes only support id
local function Header(el)
el.attr = pandoc.Attr(el.identifier)
return el
end
-- transform 'mdx' into passthrough content, transform 'html'
-- into raw commamark to pass through via dangerouslySetInnerHTML
local function RawBlock(el)
if el.format == 'mdx' then
-- special mdx-code-block is not handled if whitespace is present after backtrick (#8333)
return pandoc.RawBlock("markdown", "````mdx-code-block\n" .. el.text .. "\n````")
elseif el.format == 'html' then
-- track the raw html vars (we'll insert them at the top later on as
-- mdx requires all exports be declared together)
local html = string.gsub(el.text, "\n+", "\n")
rawHtmlVars:insert(html)
-- generate a div container for the raw html and return it as the block
local html = ("<div dangerouslySetInnerHTML={{ __html: %s[%d] }} />")
:format(kQuartoRawHtml, #rawHtmlVars-1) .. "\n"
return pandoc.RawBlock("html", html)
end
end
local function DecoratedCodeBlock(node)
local el = node.code_block
return code_block(el, node.filename)
end
local function jsx(content)
return pandoc.RawBlock("markdown", content)
end
local function tabset(node)
-- note groupId
local groupId = ""
local group = node.attr.attributes["group"]
if group then
groupId = ([[ groupId="%s"]]):format(group)
end
-- create tabs
local tabs = pandoc.Div({})
tabs.content:insert(jsx("<Tabs" .. groupId .. ">"))
-- iterate through content
for i=1,#node.tabs do
local content = node.tabs[i].content
local title = node.tabs[i].title
tabs.content:insert(jsx(([[<TabItem value="%s">]]):format(pandoc.utils.stringify(title))))
if type(content) == "table" then
tabs.content:extend(content)
else
tabs.content:insert(content)
end
tabs.content:insert(jsx("</TabItem>"))
end
-- end tab and tabset
tabs.content:insert(jsx("</Tabs>"))
-- ensure we have required deps
addPreamble("import Tabs from '@theme/Tabs';")
addPreamble("import TabItem from '@theme/TabItem';")
return tabs
end
local function Table(tbl)
local out = pandoc.write(pandoc.Pandoc({tbl}), FORMAT, PANDOC_WRITER_OPTIONS)
-- if the table was written in a way that looks like HTML, then wrap it in the right RawBlock way
if string.match(out, "^%s*%<table") then
local unwrapped = pandoc.RawBlock('html', out)
return RawBlock(unwrapped)
end
end
quarto._quarto.ast.add_renderer("Tabset", function()
return quarto._quarto.format.isDocusaurusOutput()
end, function(node)
return tabset(node)
end)
quarto._quarto.ast.add_renderer("Callout", function()
return quarto._quarto.format.isDocusaurusOutput()
end, function(node)
local admonition = pandoc.Blocks({})
if node.title then
start = pandoc.Plain({})
start.content:insert(pandoc.RawInline("markdown", ":::" .. node.type .. "["))
start.content:extend(quarto.utils.as_inlines(node.title))
start.content:insert(pandoc.RawInline("markdown", "]\n"))
admonition:insert(start)
else
admonition:insert(pandoc.RawBlock("markdown", "\n:::" .. node.type))
end
local content = node.content
if type(content) == "table" then
admonition:extend(content)
else
admonition:insert(content)
end
admonition:insert(pandoc.RawBlock("markdown", ":::\n"))
return admonition
end)
quarto._quarto.ast.add_renderer("DecoratedCodeBlock", function()
return quarto._quarto.format.isDocusaurusOutput()
end, function(node)
local el = node.code_block
return code_block(el, node.filename)
end)
quarto._quarto.ast.add_renderer("FloatRefTarget", function()
return quarto._quarto.format.isDocusaurusOutput()
end, function(float)
float = quarto.doc.crossref.decorate_caption_with_crossref(float)
if quarto.doc.crossref.cap_location(float) == "top" then
return pandoc.Blocks({
pandoc.RawBlock("markdown", "<div id=\"" .. float.identifier .. "\">"),
pandoc.Div(quarto.utils.as_blocks(float.caption_long)),
pandoc.Div(quarto.utils.as_blocks(float.content)),
pandoc.RawBlock("markdown", "</div>")
})
else
return pandoc.Blocks({
pandoc.RawBlock("markdown", "<div id=\"" .. float.identifier .. "\">"),
pandoc.Div(quarto.utils.as_blocks(float.content)),
pandoc.Div(quarto.utils.as_blocks(float.caption_long)),
pandoc.RawBlock("markdown", "</div>")
})
end
end)
return {
{
traverse = "topdown",
Image = Image,
Header = Header,
RawBlock = RawBlock,
DecoratedCodeBlock = DecoratedCodeBlock,
CodeBlock = CodeBlock,
Table = Table,
},
{
Pandoc = Pandoc,
}
}