Skip to content

Instantly share code, notes, and snippets.

@jskherman
Last active November 17, 2022 04:02
Show Gist options
  • Save jskherman/8ba720e392696fc64935c1fb9c4a9a28 to your computer and use it in GitHub Desktop.
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
-- 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
@jskherman
Copy link
Author

jskherman commented Nov 17, 2022

This filter has been modified to accommodate Quarto callout blocks (with the collapse attribute).
See 👉 original source at the Obsidian Forum.

Note
Write a title for the callout block instead as ## Heading 2 or other heading levels instead of beside the [!note] syntax for Quarto compatibility.

Warning
Also make sure to have a blank line in between the syntax [!note] and your content for it to properly work, like so:

> [!note]+
> 
> #### Title of the Callout
> This is a note callout block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment