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

Commit fedf727

Browse files
change!: build the docs at download time & add picker previewer
Docs are now converted to markdown and splited into multiple files at download time, installation and uninstall behavior also changed so old docs and index.json are now incompatible with the plugin. The picker now also support preview for global search.
1 parent 4380863 commit fedf727

File tree

7 files changed

+510
-399
lines changed

7 files changed

+510
-399
lines changed

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,25 @@ Here is the default configuration:
6060
{
6161
dir_path = vim.fn.stdpath("data") .. "/devdocs", -- installation directory
6262
telescope = {}, -- passed to the telescope picker
63-
telescope_alt = { -- when searching globally without preview
64-
layout_config = {
65-
width = 75,
66-
},
67-
},
6863
float_win = { -- passed to nvim_open_win(), see :h api-floatwin
6964
relative = "editor",
7065
height = 25,
7166
width = 100,
7267
border = "rounded",
7368
},
74-
previewer_cmd = nil, -- like glow
75-
cmd_args = {}, -- example using glow { "-s", "dark", "-w", "80" }
7669
wrap = false, -- text wrap, only applies to floating window
70+
previewer_cmd = nil, -- for example: "glow"
71+
cmd_args = {}, -- example using glow: { "-s", "dark", "-w", "80" }
72+
cmd_ignore = {}, -- ignore cmd rendering for the listed docs
73+
picker_cmd = false, -- use cmd previewer in picker preview
74+
picker_cmd_args = {}, -- example using glow: { "-p" }
7775
ensure_installed = {}, -- get automatically installed
7876
}
7977
```
8078

8179
## Usage
8280

83-
To use the documentations from nvim-devdocs you have to **install** the documentation using `:DevdocsInstall`.
81+
To use the documentations from nvim-devdocs, you need to install it by executing `:DevdocsInstall`. The documentation is indexed and built during the download. Since the building process is done synchronously and may block input, you may want to download larger documents (more than 10MB) in headless mode: `nvim --headless +"DevdocsInstall rust"`.
8482

8583
## Commands
8684

@@ -98,10 +96,6 @@ Available commands:
9896

9997
Commands support completion, and the Telescope picker will be used when no argument is provided.
10098

101-
> ℹ️ **NOTE**:<br>
102-
> At the moment, Telescope's Previewer is available only when opening a specific documentation.
103-
> E.g. `:DevdocsOpen javascript`
104-
10599
## TODO
106100

107101
- More search options

lua/nvim-devdocs/build.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
local path = require("plenary.path")
2+
3+
local notify = require("nvim-devdocs.notify")
4+
local plugin_config = require("nvim-devdocs.config").get()
5+
local html_to_md = require("nvim-devdocs.transpiler").html_to_md
6+
7+
local function build_docs(entry, index, docs)
8+
local alias = entry.slug:gsub("~", "-")
9+
10+
notify.log("Building " .. alias .. " documentation...")
11+
12+
local docs_dir = path:new(plugin_config.dir_path, "docs")
13+
local current_doc_dir = path:new(docs_dir, alias)
14+
local index_path = path:new(plugin_config.dir_path, "index.json")
15+
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
16+
17+
if not docs_dir:exists() then docs_dir:mkdir() end
18+
if not current_doc_dir:exists() then current_doc_dir:mkdir() end
19+
if not index_path:exists() then index_path:write("{}", "w") end
20+
if not lock_path:exists() then lock_path:write("{}", "w") end
21+
22+
local section_map = {}
23+
local path_map = {}
24+
25+
for _, index_entry in pairs(index.entries) do
26+
local splited = vim.split(index_entry.path, "#")
27+
local main = splited[1]
28+
local id = splited[2]
29+
30+
if not section_map[main] then section_map[main] = {} end
31+
if id then table.insert(section_map[main], id) end
32+
end
33+
34+
local count = 1
35+
36+
for key, doc in pairs(docs) do
37+
local sections = section_map[key]
38+
39+
local markdown, md_sections = html_to_md(doc, sections)
40+
41+
for id, md_path in pairs(md_sections) do
42+
path_map[key .. "#" .. id] = count .. "," .. md_path
43+
end
44+
45+
path_map[key] = tostring(count)
46+
47+
local file_path = path:new(current_doc_dir, tostring(count) .. ".md")
48+
49+
file_path:write(markdown, "w")
50+
count = count + 1
51+
end
52+
53+
for i, index_entry in ipairs(index.entries) do
54+
local main = vim.split(index_entry.path, "#")[1]
55+
index.entries[i].path = path_map[index_entry.path] or path_map[main]
56+
end
57+
58+
local index_parsed = vim.fn.json_decode(index_path:read())
59+
index_parsed[alias] = index
60+
index_path:write(vim.fn.json_encode(index_parsed), "w")
61+
62+
local lock_parsed = vim.fn.json_decode(lock_path:read())
63+
lock_parsed[alias] = entry
64+
lock_path:write(vim.fn.json_encode(lock_parsed), "w")
65+
66+
notify.log("Build complete!")
67+
end
68+
69+
return build_docs

lua/nvim-devdocs/config.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ local M = {}
33
local config = {
44
dir_path = vim.fn.stdpath("data") .. "/devdocs",
55
telescope = {},
6-
telescope_alt = {
7-
layout_config = {
8-
width = 75,
9-
},
10-
},
116
float_win = {
127
relative = "editor",
138
height = 25,
149
width = 100,
1510
border = "rounded",
1611
},
12+
wrap = false,
1713
previewer_cmd = nil,
1814
cmd_args = {},
19-
wrap = false,
15+
cmd_ignore = {},
16+
picker_cmd = false,
17+
picker_cmd_args = {},
2018
ensure_installed = {},
2119
}
2220

lua/nvim-devdocs/list.lua

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
local M = {}
22

33
local path = require("plenary.path")
4-
local scandir = require("plenary.scandir")
54

65
local notify = require("nvim-devdocs.notify")
76
local plugin_config = require("nvim-devdocs.config").get()
87

9-
local docs_dir = path:new(plugin_config.dir_path, "docs")
10-
local index_path = path:new(plugin_config.dir_path, "index.json")
8+
local lock_path = path:new(plugin_config.dir_path, "docs-lock.json")
119
local registery_path = path:new(plugin_config.dir_path, "registery.json")
1210

1311
M.get_installed_alias = function()
14-
if not docs_dir:exists() then return {} end
12+
if not lock_path:exists() then return {} end
1513

16-
local files = scandir.scan_dir(path.__tostring(docs_dir), { all_dirs = false })
17-
local installed = vim.tbl_map(function(file_path)
18-
local splited = path._split(path:new(file_path))
19-
local filename = splited[#splited]
20-
local basename = filename:gsub(".json", "")
21-
return basename
22-
end, files)
14+
local lockfile = lock_path:read()
15+
local lock_parsed = vim.fn.json_decode(lockfile)
16+
local installed = vim.tbl_keys(lock_parsed)
2317

2418
return installed
2519
end
@@ -45,20 +39,20 @@ M.get_installed_entry = function()
4539
end
4640

4741
M.get_updatable = function()
48-
if not registery_path:exists() then return {} end
49-
if not index_path:exists() then return {} end
42+
if not registery_path:exists() or not lock_path:exists() then return {} end
5043

5144
local results = {}
52-
local registery_content = registery_path:read()
53-
local registery_parsed = vim.fn.json_decode(registery_content)
54-
local index_content = index_path:read()
55-
local index_parsed = vim.fn.json_decode(index_content)
56-
57-
for alias, value in pairs(index_parsed) do
58-
local slug = alias:gsub("-", "~")
45+
local registery = registery_path:read()
46+
local registery_parsed = vim.fn.json_decode(registery)
47+
local lockfile = lock_path:read()
48+
local lock_parsed = vim.fn.json_decode(lockfile)
5949

50+
for alias, value in pairs(lock_parsed) do
6051
for _, doc in pairs(registery_parsed) do
61-
if doc.slug == slug and doc.mtime > value.mtime then table.insert(results, alias) end
52+
if doc.slug == value.slug and doc.mtime > value.mtime then
53+
table.insert(results, alias)
54+
break
55+
end
6256
end
6357
end
6458

0 commit comments

Comments
 (0)