Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol
diffdown.com
1# @codemirror/language [](https://www.npmjs.org/package/@codemirror/language)
2
3[ [**WEBSITE**](https://codemirror.net/) | [**DOCS**](https://codemirror.net/docs/ref/#language) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/language/blob/main/CHANGELOG.md) ]
4
5This package implements the language support infrastructure for the
6[CodeMirror](https://codemirror.net/) code editor.
7
8The [project page](https://codemirror.net/) has more information, a
9number of [examples](https://codemirror.net/examples/) and the
10[documentation](https://codemirror.net/docs/).
11
12This code is released under an
13[MIT license](https://github.com/codemirror/language/tree/main/LICENSE).
14
15We aim to be an inclusive, welcoming community. To make that explicit,
16we have a [code of
17conduct](http://contributor-covenant.org/version/1/1/0/) that applies
18to communication around the project.
19
20## Usage
21
22Setting up a language from a [Lezer](https://lezer.codemirror.net)
23parser looks like this:
24
25```javascript
26import {parser} from "@lezer/json"
27import {LRLanguage, continuedIndent, indentNodeProp,
28 foldNodeProp, foldInside} from "@codemirror/language"
29
30export const jsonLanguage = LRLanguage.define({
31 name: "json",
32 parser: parser.configure({
33 props: [
34 indentNodeProp.add({
35 Object: continuedIndent({except: /^\s*\}/}),
36 Array: continuedIndent({except: /^\s*\]/})
37 }),
38 foldNodeProp.add({
39 "Object Array": foldInside
40 })
41 ]
42 }),
43 languageData: {
44 closeBrackets: {brackets: ["[", "{", '"']},
45 indentOnInput: /^\s*[\}\]]$/
46 }
47})
48```
49
50Often, you'll also use this package just to access some specific
51language-related features, such as accessing the editor's syntax
52tree...
53
54```javascript
55import {syntaxTree} from "@codemirror/language"
56
57const tree = syntaxTree(view)
58```
59
60... or computing the appriate indentation at a given point.
61
62```javascript
63import {getIndentation} from "@codemirror/language"
64
65console.log(getIndentation(view.state, view.state.selection.main.head))
66```