@@ -4,20 +4,12 @@ local Context = require('render-markdown.request.context')
44local Env = require (' render-markdown.lib.env' )
55local Extmark = require (' render-markdown.lib.extmark' )
66local Iter = require (' render-markdown.lib.iter' )
7+ local handlers = require (' render-markdown.core.handlers' )
78local log = require (' render-markdown.core.log' )
89local state = require (' render-markdown.state' )
910
10- --- @type table<string , render.md.Handler>
11- local builtin_handlers = {
12- html = require (' render-markdown.handler.html' ),
13- latex = require (' render-markdown.handler.latex' ),
14- markdown = require (' render-markdown.handler.markdown' ),
15- markdown_inline = require (' render-markdown.handler.markdown_inline' ),
16- }
17-
1811--- @class render.md.ui.Config
1912--- @field on render.md.on.Config
20- --- @field custom_handlers table<string , render.md.Handler>
2113
2214--- @class render.md.Ui
2315--- @field private config render.md.ui.Config
@@ -165,53 +157,8 @@ function M.parse_buffer(buf, win, config, mode)
165157 local context = Context .start (buf , win , config , mode )
166158 -- make sure injections are processed
167159 context .view :parse (parser )
168- -- parse markdown after other nodes to get accurate state
169- local marks = {} --- @type render.md.Mark[]
170- local markdown = {} --- @type render.md.handler.Context[]
171- parser :for_each_tree (function (tree , language_tree )
172- local language = language_tree :lang ()
173- --- @type render.md.handler.Context
174- local ctx = { buf = buf , root = tree :root () }
175- if language == ' markdown' then
176- markdown [# markdown + 1 ] = ctx
177- else
178- vim .list_extend (marks , M .parse_tree (context , ctx , language ))
179- end
180- end )
181- for _ , ctx in ipairs (markdown ) do
182- vim .list_extend (marks , M .parse_tree (context , ctx , ' markdown' ))
183- end
160+ local marks = handlers .run (context , parser )
184161 return Iter .list .map (marks , Extmark .new )
185162end
186163
187- --- Run user & builtin handlers when available. User handler is always executed,
188- --- builtin handler is skipped if user handler does not specify extends.
189- --- @private
190- --- @param context render.md.request.Context
191- --- @param ctx render.md.handler.Context
192- --- @param language string
193- --- @return render.md.Mark[]
194- function M .parse_tree (context , ctx , language )
195- log .buf (' debug' , ' language' , ctx .buf , language )
196- if not context .view :overlaps (ctx .root ) then
197- return {}
198- end
199-
200- local marks = {}
201- local user = M .config .custom_handlers [language ]
202- if user then
203- log .buf (' debug' , ' handler' , ctx .buf , ' user' )
204- vim .list_extend (marks , user .parse (ctx ))
205- if not user .extends then
206- return marks
207- end
208- end
209- local builtin = builtin_handlers [language ]
210- if builtin then
211- log .buf (' debug' , ' handler' , ctx .buf , ' builtin' )
212- vim .list_extend (marks , builtin .parse (ctx ))
213- end
214- return marks
215- end
216-
217164return M
0 commit comments