irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(parser): improved link handling

kawreauthored
parent 9928d34commit ef5a8181d28bf7f605fa951b30dc41927c53969cBrowse files

2 files changed, +36 -21

+33-18lua/leetcode/parser/init.lua
@@ -150,16 +150,31 @@ end
150150 ---@return NuiLine
151151 function Parser:handle_link(text, tag_data)
152152 local line = NuiLine()
153-
154- local href
155- for _, attr in ipairs(tag_data.attrs) do
156- if attr.name == "href" then href = attr.value end
153+ local tag = tag_data.tag
154+ local link
155+
156+ if tag == "a" then
157+ local href = vim.tbl_filter(function(attr)
158+ if attr.name == "href" then return attr end
159+ end, tag_data.attrs)[1]
160+
161+ link = href.value or ""
162+ elseif tag == "img" then
163+ local alt = vim.tbl_filter(function(attr)
164+ if attr.name == "alt" then return attr end
165+ end, tag_data.attrs)[1]
166+ text = alt.value ~= "" and alt.value or "img"
167+
168+ local src = vim.tbl_filter(function(attr)
169+ if attr.name == "src" then return attr end
170+ end, tag_data.attrs)[1]
171+ link = src.value or ""
157172 end
158- if not href then return line end
159173
160- line:append("[", "leetcode_indent")
161- line:append(text, "leetcode_link")
162- line:append(string.format("](%s)", href), "leetcode_indent")
174+ line:append(text, "leetcode_list")
175+ line:append("->(", "leetcode_normal")
176+ line:append(link, "leetcode_link")
177+ line:append(")", "leetcode_normal")
163178
164179 return line
165180 end
@@ -189,7 +204,7 @@ end
189204 ---@param node TSNode
190205 ---@param tags lc.Parser.Tag
191206 --@param tag_data lc.Parser.Tag
192-function Parser:node_hi(node, tags, tag_data)
207+function Parser:node_hl(node, tags, tag_data)
193208 local text = self:get_text(node)
194209 local tag = tags[1]
195210
@@ -209,8 +224,12 @@ function Parser:node_hi(node, tags, tag_data)
209224 if tag == "sup" then text = "^" .. text end
210225 if tag == "sub" then text = "_" .. text end
211226
212- local nui_text = tag == "a" and self:handle_link(text, tag_data)
213- or NuiText(text, utils.hi(tags))
227+ local nui_text
228+ if tag == "a" or tag == "img" then
229+ nui_text = self:handle_link(text, tag_data)
230+ else
231+ nui_text = NuiText(text, utils.hi(tags))
232+ end
214233
215234 self.line:append(nui_text)
216235 end
@@ -221,15 +240,12 @@ end
221240 ---@param tags table
222241 function Parser:rec_parse(node, tags)
223242 local tag_data = self:get_tag_data(node)
224- ---handle img
225- if tag_data and tag_data.tag == "img" then return self:handle_img(tag_data) end
226243
227- ---handle rest
228244 for child in node:iter_children() do
229245 local ntype = child:type()
230246
231247 if tag_data then table.insert(tags, 1, tag_data.tag) end
232- if ntype == "text" or ntype == "entity" then self:node_hi(child, tags, tag_data) end
248+ if ntype == "text" or ntype == "entity" then self:node_hl(child, tags, tag_data) end
233249 self:rec_parse(child, tags)
234250 if tag_data then table.remove(tags, 1) end
235251 end
@@ -250,7 +266,7 @@ local function normalize_html(str)
250266 "<p><strong[^>]*>(Constraints:)</strong></p>(\n*)",
251267 "\n\n<constraints> %1</constraints>\n\n"
252268 )
253- :gsub("(<code[^>]*>)(.-)(</code>)", "%1&lccode;%2&lccode;%3")
269+ :gsub("<img([^>]*)/>\n*", "<img%1>img</img>")
254270 :gsub("<pre>\n*(.-)\n*</pre>", "<pre>\n%1</pre>")
255271 :gsub("\n*<p>&nbsp;</p>\n*", "&lcpad;")
256272 :gsub("\n", "&lcnl;")
@@ -297,14 +313,13 @@ function Parser:parse()
297313 self:plain_parser()
298314 end
299315
316+ log.debug(self.str)
300317 return self:trim()
301318 end
302319
303320 ---@param str string
304321 ---@param lang string
305322 function Parser:init(str, lang)
306- log.debug(str)
307-
308323 local obj = setmetatable({
309324 str = str,
310325 ts = vim.treesitter,
+3-3lua/leetcode/theme/default.lua
@@ -32,15 +32,15 @@ M.get = function()
3232 error = { fg = hl("DiagnosticError").fg },
3333
3434 alt = { fg = hl("Comment").fg },
35- normal = { fg = hl("FloatTitle").fg },
36- italic = { fg = hl("FloatTitle").fg, italic = true },
35+ normal = { fg = hl("Delimiter").fg },
36+ italic = { fg = hl("Delimiter").fg, italic = true },
3737 bold = { bold = true },
3838
3939 code = { fg = hl("Type").fg, bg = hl("Normal").bg },
4040 example = { fg = hl("DiagnosticHint").fg },
4141 constraints = { fg = hl("DiagnosticInfo").fg },
4242 indent = { fg = hl("Comment").fg },
43- link = { fg = hl("Function").fg },
43+ link = { fg = hl("Function").fg, underline = true },
4444 list = { fg = hl("Tag").fg },
4545 }
4646 end