Path: blob/main/src/resources/extensions/quarto/docusaurus/docusaurus_utils.lua
12923 views
-- docusaurus_utils.lua1-- Copyright (C) 2020-2023 Posit Software, PBC23-- a slightly different version of render_folded_block4-- that avoids emitting rawhtml blocks, since these are handled5-- specially by the docusaurus filter6function render_folded_block(block)7local make_code_fold_html = function(fold, summary)8local div = pandoc.Div({}, pandoc.Attr("", {9"quarto-scaffold"10}))11quarto_global_state.codeFoldingCss = _quarto.format.isHtmlOutput()12local open = ""13if fold == "show" then14open = " open"15end16local style = ""17local clz = 'code-fold'18if block.attr.classes:includes("hidden") then19clz = clz .. " hidden"20end2122style = ' class="' .. clz .. '"'23local beginPara = pandoc.Plain({24pandoc.RawInline("markdown", "<details" .. open .. style .. ">\n<summary>"),25})2627if not isEmpty(summary) then28local inlines = process_shortcodes(string_to_quarto_ast_inlines(summary))29tappend(beginPara.content, inlines)30end31beginPara.content:insert(pandoc.RawInline("markdown", "</summary>"))32div.content:insert(beginPara)33div.content:insert(block)34div.content:insert(pandoc.RawInline("markdown", "</details>"))35return div36end37local make_code_cell_scaffold = function(div)38return pandoc.Div({ block }, pandoc.Attr("", { "quarto-scaffold" }))39end40if not block.attr.classes:includes("cell-code") then41return nil42end43if not (_quarto.format.isHtmlOutput() or _quarto.format.isMarkdownWithHtmlOutput()) then44return make_code_cell_scaffold(block)45end46local fold = foldAttribute(block)47local summary = summaryAttribute(block)48if fold ~= nil or summary ~= nil then49block.attr.attributes["code-fold"] = nil50block.attr.attributes["code-summary"] = nil51if fold ~= "none" then52return make_code_fold_html(fold, summary)53else54return block55end56else57return block58end59end6061local function max_backticks(el)62local v = 063for w in el.text:gmatch("`+") do64v = math.max(v, #w)65end66return v67end6869function code_block(code_block_slot, filename)70function process(el)71local lang = el.attr.classes[1]72local title = filename or el.attr.attributes["filename"] or el.attr.attributes["title"]73local showLineNumbers = el.attr.classes:includes('number-lines')74local codeLineNumbers = el.attr.attributes["code-line-numbers"]75if lang or title or showLineNumbers then76if not lang then77lang = 'text'78end79local backticks = string.rep("`", math.max(3, max_backticks(el) + 1))80local code = "\n" .. backticks .. lang81if codeLineNumbers then82code = code .. " {" .. codeLineNumbers .. "}"83end84if showLineNumbers then85code = code .. " showLineNumbers"86end87if title then88code = code .. " title=\"" .. title .. "\""89end90code = code .. "\n" .. el.text .. "\n" .. backticks .. "\n"9192-- docusaures code block attributes don't conform to any syntax93-- that pandoc natively understands, so return the CodeBlock as94-- "raw" markdown (so it bypasses pandoc processing entirely)95return pandoc.RawBlock("markdown", code)9697elseif #el.attr.classes == 0 then98el.attr.classes:insert('text')99return el100else101return el102end103end104105code_block_slot = render_folded_block(code_block_slot) or code_block_slot106107if code_block_slot.t == "CodeBlock" then108return process(code_block_slot)109else110return _quarto.ast.walk(code_block_slot, {111CodeBlock = process112})113end114end115116return {117code_block = code_block118}119120