hugo.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. local M = {
  2. "phelipetls/vim-hugo",
  3. }
  4. M.config = function()
  5. local function is_hugo_project()
  6. local config_files = { "hugo.toml", "hugo.yaml", "hugo.json" }
  7. for _, file in ipairs(config_files) do
  8. if vim.fn.glob(file) ~= "" then
  9. return true
  10. end
  11. end
  12. return false
  13. end
  14. local function get_archetypes()
  15. local kinds = {}
  16. local archetype_paths = {
  17. "archetypes/",
  18. "themes/*/archetypes/",
  19. }
  20. for _, path in ipairs(archetype_paths) do
  21. local files = vim.fn.glob(path .. "*.md", false, true)
  22. for _, file in ipairs(files) do
  23. local kind = vim.fn.fnamemodify(file, ":t:r")
  24. table.insert(kinds, kind)
  25. end
  26. end
  27. return kinds
  28. end
  29. local function get_content_completions(lead)
  30. local search_path = lead:match("^content/") and lead or "content/" .. lead
  31. local matches = vim.fn.glob("content/**/*", false, true)
  32. local completions = {}
  33. for _, match in ipairs(matches) do
  34. local display = match:gsub("^content/", "")
  35. if match:match("^" .. vim.pesc(search_path)) then
  36. if vim.fn.isdirectory(match) == 2 then
  37. display = display .. "/"
  38. end
  39. table.insert(completions, display)
  40. end
  41. end
  42. return completions
  43. end
  44. local function run_hugo_command(args)
  45. local cmd = "hugo " .. table.concat(args, " ")
  46. vim.cmd("!" .. cmd)
  47. end
  48. local function hugo_complete_arglead(lead, cmd_line, _)
  49. cmd_line = cmd_line:gsub("%s+", " ")
  50. local parts = vim.split(cmd_line, " ")
  51. local basic_subcommands = {
  52. "server",
  53. "new",
  54. "help",
  55. "version",
  56. "config",
  57. }
  58. if #parts <= 2 then
  59. return vim.tbl_filter(function(sub)
  60. return sub:match("^" .. vim.pesc(lead or ""))
  61. end, basic_subcommands)
  62. end
  63. if parts[2] == "new" then
  64. if #parts == 3 then
  65. return { "content" }
  66. end
  67. if parts[3] == "content" then
  68. -- If the previous part is -k or --kind
  69. if parts[#parts - 1] == "-k" or parts[#parts - 1] == "--kind" then
  70. return get_archetypes()
  71. end
  72. return get_content_completions(lead)
  73. end
  74. end
  75. return {}
  76. end
  77. if is_hugo_project() then
  78. vim.api.nvim_create_user_command("Hugo", function(opts)
  79. run_hugo_command(opts.fargs)
  80. end, {
  81. nargs = "*",
  82. complete = hugo_complete_arglead,
  83. })
  84. end
  85. end
  86. return M