Skip to content

Commit

Permalink
Merge pull request #7296 from quarto-dev/bugfix/7294
Browse files Browse the repository at this point in the history
move docusaurus custom nodes to new renderer api
  • Loading branch information
cscheid authored Oct 19, 2023
2 parents 9a01c2a + 11f20d0 commit 9a332f2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function codeBlock(el, filename)
-- docusaurus_utils.lua
-- Copyright (C) 2020-2023 Posit Software, PBC

function code_block(el, filename)
local lang = el.attr.classes[1]
local title = filename or el.attr.attributes["filename"] or el.attr.attributes["title"]
local showLineNumbers = el.attr.classes:includes('number-lines')
Expand Down Expand Up @@ -33,5 +36,5 @@ function codeBlock(el, filename)
end

return {
codeBlock = codeBlock
code_block = code_block
}
82 changes: 43 additions & 39 deletions src/resources/extensions/quarto/docusaurus/docusaurus_writer.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- docusaurus_writer.lua
-- Copyright (C) 2020-2023 Posit Software, PBC

local codeBlock = require('docusaurus_utils').codeBlock

local code_block = require('docusaurus_utils').code_block

local reactPreamble = pandoc.List()

Expand All @@ -14,7 +15,7 @@ local function jsx(content)
return pandoc.RawBlock("markdown", content)
end

local function tabset(node, filter)
local function tabset(node)
-- note groupId
local groupId = ""
local group = node.attr.attributes["group"]
Expand All @@ -32,11 +33,10 @@ local function tabset(node, filter)
local title = node.tabs[i].title

tabs.content:insert(jsx(([[<TabItem value="%s">]]):format(pandoc.utils.stringify(title))))
local result = quarto._quarto.ast.walk(content, filter)
if type(result) == "table" then
tabs.content:extend(result)
if type(content) == "table" then
tabs.content:extend(content)
else
tabs.content:insert(result)
tabs.content:insert(content)
end
tabs.content:insert(jsx("</TabItem>"))
end
Expand All @@ -51,38 +51,42 @@ local function tabset(node, filter)
return tabs
end

function Writer(doc, opts)
local filter
filter = {
CodeBlock = codeBlock,

DecoratedCodeBlock = function(node)
local el = node.code_block
return codeBlock(el, node.filename)
end,

Tabset = function(node)
return tabset(node, filter)
end,

Callout = function(node)
local admonition = pandoc.List()
admonition:insert(pandoc.RawBlock("markdown", "\n:::" .. node.type))
if node.title then
admonition:insert(pandoc.Header(2, node.title))
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
}

doc = quarto._quarto.ast.walk(doc, filter)
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({})
admonition:insert(pandoc.RawBlock("markdown", "\n:::" .. node.type))
if node.title then
admonition:insert(pandoc.Header(2, node.title))
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)

function Writer(doc, opts)
doc = quarto._quarto.ast.walk(doc, {
CodeBlock = code_block,
})
assert(doc ~= nil)

-- insert react preamble if we have it
if #reactPreamble > 0 then
Expand Down
36 changes: 36 additions & 0 deletions tests/docs/smoke-all/2023/10/19/7294.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: foo
format: docusaurus-md
_quarto:
tests:
docusaurus-md:
ensureFileRegexMatches:
-
- "<Tabs>" # tabset
- ":::note" # callouts
- "```python title=\"Python\"" # decorated code blocks
- []
---

:::{.panel-tabset}

## Not work

```{.python filename="Python"}
print("bar")
```

## Work

```{.python}
print("bar")
```

:::

::: callout-note

Note that this needs to be rendered.

:::

0 comments on commit 9a332f2

Please sign in to comment.