Last active
November 17, 2022 04:02
-
-
Save jskherman/8ba720e392696fc64935c1fb9c4a9a28 to your computer and use it in GitHub Desktop.
Pandoc Lua-filter by saf-dmitry for Obsidian Callouts to Quarto Callout Blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Source: https://stackoverflow.com/a/33511182 | |
-- Check if array contains a specific value | |
local function has_value (tab, val) | |
for index, value in ipairs(tab) do | |
if value == val then | |
return true | |
end | |
end | |
return false | |
end | |
-- -- | |
-- Pandoc Lua-filter for Obsidian Callouts | |
-- Original Source: https://forum.obsidian.md/t/rendering-callouts-similarly-in-pandoc/40020/ | |
-- Notes: | |
-- Original snippet modified to accommodate Quarto callout blocks with collapse. | |
-- Make sure to have a blank line between the `> [!note]` and content of the callout block. | |
local stringify = (require "pandoc.utils").stringify | |
local callouts = {'note', 'warning', 'tip', 'important', 'caution'} | |
function BlockQuote (el) | |
start = el.content[1] | |
if (start.t == "Para" and start.content[1].t == "Str" and | |
start.content[1].text:match("^%[!%w+%][-+]?$")) then | |
_, _, ctype = start.content[1].text:find("%[!(%w+)%]") | |
_, _, collapse = start.content[1].text:find("%[!%w+%]([-+]?)") | |
el.content:remove(1) | |
start.content:remove(1) | |
if has_value(callouts, ctype:lower()) then | |
div = pandoc.Div(el.content, {class = "callout-" .. ctype:lower()}) | |
else | |
div = pandoc.Div(el.content, {class = "callout-note"}) | |
end | |
div.attributes["data-callout"] = ctype:lower() | |
div.attributes["data-title"] = stringify(start.content):gsub("^ ", "") | |
if collapse == "-" then | |
div.attributes["collapse"] = "true" | |
elseif collapse == "+" then | |
div.attributes["collapse"] = "false" | |
end | |
return div | |
else | |
return el | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This filter has been modified to accommodate Quarto callout blocks (with the
collapse
attribute).See 👉 original source at the Obsidian Forum.