Skip to content

Commit 3312cac

Browse files
committed
Refactor
1 parent 2988ce3 commit 3312cac

File tree

1 file changed

+38
-82
lines changed

1 file changed

+38
-82
lines changed

src/adf2md.lua

Lines changed: 38 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1+
local mark_handlers = {
2+
strong = function(text) return "**" .. text .. "**" end,
3+
em = function(text) return "*" .. text .. "*" end,
4+
code = function(text) return "`" .. text .. "`" end,
5+
strike = function(text) return "~~" .. text .. "~~" end,
6+
link = function(text, attrs) return "[" .. text .. "](" .. attrs.href .. ")" end,
7+
}
8+
19
local function apply_marks(text, marks)
210
if not marks then
311
return text
412
end
513

614
local result = text
715
for _, mark in ipairs(marks) do
8-
if mark.type == "strong" then
9-
result = "**" .. result .. "**"
10-
elseif mark.type == "em" then
11-
result = "*" .. result .. "*"
12-
elseif mark.type == "link" then
13-
result = "[" .. result .. "](" .. mark.attrs.href .. ")"
14-
elseif mark.type == "code" then
15-
result = "`" .. result .. "`"
16-
elseif mark.type == "strike" then
17-
result = "~~" .. result .. "~~"
16+
local handler = mark_handlers[mark.type]
17+
if handler then
18+
result = handler(result, mark.attrs)
1819
end
1920
end
2021
return result
2122
end
2223

2324
local node_handlers = {}
2425

25-
node_handlers.paragraph = function(node, convert_node)
26-
local result = ""
26+
local function collect_children(node, convert_node)
27+
local parts = {}
2728
for _, child in ipairs(node.content or {}) do
28-
if child.type == "text" then
29-
result = result .. apply_marks(child.text, child.marks)
30-
elseif child.type == "hardBreak" then
31-
result = result .. " \n"
32-
else
33-
result = result .. convert_node(child)
34-
end
29+
table.insert(parts, convert_node(child))
3530
end
36-
return result
31+
return table.concat(parts)
32+
end
33+
34+
node_handlers.text = function(node, convert_node)
35+
return apply_marks(node.text, node.marks)
36+
end
37+
38+
node_handlers.hardBreak = function(node, convert_node)
39+
return " \n"
40+
end
41+
42+
node_handlers.paragraph = function(node, convert_node)
43+
return collect_children(node, convert_node)
3744
end
3845

3946
node_handlers.mention = function(node, convert_node)
@@ -58,26 +65,18 @@ node_handlers.status = function(node, convert_node)
5865
return emoji .. " " .. text
5966
end
6067

61-
local function get_list_item_content(item, convert_node)
62-
local content = ""
63-
for _, child in ipairs(item.content or {}) do
64-
content = content .. convert_node(child)
65-
end
66-
return content
67-
end
68-
6968
node_handlers.bulletList = function(node, convert_node)
7069
local items = {}
7170
for _, item in ipairs(node.content or {}) do
72-
table.insert(items, "+ " .. get_list_item_content(item, convert_node))
71+
table.insert(items, "+ " .. collect_children(item, convert_node))
7372
end
7473
return table.concat(items, "\n")
7574
end
7675

7776
node_handlers.orderedList = function(node, convert_node)
7877
local items = {}
7978
for i, item in ipairs(node.content or {}) do
80-
table.insert(items, i .. ". " .. get_list_item_content(item, convert_node))
79+
table.insert(items, i .. ". " .. collect_children(item, convert_node))
8180
end
8281
return table.concat(items, "\n")
8382
end
@@ -89,41 +88,21 @@ node_handlers.taskList = function(node, convert_node)
8988
if item.attrs and item.attrs.state == "DONE" then
9089
checkbox = "[x]"
9190
end
92-
local content = ""
93-
for _, child in ipairs(item.content or {}) do
94-
if child.type == "text" then
95-
content = content .. apply_marks(child.text, child.marks)
96-
else
97-
content = content .. convert_node(child)
98-
end
99-
end
100-
table.insert(items, "- " .. checkbox .. " " .. content)
91+
table.insert(items, "- " .. checkbox .. " " .. collect_children(item, convert_node))
10192
end
10293
return table.concat(items, "\n")
10394
end
10495

10596
node_handlers.taskItem = function(node, convert_node)
106-
local content = ""
107-
for _, child in ipairs(node.content or {}) do
108-
if child.type == "text" then
109-
content = content .. apply_marks(child.text, child.marks)
110-
else
111-
content = content .. convert_node(child)
112-
end
113-
end
114-
return content
97+
return collect_children(node, convert_node)
11598
end
11699

117100
node_handlers.listItem = function(node, convert_node)
118-
return get_list_item_content(node, convert_node)
101+
return collect_children(node, convert_node)
119102
end
120103

121104
node_handlers.blockquote = function(node, convert_node)
122-
local content = ""
123-
for _, child in ipairs(node.content or {}) do
124-
content = content .. convert_node(child)
125-
end
126-
return "> " .. content
105+
return "> " .. collect_children(node, convert_node)
127106
end
128107

129108
node_handlers.panel = function(node, convert_node)
@@ -136,28 +115,15 @@ node_handlers.panel = function(node, convert_node)
136115
}
137116
local panel_type = node.attrs and node.attrs.panelType or "info"
138117
local alert_type = panel_type_map[panel_type] or "NOTE"
139-
140-
local content = ""
141-
for _, child in ipairs(node.content or {}) do
142-
content = content .. convert_node(child)
143-
end
144-
return "> [!" .. alert_type .. "]\n> " .. content
118+
return "> [!" .. alert_type .. "]\n> " .. collect_children(node, convert_node)
145119
end
146120

147121
node_handlers.tableCell = function(node, convert_node)
148-
local content = ""
149-
for _, child in ipairs(node.content or {}) do
150-
content = content .. convert_node(child)
151-
end
152-
return content
122+
return collect_children(node, convert_node)
153123
end
154124

155125
node_handlers.tableHeader = function(node, convert_node)
156-
local content = ""
157-
for _, child in ipairs(node.content or {}) do
158-
content = content .. convert_node(child)
159-
end
160-
return content
126+
return collect_children(node, convert_node)
161127
end
162128

163129
node_handlers.tableRow = function(node, convert_node)
@@ -187,13 +153,7 @@ end
187153
node_handlers.heading = function(node, convert_node)
188154
local level = node.attrs and node.attrs.level or 1
189155
local prefix = string.rep("#", level)
190-
local content = ""
191-
for _, child in ipairs(node.content or {}) do
192-
if child.type == "text" then
193-
content = content .. apply_marks(child.text, child.marks)
194-
end
195-
end
196-
return prefix .. " " .. content
156+
return prefix .. " " .. collect_children(node, convert_node)
197157
end
198158

199159
node_handlers.codeBlock = function(node, convert_node)
@@ -213,11 +173,7 @@ node_handlers.media = function(node, convert_node)
213173
end
214174

215175
node_handlers.mediaSingle = function(node, convert_node)
216-
local content = ""
217-
for _, child in ipairs(node.content or {}) do
218-
content = content .. convert_node(child)
219-
end
220-
return content
176+
return collect_children(node, convert_node)
221177
end
222178

223179
node_handlers.rule = function(node, convert_node)

0 commit comments

Comments
 (0)