|
| 1 | +-- This filter transforms Asciidoctor's admonition blocks into GitHub Flavored Markdown alerts. |
| 2 | + |
| 3 | +-- This filter works by finding a Div with class "admonitionblock", then |
| 4 | +-- walking its contents to find the table cell (<td>) with class "content". |
| 5 | +-- It then extracts the content of that cell, prepends a GFM alert header |
| 6 | +-- like "[!NOTE]", and wraps the whole thing in a blockquote. |
| 7 | + |
| 8 | +-- Define a walker to find the content cell. |
| 9 | +-- We use a variable in the parent scope to store the result. |
| 10 | +local content_cell_blocks = nil |
| 11 | +local walker = { |
| 12 | + Cell = function(cell) |
| 13 | + -- The admonition content is in a cell with class "content". |
| 14 | + if cell.attr.classes:includes('content') then |
| 15 | + content_cell_blocks = cell.content |
| 16 | + end |
| 17 | + return cell |
| 18 | + end |
| 19 | +} |
| 20 | + |
| 21 | +function Div(div) |
| 22 | + -- Asciidoctor creates a div with class "admonitionblock" and a second class |
| 23 | + -- for the type (e.g., "note", "important"). |
| 24 | + if not div.classes:includes("admonitionblock") then |
| 25 | + return div -- Not an admonition block, do nothing. |
| 26 | + end |
| 27 | + |
| 28 | + -- Determine the admonition type from the div's classes. |
| 29 | + local admonition_type = "" |
| 30 | + local type_map = { note = "NOTE", important = "IMPORTANT", tip = "TIP", warning = "WARNING", caution = "CAUTION" } |
| 31 | + for class, type in pairs(type_map) do |
| 32 | + if div.classes:includes(class) then |
| 33 | + admonition_type = type |
| 34 | + break |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + if admonition_type == "" then |
| 39 | + return div -- Unknown admonition type, do nothing. |
| 40 | + end |
| 41 | + |
| 42 | + -- Reset and run the walker to find the content cell's blocks. |
| 43 | + content_cell_blocks = nil |
| 44 | + pandoc.walk_block(div, walker) |
| 45 | + |
| 46 | + if content_cell_blocks then |
| 47 | + -- The cell content is wrapped in further divs (e.g., class="paragraph"). |
| 48 | + -- We need to extract the actual content blocks from them. |
| 49 | + local extracted_blocks = {} |
| 50 | + for _, block in ipairs(content_cell_blocks) do |
| 51 | + if block.t == 'Div' and block.content then |
| 52 | + for _, inner_block in ipairs(block.content) do |
| 53 | + table.insert(extracted_blocks, inner_block) |
| 54 | + end |
| 55 | + else |
| 56 | + table.insert(extracted_blocks, block) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + -- Create the GFM alert header, e.g., '[!IMPORTANT]' |
| 61 | + local alert_header = pandoc.Para{ |
| 62 | + pandoc.RawInline('markdown', '[!' .. admonition_type .. ']') |
| 63 | + } |
| 64 | + |
| 65 | + -- Prepend the header and wrap everything in a BlockQuote. |
| 66 | + table.insert(extracted_blocks, 1, alert_header) |
| 67 | + return pandoc.BlockQuote(extracted_blocks) |
| 68 | + end |
| 69 | + |
| 70 | + -- If we didn't find a content cell, return the div unchanged. |
| 71 | + return div |
| 72 | +end |
0 commit comments