Use tree-sitter-superhtml for parsing
maintree-sitter-superhtml package provides a Tree-sitter grammar for parsing SuperHTML. It is a fork of the standard tree-sitter-html grammar, specifically adapted for the SuperHTML specification.repository·main·Indexed 23 days ago
https://github.com/kristoff-it/superhtmlA high-fidelity HTML validator, formatter, and Language Server Protocol (LSP) implementation focusing on strict adherence to the WHATWG HTML5 living spec. It provides deep validation for element nesting and attribute correctness. The toolset includes a CLI for checking and formatting, a Tree-Sitter grammar (tree-sitter-superhtml), and integrations for VSCode, BBEdit, Emacs, Neovim, and Vim.
tree-sitter-superhtml package provides a Tree-sitter grammar for parsing SuperHTML. It is a fork of the standard tree-sitter-html grammar, specifically adapted for the SuperHTML specification.<li> to be left unclosed (implicitly closing when a sibling is encountered), SuperHTML requires explicit closing tags. This is done to prevent ambiguity caused by typos; an unclosed tag might be a legitimate implicit closure or simply a developer error. SuperHTML prioritizes strictness to catch these errors.The autoformatter manages horizontal and vertical alignment using two main rules:
>: Controls whitespace between the final attribute of a start tag and the closing bracket.Example Rule 1 (Content Alignment): Before:
<div> <p>Foo</p></div>After:
<div>
<p>Foo</p>
</div>Example Rule 2 (Attribute Alignment): Before:
<div foo="bar" style="verylongstring" hidden >Foo</div>After:
<div foo="bar"
style="verylongstring"
hidden
>Foo</div><div />) because they do not exist in the HTML spec. While browsers ignore the slash in void elements, using them on non-void elements (like <div>) can lead to incorrect DOM structures where subsequent elements are treated as children rather than siblings. SuperHTML enforces correct HTML behavior to prevent these common misconceptions.You can integrate SuperHTML into Emacs using the built-in eglot client (available in Emacs 29+). To use this integration, ensure the superhtml binary is present in your $PATH.
If you use a mode other than web-mode, substitute it in the configuration. The :language-id "html" property is required to ensure eglot correctly identifies the content type as HTML when communicating with the server.
;; Using use-package
(use-package eglot
:defer t
:hook ((web-mode . eglot-ensure)
)
:config
(add-to-list 'eglot-server-programs '((web-mode :language-id "html") . ("superhtml" "lsp"))))To use SuperHTML as the Language Server Protocol (LSP) provider in BBEdit, you must override the default HTML language settings. This replaces the default vscode-html-languageserver with superhtml.
BBEdit > Settings or ⌘+,).html-languageserver and —stdio values.superhtml in the Command text box.lsp in the Arguments box.Once configured, SuperHTML will provide error and warning diagnostics for HTML documents opened in BBEdit.
To use SuperHTML in Neovim, download a prebuilt version of superhtml and ensure it is in your PATH. You can configure it using the built-in LSP client or LspZero.
# Neovim Built-In
vim.api.nvim_create_autocmd("Filetype", {
pattern = { "html", "shtml", "htm" },
callback = function()
vim.lsp.start({
name = "superhtml",
cmd = { "superhtml", "lsp" },
root_dir = vim.fs.dirname(vim.fs.find({".git"}, { upward = true })[1])
})
end
})
# LspZero
local lsp = require("lsp-zero")
require('lspconfig.configs').superhtml = {
default_config = {
name = 'superhtml',
cmd = {'superhtml', 'lsp'},
filetypes = {'html', 'shtml', 'htm'},
root_dir = require('lspconfig.util').root_pattern('.git')
}
}
lsp.configure('superhtml', {force_setup = true})The SuperHTML VSCode extension provides a Language Server for HTML that performs syntax validation, element nesting checks, and attribute value validation.
To avoid conflicts with end tag suggestions, you must disable the built-in VSCode HTML extension. SuperHTML cannot disable these suggestions automatically. You can find manual instructions for disabling the built-in extension here.
Use the check command to validate documents for syntax, element nesting, and attribute values. Use the fmt command to autoformat documents.
Tip: Use superhtml fmt --check in your CI/CD pipelines to enforce that all changes are performed on normalized HTML files.
If you do not use use-package, you can configure SuperHTML for eglot by requiring the library and using with-eval-after-load to add the server program to the eglot-server-programs list.
(require 'eglot)
(with-eval-after-load 'eglot
(add-to-list 'eglot-server-programs
`((web-mode :language-id "html") . ("superhtml" "lsp"))))You can use SuperHTML in Vim by setting makeprg for error checking via :make and formatprg for formatting via gq motions.
" for any html file, a :make<cr> action will populate the quickfix menu
autocmd filetype html setlocal makeprg=superhtml\ check\ %
" if you want to use gq{motion} to format sections or the whole buffer (with gggqG)
autocmd filetype html setlocal formatprg=superhtml\ fmt\ --stdin