Skip to content

Commit 4d7bb5e

Browse files
committed
add templating library example
1 parent 36f58b3 commit 4d7bb5e

3 files changed

Lines changed: 474 additions & 77 deletions

File tree

README.md

Lines changed: 96 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# SuperHTML
2+
23
HTML Validator, Formatter, LSP, and Templating Language Library
34

45
## SuperHTML CLI Tool
6+
57
The SuperHTML CLI Tool offers **validation** and **autoformatting** features for HTML files.
68

79
The tool can be used either directly (for example by running it on save), or through a LSP client implementation.
@@ -22,152 +24,152 @@ General Options:
2224
--syntax-only Disable HTML element and attribute validation.
2325
```
2426

25-
>[!WARNING]
26-
>SuperHTML only supports HTML5 (the WHATWG living spec) regardless of what you put in your doctype (a warning will be generated for unsupported doctypes).
27+
> [!WARNING]
28+
> SuperHTML only supports HTML5 (the WHATWG living spec) regardless of what you put in your doctype (a warning will be generated for unsupported doctypes).
2729
28-
>[!WARNING]
29-
>Templated HTML (Jinja2, Angular, Mustache, ...) is not yet supported when all validation rules are enabled, use `--syntax-only` (or the relative Extension Setting in VSCode) to limit validation to syntax errors to use SuperHTML with templated HTML documents.
30+
> [!WARNING]
31+
> Templated HTML (Jinja2, Angular, Mustache, ...) is not yet supported when all validation rules are enabled, use `--syntax-only` (or the relative Extension Setting in VSCode) to limit validation to syntax errors to use SuperHTML with templated HTML documents.
3032
>
31-
> Compatibility with popular templating languages is being explored.
32-
33+
> Compatibility with popular templating languages is being explored.
3334
3435
### Diagnostics
36+
3537
SuperHTML validates not only syntax but also element nesting and attribute values.
3638
No other language server implements the full HTML spec in its validation code.
3739

3840
![](.github/helix.png)
3941

40-
4142
### Autoformatting
43+
4244
The autoformatter has two main ways of interacting with it in order to request for horizontal / vertical alignment.
4345

4446
1. Adding / removing whitespace between the **start tag** of an element and its content.
45-
2. Adding / removing whitespace between the **last attribute** of a start tag and the closing `>`.
46-
47-
48-
>[!TIP]
49-
>Consider using `superhtml fmt --check` in your CI to enforce every change to be performed on normalized HTML files. This is a technique commonly used in Zig (and Go) for source code that can also help streamline frontend development.
47+
2. Adding / removing whitespace between the **last attribute** of a start tag and the closing `>`.
5048

49+
> [!TIP]
50+
> Consider using `superhtml fmt --check` in your CI to enforce every change to be performed on normalized HTML files. This is a technique commonly used in Zig (and Go) for source code that can also help streamline frontend development.
5151
5252
#### Example of rule #1
53+
5354
Before:
55+
5456
```html
55-
<div> <p>Foo</p></div>
57+
<div><p>Foo</p></div>
5658
```
5759

5860
After:
61+
5962
```html
6063
<div>
61-
<p>Foo</p>
64+
<p>Foo</p>
6265
</div>
6366
```
6467

6568
##### Reverse
6669

6770
Before:
71+
6872
```html
69-
<div><p>Foo</p>
70-
</div>
73+
<div><p>Foo</p></div>
7174
```
7275

7376
After:
77+
7478
```html
7579
<div><p>Foo</p></div>
7680
```
7781

7882
### Example of rule #2
83+
7984
Before:
85+
8086
```html
81-
<div foo="bar" style="verylongstring" hidden >
82-
Foo
83-
</div>
87+
<div foo="bar" style="verylongstring" hidden>Foo</div>
8488
```
8589

8690
After:
91+
8792
```html
88-
<div foo="bar"
89-
style="verylongstring"
90-
hidden
91-
>
92-
Foo
93-
</div>
93+
<div foo="bar" style="verylongstring" hidden>Foo</div>
9494
```
9595

9696
#### Reverse
9797

9898
Before:
99+
99100
```html
100-
<div foo="bar"
101-
style="verylongstring"
102-
hidden>
103-
Foo
104-
</div>
101+
<div foo="bar" style="verylongstring" hidden>Foo</div>
105102
```
106103

107104
After:
105+
108106
```html
109-
<div foo="bar" style="verylongstring" hidden>
110-
Foo
111-
</div>
107+
<div foo="bar" style="verylongstring" hidden>Foo</div>
112108
```
113109

114110
### Download
111+
115112
See the Releases section here on GitHub.
116113

117114
### Editor support
115+
118116
#### VSCode
117+
119118
Install the [Super HTML VSCode extension](https://marketplace.visualstudio.com/items?itemName=LorisCro.super) (doesn't require the CLI tool as it bundles a WASM build of the language server).
120119

121120
#### Neovim
121+
122122
1. Download a prebuilt version of `superhtml` from the Releases section (or build it yourself).
123123
2. Put `superhtml` in your `PATH`.
124124
3. Configure `superhtml` for your chosen lsp
125-
126-
- ##### [Neovim Built-In](https://neovim.io/doc/user/lsp.html#vim.lsp.start())
127-
128-
```lua
129-
vim.api.nvim_create_autocmd("Filetype", {
130-
pattern = { "html", "shtml", "htm" },
131-
callback = function()
132-
vim.lsp.start({
133-
name = "superhtml",
134-
cmd = { "superhtml", "lsp" },
135-
root_dir = vim.fs.dirname(vim.fs.find({".git"}, { upward = true })[1])
136-
})
137-
end
138-
})
139-
```
140-
141-
- ##### [LspZero](https://github.com/VonHeikemen/lsp-zero.nvim)
142-
143-
```lua
144-
local lsp = require("lsp-zero")
145-
146-
require('lspconfig.configs').superhtml = {
147-
default_config = {
148-
name = 'superhtml',
149-
cmd = {'superhtml', 'lsp'},
150-
filetypes = {'html', 'shtml', 'htm'},
151-
root_dir = require('lspconfig.util').root_pattern('.git')
152-
}
153-
}
154-
155-
lsp.configure('superhtml', {force_setup = true})
156-
```
125+
- ##### [Neovim Built-In](<https://neovim.io/doc/user/lsp.html#vim.lsp.start()>)
126+
127+
```lua
128+
vim.api.nvim_create_autocmd("Filetype", {
129+
pattern = { "html", "shtml", "htm" },
130+
callback = function()
131+
vim.lsp.start({
132+
name = "superhtml",
133+
cmd = { "superhtml", "lsp" },
134+
root_dir = vim.fs.dirname(vim.fs.find({".git"}, { upward = true })[1])
135+
})
136+
end
137+
})
138+
```
139+
140+
- ##### [LspZero](https://github.com/VonHeikemen/lsp-zero.nvim)
141+
142+
```lua
143+
local lsp = require("lsp-zero")
144+
145+
require('lspconfig.configs').superhtml = {
146+
default_config = {
147+
name = 'superhtml',
148+
cmd = {'superhtml', 'lsp'},
149+
filetypes = {'html', 'shtml', 'htm'},
150+
root_dir = require('lspconfig.util').root_pattern('.git')
151+
}
152+
}
153+
154+
lsp.configure('superhtml', {force_setup = true})
155+
```
157156

158157
#### Helix
159-
In versions later than `24.07` `superhtml` is supported out of the box, simply add executable to your `PATH`.
160158

159+
In versions later than `24.07` `superhtml` is supported out of the box, simply add executable to your `PATH`.
161160

162161
#### [Flow Control](https://github.com/neurocyte/flow)
162+
163163
Already defaults to using SuperHTML, just add the executable to your `PATH`.
164164

165165
#### Vim
166+
166167
Vim should be able to parse the errors that `superhtml check [PATH]` generates.
167168
This means that you can use `:make` and the quickfix window to check for syntax
168169
errors.
169170

170171
Set the `makeprg` to the following in your .vimrc:
172+
171173
```
172174
" for any html file, a :make<cr> action will populate the quickfix menu
173175
autocmd filetype html setlocal makeprg=superhtml\ check\ %
@@ -176,26 +178,29 @@ autocmd filetype html setlocal formatprg=superhtml\ fmt\ --stdin
176178
```
177179
178180
#### Zed
181+
179182
See [WeetHet/superhtml-zed](https://github.com/WeetHet/superhtml-zed).
180183
181184
#### Other editors
182-
Follow your editor specific instructions on how to define a new Language Server for a given language / file format.
183185
184-
*(Also feel free to contribute more specific instructions to this readme / add files under the `editors/` subdirectory).*
186+
Follow your editor specific instructions on how to define a new Language Server for a given language / file format.
185187
188+
_(Also feel free to contribute more specific instructions to this readme / add files under the `editors/` subdirectory)._
186189
187190
## FAQs
191+
188192
### Why doesn't SuperHTML support self-closing tags?
193+
189194
Because self-closing tags don't exist in HTML and, while harmless when used with void elements, it just keeps misleating people into thinking that you can self-close HTML tags.
190195
191196
In particular, given this HTML code:
192197
193198
```html
194199
<!doctype html>
195-
<html>
200+
<html>
196201
<head></head>
197202
<body>
198-
<div/>
203+
<div />
199204
<p></p>
200205
</body>
201206
</html>
@@ -208,9 +213,11 @@ Add to that the fact that tooling like the default HTML formatter in VSCode will
208213
Related: [#100](https://github.com/kristoff-it/superhtml/pull/100).
209214

210215
### Why doesn't SuperHTML report duplicate values in `[class]` as an error?
211-
The HTML spec defines the global `class` attribute as a space-separated list of tokens, as opposed to a space-separated list of *unique* tokens, like some other attributes are (e.g. `accesskey`).
216+
217+
The HTML spec defines the global `class` attribute as a space-separated list of tokens, as opposed to a space-separated list of _unique_ tokens, like some other attributes are (e.g. `accesskey`).
212218

213219
### Why is `<style>` under `<body>` an error? It works in all browsers!
220+
214221
As far as I'm concerned, there is no good reason to forbid `<style>` in body, but that's what the HTML spec does.
215222

216223
Related upstream issue: https://github.com/whatwg/html/issues/1605
@@ -222,23 +229,24 @@ One common example is `<li>`, which enables this usage pattern:
222229

223230
```html
224231
<ul>
225-
<li> First point
226-
<li> Second point
227-
</ul>
232+
<li>First point</li>
233+
<li>Second point</li>
234+
</ul>
228235
```
229236

230237
The reason why this is not ambiguous is that `<li>`cannot be nested inside
231238
another `<li>` so whe the second sibling is encountered, it's possible to always
232239
correctly implicitly close the first `<li>`.
233240

234241
SuperHTML breaks compatibility with the HTML spec in this regard for one main reason:
235-
while implicitly closed tags are unambiguous in *valid* HTML documents, it creates a
242+
while implicitly closed tags are unambiguous in _valid_ HTML documents, it creates a
236243
problematic gray area when it comes to typos.
237244

238245
Consider the following snippet:
239246

240247
```html
241-
<li>first point<li>
248+
<li>first point</li>
249+
<li></li>
242250
```
243251

244252
If SuperHTML were to follow the HTML spec it would have to consider this valid
@@ -254,16 +262,26 @@ If you want to write HTML code that leverages this feature, do not use SuperHTML
254262
as there are no plans to ever support it.
255263

256264
#### But what about Google AMP?
265+
257266
Why are you letting an ad company decide what the world wide web should look like.
258267
Do you want ads? Because that's how you get ads.
259268

260269
## Templating Language Library
261-
SuperHTML is also a HTML templating language. More on that soon.
270+
271+
SuperHTML is also a HTML templating language.
272+
273+
First make sure to familiarize yourself with the process to setup a
274+
ScriptyVM (see https://github.com/kristoff-it/scripty).
275+
276+
See `src/example.zig` for more info. Note that the API is not polished yet and
277+
will change over time.
262278

263279
## Contributing
280+
264281
SuperHTML tracks the latest Zig release (0.15.1 at the moment of writing).
265282

266283
### Contributing to the HTML parser & LSP
284+
267285
Contributing to the HTML parser and LSP doesn't require you to be familiar with the templating language, basically limiting the scope of what you have to worry about to:
268286

269287
- `src/cli.zig`
@@ -277,6 +295,7 @@ You can invoke `zig build test` to run all unit tests.
277295
Running `zig build` will compile the SuperHTML CLI tool, allowing you to also then test the LSP behavior directly from your favorite editor.
278296

279297
For testing within VSCode:
298+
280299
1. Run `zig build wasm -p src/editors/vscode/wasm`
281300
2. Open `src/editors/vscode` in VSCode
282301
3. Start debugging.

0 commit comments

Comments
 (0)