Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
fix: spacing & linebreak issues
Browse files Browse the repository at this point in the history
Fixed issues observed in lua docs.
  • Loading branch information
luckasRanarison committed Dec 1, 2023
1 parent 554d0ac commit 738c5d2
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lua/nvim-devdocs/transpiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ function transpiler:eval(node)
local result = ""
local node_type = node:type()
local node_text = self:get_node_text(node)
local tag_name = self:get_node_tag_name(node)
local attributes = self:get_node_attributes(node)

if node_type == "text" or node_type == "entity" then
result = result .. normalize_html(node_text)
elseif node_type == "element" then
local tag_node = node:named_child()
local tag_type = tag_node:type()
local tag_name = self:get_node_tag_name(node)
local parent_node = node:parent()
local parent_tag_name = self:get_node_tag_name(parent_node)

Expand Down Expand Up @@ -325,6 +325,19 @@ function transpiler:eval(node)
end
end

local parent = node:parent()
local sibling = node:next_named_sibling()

-- checks if there should be additional spaces or linebreaks
if parent and parent:type() == "fragment" and sibling then
local start_row, start_col = node:end_()
local target_row, target_col = sibling:start()
local is_inline = is_inline_tag(tag_name) or not tag_name -- is text

if is_inline and start_col ~= target_col then result = result .. " " end
if is_inline then result = result .. string.rep("\n", target_row - start_row) end
end

-- use the Markdown text for indexing docs in the index.json file
local id = attributes.id

Expand Down Expand Up @@ -358,6 +371,7 @@ function transpiler:eval_child(node, parent_node)
-- skip all the tags to get the actual text offset, see #56
while child do
local child_sibling = child:next_named_sibling()

if child:type() == "start_tag" and child_sibling then
local c_row, c_col = child:end_()
local s_row, s_col = child_sibling:start()
Expand All @@ -375,8 +389,10 @@ function transpiler:eval_child(node, parent_node)
end

local row, col = start_row, start_col

while row ~= target_row or col ~= target_col do
local char = self:get_text_range(row, col, row, col + 1)

if char ~= "" then
result = result .. char
col = col + 1
Expand All @@ -387,7 +403,9 @@ function transpiler:eval_child(node, parent_node)
end
else
local is_inline = is_inline_tag(tag_name) or not tag_name -- is text

if is_inline and start_col ~= target_col then result = result .. " " end
if is_inline then result = result .. string.rep("\n", target_row - start_row) end
end
end

Expand All @@ -414,7 +432,7 @@ function transpiler:eval_table(node)
end

local max_col_len_map = {}
local result_map = {} -- the converted text of each col
local result_map = {} -- the converted text of each col
local colspan_map = {} -- colspan attribute

for i, tr in ipairs(tr_nodes) do
Expand Down Expand Up @@ -515,7 +533,7 @@ M.to_yaml = function(entry)
return table.concat(lines, "\n")
end

---Converts HTML to markdow
---Converts HTML to markdown
---@param html string
---@param section_map table<string, string>?
---@return string, table<string, string>
Expand Down

0 comments on commit 738c5d2

Please sign in to comment.