-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathpandoc_codeblock.lua
72 lines (62 loc) · 2.14 KB
/
pandoc_codeblock.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- Pandoc Lua filter to handle code blocks
-- Test cases
-- raster/r.sun/r.sun.html
-- Enforces markdownlint rules during Pandoc conversion
local MAX_LINE_LENGTH = 120 -- Adjust as needed for MD013
local LIST_INDENT = ""
function Image(el)
-- Convert HTML <img> to Markdown 
local alt_text = el.alt or "image-alt"
local src = el.src
return pandoc.Image({pandoc.Str(alt_text)}, src)
end
-- Fixes some edge cases with raw HTML elements
function RawInline(el)
if el.format == "html" then
if el.text:match("<em>") then
return pandoc.RawInline("markdown", "*")
elseif el.text:match("</em>") then
return pandoc.RawInline("markdown", "*")
elseif el.text:match("<i>") then
return pandoc.RawInline("markdown", "*")
elseif el.text:match("</i>") then
return pandoc.RawInline("markdown", "*")
elseif el.text:match(" ") then
return pandoc.RawInline("markdown", " ")
elseif el.text:match("<") then
return pandoc.RawInline("markdown", "<")
elseif el.text:match(">") then
return pandoc.RawInline("markdown", ">")
end
end
return el
end
function CodeBlock(el)
-- Ensure fenced code blocks with backticks
local lang = el.classes[1] or "sh" -- Preserve language if available
return pandoc.RawBlock("markdown", "```" .. lang .. "\n" .. el.text .. "\n```")
end
function Header(el)
return pandoc.Header(el.level, el.content) -- Ensure ATX-style headers
end
function Str(el)
local text = el.text:gsub("%s+$", "") -- Remove trailing spaces
return pandoc.Str(text)
end
function Pandoc(doc)
-- Process document with defined rules
local new_blocks = {}
local previous_blank = false
for _, block in ipairs(doc.blocks) do
if block.t == "Para" and #block.content == 0 then
if not previous_blank then
table.insert(new_blocks, block)
end
previous_blank = true
else
table.insert(new_blocks, block)
previous_blank = false
end
end
return pandoc.Pandoc(new_blocks)
end