A Neovim plugin enabling you to solve LeetCode problems.
feat(parser): improved link handling
2 files changed, +36 -21
+33-18lua/leetcode/parser/init.lua
| @@ -150,16 +150,31 @@ end | ||
| 150 | 150 | ---@return NuiLine |
| 151 | 151 | function Parser:handle_link(text, tag_data) |
| 152 | 152 | 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 "" | |
| 157 | 172 | end |
| 158 | - if not href then return line end | |
| 159 | 173 | |
| 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") | |
| 163 | 178 | |
| 164 | 179 | return line |
| 165 | 180 | end |
| @@ -189,7 +204,7 @@ end | ||
| 189 | 204 | ---@param node TSNode |
| 190 | 205 | ---@param tags lc.Parser.Tag |
| 191 | 206 | --@param tag_data lc.Parser.Tag |
| 192 | -function Parser:node_hi(node, tags, tag_data) | |
| 207 | +function Parser:node_hl(node, tags, tag_data) | |
| 193 | 208 | local text = self:get_text(node) |
| 194 | 209 | local tag = tags[1] |
| 195 | 210 | |
| @@ -209,8 +224,12 @@ function Parser:node_hi(node, tags, tag_data) | ||
| 209 | 224 | if tag == "sup" then text = "^" .. text end |
| 210 | 225 | if tag == "sub" then text = "_" .. text end |
| 211 | 226 | |
| 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 | |
| 214 | 233 | |
| 215 | 234 | self.line:append(nui_text) |
| 216 | 235 | end |
| @@ -221,15 +240,12 @@ end | ||
| 221 | 240 | ---@param tags table |
| 222 | 241 | function Parser:rec_parse(node, tags) |
| 223 | 242 | 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 | |
| 226 | 243 | |
| 227 | - ---handle rest | |
| 228 | 244 | for child in node:iter_children() do |
| 229 | 245 | local ntype = child:type() |
| 230 | 246 | |
| 231 | 247 | 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 | |
| 233 | 249 | self:rec_parse(child, tags) |
| 234 | 250 | if tag_data then table.remove(tags, 1) end |
| 235 | 251 | end |
| @@ -250,7 +266,7 @@ local function normalize_html(str) | ||
| 250 | 266 | "<p><strong[^>]*>(Constraints:)</strong></p>(\n*)", |
| 251 | 267 | "\n\n<constraints> %1</constraints>\n\n" |
| 252 | 268 | ) |
| 253 | - :gsub("(<code[^>]*>)(.-)(</code>)", "%1&lccode;%2&lccode;%3") | |
| 269 | + :gsub("<img([^>]*)/>\n*", "<img%1>img</img>") | |
| 254 | 270 | :gsub("<pre>\n*(.-)\n*</pre>", "<pre>\n%1</pre>") |
| 255 | 271 | :gsub("\n*<p> </p>\n*", "&lcpad;") |
| 256 | 272 | :gsub("\n", "&lcnl;") |
| @@ -297,14 +313,13 @@ function Parser:parse() | ||
| 297 | 313 | self:plain_parser() |
| 298 | 314 | end |
| 299 | 315 | |
| 316 | + log.debug(self.str) | |
| 300 | 317 | return self:trim() |
| 301 | 318 | end |
| 302 | 319 | |
| 303 | 320 | ---@param str string |
| 304 | 321 | ---@param lang string |
| 305 | 322 | function Parser:init(str, lang) |
| 306 | - log.debug(str) | |
| 307 | - | |
| 308 | 323 | local obj = setmetatable({ |
| 309 | 324 | str = str, |
| 310 | 325 | ts = vim.treesitter, |
+3-3lua/leetcode/theme/default.lua
| @@ -32,15 +32,15 @@ M.get = function() | ||
| 32 | 32 | error = { fg = hl("DiagnosticError").fg }, |
| 33 | 33 | |
| 34 | 34 | 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 }, | |
| 37 | 37 | bold = { bold = true }, |
| 38 | 38 | |
| 39 | 39 | code = { fg = hl("Type").fg, bg = hl("Normal").bg }, |
| 40 | 40 | example = { fg = hl("DiagnosticHint").fg }, |
| 41 | 41 | constraints = { fg = hl("DiagnosticInfo").fg }, |
| 42 | 42 | indent = { fg = hl("Comment").fg }, |
| 43 | - link = { fg = hl("Function").fg }, | |
| 43 | + link = { fg = hl("Function").fg, underline = true }, | |
| 44 | 44 | list = { fg = hl("Tag").fg }, |
| 45 | 45 | } |
| 46 | 46 | end |