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+
19local 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
2122end
2223
2324local 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 )
3744end
3845
3946node_handlers .mention = function (node , convert_node )
@@ -58,26 +65,18 @@ node_handlers.status = function(node, convert_node)
5865 return emoji .. " " .. text
5966end
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-
6968node_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 " )
7574end
7675
7776node_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 " )
8382end
@@ -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 " )
10394end
10495
10596node_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 )
11598end
11699
117100node_handlers .listItem = function (node , convert_node )
118- return get_list_item_content (node , convert_node )
101+ return collect_children (node , convert_node )
119102end
120103
121104node_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 )
127106end
128107
129108node_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 )
145119end
146120
147121node_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 )
153123end
154124
155125node_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 )
161127end
162128
163129node_handlers .tableRow = function (node , convert_node )
187153node_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 )
197157end
198158
199159node_handlers .codeBlock = function (node , convert_node )
@@ -213,11 +173,7 @@ node_handlers.media = function(node, convert_node)
213173end
214174
215175node_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 )
221177end
222178
223179node_handlers .rule = function (node , convert_node )
0 commit comments