Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(help): accept patterns for readme #269

Merged
merged 1 commit into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ return {
-- when the readme opens with :help it will be correctly displayed as markdown
readme = {
root = vim.fn.stdpath("state") .. "/lazy/readme",
files = { "README.md" },
files = { "README.md", "lua/**/README.md" },
-- only generate markdown helptags for plugins that dont have docs
skip_if_doc_exists = true,
},
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/core/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ M.defaults = {
-- when the readme opens with :help it will be correctly displayed as markdown
readme = {
root = vim.fn.stdpath("state") .. "/lazy/readme",
files = { "README.md" },
files = { "README.md", "lua/**/README.md" },
-- only generate markdown helptags for plugins that dont have docs
skip_if_doc_exists = true,
},
Expand Down
15 changes: 10 additions & 5 deletions lua/lazy/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ function M.index(plugin)
if Config.options.readme.skip_if_doc_exists and vim.loop.fs_stat(plugin.dir .. "/doc") then
return {}
end
local files = vim.tbl_flatten(vim.tbl_map(function(file)
return vim.fn.expand(plugin.dir .. "/" .. file, false, true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add Util.norm(vim.fn.expan...) to normalize the paths for windows

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vim.fn.expand will return a list. I'm not quite sure when to normalize, should I do it after expanding? Or before? 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after should be fine. Or just do it in the loop where you loop over the files, right before where you build the tag_filename

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I tested on linux/mac. But not very confident if filename generation would work correctly on Windows.

end, Config.options.readme.files))
---@type table<string,{file:string, tag:string, line:string}>
local tags = {}
for _, file in ipairs(Config.options.readme.files) do
file = plugin.dir .. "/" .. file
for _, file in ipairs(files) do
file = Util.norm(file)
if vim.loop.fs_stat(file) then
local rel_file = file:sub(#plugin.dir + 1)
local tag_filename = string.gsub(plugin.name .. vim.fn.fnamemodify(rel_file, ":h:gs?/?-?"), "-$", "")
local lines = vim.split(Util.read_file(file), "\n")
for _, line in ipairs(lines) do
local title = line:match("^#+%s*(.*)")
if title then
local tag = plugin.name .. "-" .. title:lower():gsub("%W+", "-")
local tag = tag_filename .. "-" .. title:lower():gsub("%W+", "-")
tag = tag:gsub("%-+", "-"):gsub("%-$", "")
line = line:gsub("([%[%]/])", "\\%1")
tags[tag] = { tag = tag, line = line, file = plugin.name .. ".md" }
tags[tag] = { tag = tag, line = line, file = tag_filename .. ".md" }
end
end
table.insert(lines, [[<!-- vim: set ft=markdown: -->]])
Util.write_file(Config.options.readme.root .. "/doc/" .. plugin.name .. ".md", table.concat(lines, "\n"))
Util.write_file(Config.options.readme.root .. "/doc/" .. tag_filename .. ".md", table.concat(lines, "\n"))
end
end
return tags
Expand Down