Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol
diffdown.com
1<!-- NOTE: README.md is generated from src/README.md -->
2
3# @codemirror/lang-html [](https://www.npmjs.org/package/@codemirror/lang-html)
4
5[ [**WEBSITE**](https://codemirror.net/) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/lang-html/blob/main/CHANGELOG.md) ]
6
7This package implements HTML language support for the
8[CodeMirror](https://codemirror.net/) code editor.
9
10The [project page](https://codemirror.net/) has more information, a
11number of [examples](https://codemirror.net/examples/) and the
12[documentation](https://codemirror.net/docs/).
13
14This code is released under an
15[MIT license](https://github.com/codemirror/lang-html/tree/main/LICENSE).
16
17We aim to be an inclusive, welcoming community. To make that explicit,
18we have a [code of
19conduct](http://contributor-covenant.org/version/1/1/0/) that applies
20to communication around the project.
21
22## Usage
23
24```javascript
25import {EditorView, basicSetup} from "codemirror"
26import {html} from "@codemirror/lang-html"
27
28const view = new EditorView({
29 parent: document.body,
30 doc: `<!doctype html>\n<title>HTML</title>`,
31 extensions: [basicSetup, html()]
32})
33```
34
35## API Reference
36
37<dl>
38<dt id="user-content-html">
39 <code><strong><a href="#user-content-html">html</a></strong>(<a id="user-content-html^config" href="#user-content-html^config">config</a>⁠?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> = {}) → <a href="https://codemirror.net/docs/ref#language.LanguageSupport">LanguageSupport</a></code></dt>
40
41<dd><p>Language support for HTML, including
42<a href="#user-content-htmlcompletion"><code>htmlCompletion</code></a> and JavaScript and
43CSS support extensions.</p>
44<dl><dt id="user-content-html^config">
45 <code><strong><a href="#user-content-html^config">config</a></strong></code></dt>
46
47<dd><dl><dt id="user-content-html^config.matchclosingtags">
48 <code><strong><a href="#user-content-html^config.matchclosingtags">matchClosingTags</a></strong>⁠?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
49
50<dd><p>By default, the syntax tree will highlight mismatched closing
51tags. Set this to <code>false</code> to turn that off (for example when you
52expect to only be parsing a fragment of HTML text, not a full
53document).</p>
54</dd><dt id="user-content-html^config.selfclosingtags">
55 <code><strong><a href="#user-content-html^config.selfclosingtags">selfClosingTags</a></strong>⁠?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
56
57<dd><p>By default, the parser does not allow arbitrary self-closing tags.
58Set this to <code>true</code> to turn on support for <code>/></code> self-closing tag
59syntax.</p>
60</dd><dt id="user-content-html^config.autoclosetags">
61 <code><strong><a href="#user-content-html^config.autoclosetags">autoCloseTags</a></strong>⁠?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
62
63<dd><p>Determines whether <a href="#user-content-autoclosetags"><code>autoCloseTags</code></a>
64is included in the support extensions. Defaults to true.</p>
65</dd><dt id="user-content-html^config.extratags">
66 <code><strong><a href="#user-content-html^config.extratags">extraTags</a></strong>⁠?: <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype">Record</a><<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, <a href="#user-content-tagspec">TagSpec</a>></code></dt>
67
68<dd><p>Add additional tags that can be completed.</p>
69</dd><dt id="user-content-html^config.extraglobalattributes">
70 <code><strong><a href="#user-content-html^config.extraglobalattributes">extraGlobalAttributes</a></strong>⁠?: <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype">Record</a><<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, readonly <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>[] | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null">null</a>></code></dt>
71
72<dd><p>Add additional completable attributes to all tags.</p>
73</dd><dt id="user-content-html^config.nestedlanguages">
74 <code><strong><a href="#user-content-html^config.nestedlanguages">nestedLanguages</a></strong>⁠?: {tag: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, attrs⁠?: fn(<a id="user-content-html^config.nestedlanguages.attrs^attrs" href="#user-content-html^config.nestedlanguages.attrs^attrs">attrs</a>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a><<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>>) → <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a>, parser: <a href="https://lezer.codemirror.net/docs/ref/#common.Parser">Parser</a>}[]</code></dt>
75
76<dd><p>Register additional languages to parse the content of specific
77tags. If given, <code>attrs</code> should be a function that, given an
78object representing the tag's attributes, returns <code>true</code> if this
79language applies.</p>
80</dd><dt id="user-content-html^config.nestedattributes">
81 <code><strong><a href="#user-content-html^config.nestedattributes">nestedAttributes</a></strong>⁠?: {name: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, tagName⁠?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, parser: <a href="https://lezer.codemirror.net/docs/ref/#common.Parser">Parser</a>}[]</code></dt>
82
83<dd><p>Register additional languages to parse attribute values with.</p>
84</dd></dl></dd></dl></dd>
85<dt id="user-content-htmllanguage">
86 <code><strong><a href="#user-content-htmllanguage">htmlLanguage</a></strong>: <a href="https://codemirror.net/docs/ref#language.LRLanguage">LRLanguage</a></code></dt>
87
88<dd><p>A language provider based on the <a href="https://github.com/lezer-parser/html">Lezer HTML
89parser</a>, extended with the
90JavaScript and CSS parsers to parse the content of <code><script></code> and
91<code><style></code> tags.</p>
92</dd>
93<dt id="user-content-htmlcompletionsource">
94 <code><strong><a href="#user-content-htmlcompletionsource">htmlCompletionSource</a></strong>(<a id="user-content-htmlcompletionsource^context" href="#user-content-htmlcompletionsource^context">context</a>: <a href="https://codemirror.net/docs/ref#autocomplete.CompletionContext">CompletionContext</a>) → <a href="https://codemirror.net/docs/ref#autocomplete.CompletionResult">CompletionResult</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null">null</a></code></dt>
95
96<dd><p>HTML tag completion. Opens and closes tags and attributes in a
97context-aware way.</p>
98</dd>
99<dt id="user-content-tagspec">
100 <h4>
101 <code>interface</code>
102 <a href="#user-content-tagspec">TagSpec</a></h4>
103</dt>
104
105<dd><p>Type used to specify tags to complete.</p>
106<dl><dt id="user-content-tagspec.attrs">
107 <code><strong><a href="#user-content-tagspec.attrs">attrs</a></strong>⁠?: <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype">Record</a><<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, readonly <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>[] | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null">null</a>></code></dt>
108
109<dd><p>Define tag-specific attributes. Property names are attribute
110names, and property values can be null to indicate free-form
111attributes, or a list of strings for suggested attribute values.</p>
112</dd><dt id="user-content-tagspec.globalattrs">
113 <code><strong><a href="#user-content-tagspec.globalattrs">globalAttrs</a></strong>⁠?: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code></dt>
114
115<dd><p>When set to false, don't complete global attributes on this tag.</p>
116</dd><dt id="user-content-tagspec.children">
117 <code><strong><a href="#user-content-tagspec.children">children</a></strong>⁠?: readonly <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>[]</code></dt>
118
119<dd><p>Can be used to specify a list of child tags that are valid
120inside this tag. The default is to allow any tag.</p>
121</dd></dl>
122
123</dd>
124<dt id="user-content-htmlcompletionsourcewith">
125 <code><strong><a href="#user-content-htmlcompletionsourcewith">htmlCompletionSourceWith</a></strong>(<a id="user-content-htmlcompletionsourcewith^config" href="#user-content-htmlcompletionsourcewith^config">config</a>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>) → fn(<a id="user-content-htmlcompletionsourcewith^returns^context" href="#user-content-htmlcompletionsourcewith^returns^context">context</a>: <a href="https://codemirror.net/docs/ref#autocomplete.CompletionContext">CompletionContext</a>) → <a href="https://codemirror.net/docs/ref#autocomplete.CompletionResult">CompletionResult</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null">null</a></code></dt>
126
127<dd><p>Create a completion source for HTML extended with additional tags
128or attributes.</p>
129<dl><dt id="user-content-htmlcompletionsourcewith^config">
130 <code><strong><a href="#user-content-htmlcompletionsourcewith^config">config</a></strong></code></dt>
131
132<dd><dl><dt id="user-content-htmlcompletionsourcewith^config.extratags">
133 <code><strong><a href="#user-content-htmlcompletionsourcewith^config.extratags">extraTags</a></strong>⁠?: <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype">Record</a><<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, <a href="#user-content-tagspec">TagSpec</a>></code></dt>
134
135<dd><p>Define extra tag names to complete.</p>
136</dd><dt id="user-content-htmlcompletionsourcewith^config.extraglobalattributes">
137 <code><strong><a href="#user-content-htmlcompletionsourcewith^config.extraglobalattributes">extraGlobalAttributes</a></strong>⁠?: <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype">Record</a><<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, readonly <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>[] | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null">null</a>></code></dt>
138
139<dd><p>Add global attributes that are available on all tags.</p>
140</dd></dl></dd></dl></dd>
141<dt id="user-content-autoclosetags">
142 <code><strong><a href="#user-content-autoclosetags">autoCloseTags</a></strong>: <a href="https://codemirror.net/docs/ref#state.Extension">Extension</a></code></dt>
143
144<dd><p>Extension that will automatically insert close tags when a <code>></code> or
145<code>/</code> is typed.</p>
146</dd>
147</dl>