What is @codemirror/lang-html?
@codemirror/lang-html is a language support package for CodeMirror 6 that provides syntax highlighting, code folding, and other editing features for HTML. It is part of the CodeMirror 6 ecosystem, which is a versatile text editor implemented in JavaScript for the browser.
What are @codemirror/lang-html's main functionalities?
Syntax Highlighting
This code sets up a CodeMirror editor with HTML syntax highlighting. The `html` function from `@codemirror/lang-html` is used to provide the necessary language support.
import { html } from '@codemirror/lang-html';
import { EditorState } from '@codemirror/state';
import { EditorView, basicSetup } from '@codemirror/basic-setup';
const state = EditorState.create({
doc: '<!DOCTYPE html>\n<html>\n <head>\n <title>My HTML</title>\n </head>\n <body>\n <h1>Hello, world!</h1>\n </body>\n</html>',
extensions: [basicSetup, html()]
});
const view = new EditorView({
state,
parent: document.body
});
Code Folding
This code sets up a CodeMirror editor with HTML syntax highlighting and code folding. The `foldGutter` extension is used to add foldable regions to the editor.
import { html } from '@codemirror/lang-html';
import { foldGutter } from '@codemirror/fold';
import { EditorState } from '@codemirror/state';
import { EditorView, basicSetup } from '@codemirror/basic-setup';
const state = EditorState.create({
doc: '<!DOCTYPE html>\n<html>\n <head>\n <title>My HTML</title>\n </head>\n <body>\n <h1>Hello, world!</h1>\n </body>\n</html>',
extensions: [basicSetup, html(), foldGutter()]
});
const view = new EditorView({
state,
parent: document.body
});
Linting
This code sets up a CodeMirror editor with HTML syntax highlighting and linting. The `linter` and `lintGutter` extensions are used to add linting support to the editor.
import { html } from '@codemirror/lang-html';
import { linter, lintGutter } from '@codemirror/lint';
import { EditorState } from '@codemirror/state';
import { EditorView, basicSetup } from '@codemirror/basic-setup';
const state = EditorState.create({
doc: '<!DOCTYPE html>\n<html>\n <head>\n <title>My HTML</title>\n </head>\n <body>\n <h1>Hello, world!</h1>\n </body>\n</html>',
extensions: [basicSetup, html(), lintGutter(), linter((view) => [])]
});
const view = new EditorView({
state,
parent: document.body
});
Other packages similar to @codemirror/lang-html
ace-builds
Ace is a standalone code editor written in JavaScript. It provides syntax highlighting, code folding, and other features for HTML and many other languages. Compared to @codemirror/lang-html, Ace is a more monolithic solution, whereas CodeMirror 6 is modular and allows for more customization.
monaco-editor
Monaco Editor is the code editor that powers VS Code. It provides rich language support for HTML, including syntax highlighting, code folding, and more. Monaco Editor is more feature-rich and integrated with the VS Code ecosystem, while @codemirror/lang-html is part of the more lightweight and modular CodeMirror 6.
prismjs
Prism is a lightweight, extensible syntax highlighter. It supports HTML and many other languages. Unlike @codemirror/lang-html, Prism is focused solely on syntax highlighting and does not provide other editor features like code folding or linting.
0.19.3 (2021-09-23)
New features
The package now exports a completion source function, rather than a prebuilt completion extension.
Use more specific highlighting tags for attribute names and values.