能否将htmlhint作为mfussenegger/nvim-lint的HTML代码检查器?
解决nvim-lint + htmlhint诊断不显示的问题
1. 先确认htmlhint已正确安装
确保全局或项目本地安装了htmlhint:
# 全局安装 npm install -g htmlhint # 项目本地安装(推荐) npm install htmlhint --save-dev
2. 自定义htmlhint的linter配置
问题根源是htmlhint默认输出格式无法被nvim-lint正确解析,需要强制输出JSON格式并自定义解析逻辑。在你的lint.lua中替换原有配置为:
local lint = require('lint') -- 自定义htmlhint linter配置 lint.linters.htmlhint = { cmd = 'htmlhint', -- 本地安装的话改为'./node_modules/.bin/htmlhint' stdin = true, args = {'--format', 'json', '-'}, -- 强制输出JSON并从标准输入读取内容 ignore_exitcode = true, parser = function(output, bufnr) -- 解析htmlhint的JSON输出 local ok, result = pcall(vim.json.decode, output) if not ok or not result then return {} end local diagnostics = {} for _, issue in ipairs(result) do table.insert(diagnostics, { source = 'htmlhint', -- Neovim诊断使用0-based行列号,htmlhint输出是1-based lnum = issue.line - 1, col = issue.column - 1, end_lnum = issue.line - 1, end_col = issue.column - 1, severity = issue.type == 'error' and vim.diagnostic.severity.ERROR or vim.diagnostic.severity.WARN, message = issue.message, code = issue.rule.id, }) end return diagnostics end, } -- 关联文件类型与linter lint.linters_by_ft = { html = { 'htmlhint' }, } -- 配置保存时自动触发lint vim.api.nvim_create_autocmd({ 'BufWritePost', 'BufEnter' }, { callback = function() lint.try_lint() end, })
3. 验证配置
- 保存配置后重启Neovim
- 打开一个有问题的HTML文件,手动触发lint:运行
:lua require('lint').try_lint('htmlhint') - 查看诊断信息:运行
:lua vim.diagnostic.open_float()
常见排查点
- 检查
.htmlhintrc配置文件,确保没有禁用所有规则 - 手动在终端执行
htmlhint --format json your-file.html,确认输出是合法的JSON格式 - 确认nvim-lint已通过Lazy正确加载,Lazy配置示例:
{ 'mfussenegger/nvim-lint', config = function() require('lint') require('config.lint') -- 导入你的lint.lua配置文件 end }
内容的提问来源于stack exchange,提问作者antiloquax
相关产品推荐
相关产品推荐

