@cssdoc/codemirror 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Danny Wahl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # @cssdoc/codemirror
2
+
3
+ A [CodeMirror 6](https://codemirror.net) extension that highlights cssdoc doc-comment tags inside CSS
4
+ comments — the way TSDoc highlights JSDoc tags. It's the CodeMirror counterpart to
5
+ [`@cssdoc/tmlanguage`](../tmlanguage) (the TextMate/Shiki grammar), sharing the same tag vocabulary.
6
+
7
+ Matches are found by walking the CSS syntax tree and scanning **comment nodes only**, so real CSS
8
+ at-rules like `@property`, `@scope`, and `@media` are never touched.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ npm add @cssdoc/codemirror @codemirror/lang-css
14
+ ```
15
+
16
+ ## Use
17
+
18
+ ```ts
19
+ import { cssdocHighlight } from "@cssdoc/codemirror";
20
+ import { css } from "@codemirror/lang-css";
21
+ import { EditorView, basicSetup } from "codemirror";
22
+
23
+ new EditorView({
24
+ extensions: [basicSetup, css(), cssdocHighlight()],
25
+ parent: document.body,
26
+ });
27
+ ```
28
+
29
+ Colours ship as a base theme that follows the editor's light/dark theme, so it works out of the box.
30
+ Override any token by styling its class: `.cm-cssdoc-tag`, `.cm-cssdoc-modifier`, `.cm-cssdoc-part`,
31
+ `.cm-cssdoc-property`, `.cm-cssdoc-link`, `.cm-cssdoc-punct`.
32
+
33
+ ## What it highlights
34
+
35
+ | Token | Example | Class |
36
+ | --------------------- | -------------------------------------------- | -------------------- |
37
+ | Block and inline tags | `@component`, `@modifier`, `@a11y`, `@link` | `cm-cssdoc-tag` |
38
+ | Modifier name | `-orientation-vertical` after `@modifier` | `cm-cssdoc-modifier` |
39
+ | Part and slot names | `.list` after `@part`, `label` after `@slot` | `cm-cssdoc-part` |
40
+ | Custom properties | `--tabs-gap` | `cm-cssdoc-property` |
41
+ | Inline link text | the target inside `{@link …}` | `cm-cssdoc-link` |
42
+ | Braces | `{` and `}` around an inline tag | `cm-cssdoc-punct` |
@@ -0,0 +1,17 @@
1
+ import { Extension } from "@codemirror/state";
2
+
3
+ //#region src/index.d.ts
4
+ /** Semantic token kind a doc-comment span maps to. Each becomes a `cm-cssdoc-<kind>` class. */
5
+ type CssdocTokenType = "tag" | "modifier" | "part" | "property" | "link" | "punct";
6
+ /** A highlighted span within a comment. `from`/`to` are offsets into the scanned comment text. */
7
+ interface CssdocToken {
8
+ from: number;
9
+ to: number;
10
+ type: CssdocTokenType;
11
+ }
12
+ /** Tokenize one CSS comment's text into cssdoc doc-tag spans (offsets are relative to `text`). */
13
+ declare const tokenizeComment: (text: string) => CssdocToken[];
14
+ /** The cssdoc doc-comment highlighter. Add alongside a CSS language: `[css(), cssdocHighlight()]`. */
15
+ declare const cssdocHighlight: () => Extension;
16
+ //#endregion
17
+ export { CssdocToken, CssdocTokenType, cssdocHighlight, cssdocHighlight as default, tokenizeComment };
package/dist/index.mjs ADDED
@@ -0,0 +1,153 @@
1
+ import { syntaxTree } from "@codemirror/language";
2
+ import { RangeSetBuilder } from "@codemirror/state";
3
+ import { CSSDOC_TAGS, cssdocTagNamesByArgument, cssdocTagNamesByKind } from "@cssdoc/spec";
4
+ import { Decoration, EditorView, ViewPlugin } from "@codemirror/view";
5
+ //#region src/index.ts
6
+ /**
7
+ * `@cssdoc/codemirror` — a CodeMirror 6 extension that highlights cssdoc doc-comment tags inside CSS
8
+ * comments, the way TSDoc highlights JSDoc tags inside `/** … *\/` comments. It's the CodeMirror
9
+ * counterpart to the TextMate grammar in `@cssdoc/tmlanguage`, sharing the same tag vocabulary:
10
+ * `@component`, `@modifier`, `@part`/`@slot`, `@cssproperty`, `{@link …}`, and custom properties.
11
+ *
12
+ * Matches are found by walking the CSS syntax tree and scanning **comment nodes only**, so real CSS
13
+ * at-rules like `@property`, `@scope`, and `@media` are never touched. Colours ship as a base theme
14
+ * that adapts to the editor's light/dark theme, so it works out of the box and can be overridden.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { cssdocHighlight } from "@cssdoc/codemirror";
19
+ * import { css } from "@codemirror/lang-css";
20
+ * new EditorView({ extensions: [css(), cssdocHighlight()], parent });
21
+ * ```
22
+ *
23
+ * @module @cssdoc/codemirror
24
+ */
25
+ const alt = (names) => names.join("|");
26
+ const INLINE_TAGS = cssdocTagNamesByKind("inline");
27
+ const MODIFIER_TAGS = cssdocTagNamesByArgument("modifier-name");
28
+ const PART_TAGS = cssdocTagNamesByArgument("part-name");
29
+ const PROPERTY_TAGS = cssdocTagNamesByArgument("custom-property");
30
+ const PLAIN_TAGS = CSSDOC_TAGS.filter((t) => t.kind !== "inline" && !t.argument).map((t) => t.name);
31
+ const TOKEN = new RegExp([
32
+ `(?<ibrace>\\{)(?<itag>@(?:${alt(INLINE_TAGS)}))\\b[ \\t]*(?<itext>[^}]*)(?<iend>\\})?`,
33
+ `(?<mtag>@(?:${alt(MODIFIER_TAGS)}))\\b[ \\t]*(?<mname>-[A-Za-z][A-Za-z0-9-]*)?`,
34
+ `(?<ptag>@(?:${alt(PART_TAGS)}))\\b[ \\t]*(?<pname>\\.?[A-Za-z][A-Za-z0-9_-]*)?`,
35
+ `(?<rtag>@(?:${alt(PROPERTY_TAGS)}))\\b[ \\t]*(?<rname>--[A-Za-z][A-Za-z0-9-]*)?`,
36
+ `(?<btag>@(?:${alt(PLAIN_TAGS)}))\\b`,
37
+ "(?<cprop>(?<![\\w-])--[A-Za-z][A-Za-z0-9-]*)"
38
+ ].join("|"), "gd");
39
+ const GROUP_TYPE = {
40
+ ibrace: "punct",
41
+ itag: "tag",
42
+ itext: "link",
43
+ iend: "punct",
44
+ mtag: "tag",
45
+ mname: "modifier",
46
+ ptag: "tag",
47
+ pname: "part",
48
+ rtag: "tag",
49
+ rname: "property",
50
+ btag: "tag",
51
+ cprop: "property"
52
+ };
53
+ const STRUCTURE_TAG = /@structure\b/gu;
54
+ const STRUCTURE_TOKEN = /\.[A-Za-z][\w-]*|[{}]/gu;
55
+ /** Tokenize one CSS comment's text into cssdoc doc-tag spans (offsets are relative to `text`). */
56
+ const tokenizeComment = (text) => {
57
+ const out = [];
58
+ for (const m of text.matchAll(TOKEN)) {
59
+ const groups = m.indices?.groups;
60
+ if (!groups) continue;
61
+ for (const key of Object.keys(groups)) {
62
+ const gi = groups[key];
63
+ if (gi && gi[0] !== gi[1]) out.push({
64
+ from: gi[0],
65
+ to: gi[1],
66
+ type: GROUP_TYPE[key]
67
+ });
68
+ }
69
+ }
70
+ for (const m of text.matchAll(STRUCTURE_TAG)) {
71
+ const start = (m.index ?? 0) + m[0].length;
72
+ const after = text.slice(start).search(/@[A-Za-z]/u);
73
+ const end = after === -1 ? text.length : start + after;
74
+ for (const s of text.slice(start, end).matchAll(STRUCTURE_TOKEN)) {
75
+ const from = start + (s.index ?? 0);
76
+ out.push({
77
+ from,
78
+ to: from + s[0].length,
79
+ type: s[0][0] === "." ? "part" : "punct"
80
+ });
81
+ }
82
+ }
83
+ return out;
84
+ };
85
+ const marks = /* @__PURE__ */ new Map();
86
+ const markFor = (type) => {
87
+ let mark = marks.get(type);
88
+ if (!mark) {
89
+ mark = Decoration.mark({ class: `cm-cssdoc-${type}` });
90
+ marks.set(type, mark);
91
+ }
92
+ return mark;
93
+ };
94
+ const build = (view) => {
95
+ const found = [];
96
+ const tree = syntaxTree(view.state);
97
+ for (const range of view.visibleRanges) tree.iterate({
98
+ from: range.from,
99
+ to: range.to,
100
+ enter(node) {
101
+ if (node.name !== "Comment") return;
102
+ const text = view.state.doc.sliceString(node.from, node.to);
103
+ for (const t of tokenizeComment(text)) found.push({
104
+ from: node.from + t.from,
105
+ to: node.from + t.to,
106
+ type: t.type
107
+ });
108
+ }
109
+ });
110
+ found.sort((a, b) => a.from - b.from || a.to - b.to);
111
+ const builder = new RangeSetBuilder();
112
+ for (const t of found) builder.add(t.from, t.to, markFor(t.type));
113
+ return builder.finish();
114
+ };
115
+ const baseTheme = EditorView.baseTheme({
116
+ "&light .cm-cssdoc-tag": {
117
+ color: "#8250df",
118
+ fontWeight: "600"
119
+ },
120
+ "&dark .cm-cssdoc-tag": {
121
+ color: "#d2a8ff",
122
+ fontWeight: "600"
123
+ },
124
+ "&light .cm-cssdoc-modifier": { color: "#0550ae" },
125
+ "&dark .cm-cssdoc-modifier": { color: "#79c0ff" },
126
+ "&light .cm-cssdoc-part": { color: "#116329" },
127
+ "&dark .cm-cssdoc-part": { color: "#7ee787" },
128
+ "&light .cm-cssdoc-property": { color: "#953800" },
129
+ "&dark .cm-cssdoc-property": { color: "#ffa657" },
130
+ "&light .cm-cssdoc-link": {
131
+ color: "#0969da",
132
+ textDecoration: "underline"
133
+ },
134
+ "&dark .cm-cssdoc-link": {
135
+ color: "#a5d6ff",
136
+ textDecoration: "underline"
137
+ },
138
+ "&light .cm-cssdoc-punct": { color: "#6e7781" },
139
+ "&dark .cm-cssdoc-punct": { color: "#8b949e" }
140
+ });
141
+ const plugin = ViewPlugin.fromClass(class {
142
+ decorations;
143
+ constructor(view) {
144
+ this.decorations = build(view);
145
+ }
146
+ update(u) {
147
+ if (u.docChanged || u.viewportChanged) this.decorations = build(u.view);
148
+ }
149
+ }, { decorations: (v) => v.decorations });
150
+ /** The cssdoc doc-comment highlighter. Add alongside a CSS language: `[css(), cssdocHighlight()]`. */
151
+ const cssdocHighlight = () => [plugin, baseTheme];
152
+ //#endregion
153
+ export { cssdocHighlight, cssdocHighlight as default, tokenizeComment };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@cssdoc/codemirror",
3
+ "version": "0.2.0",
4
+ "description": "A CodeMirror 6 extension that highlights cssdoc doc-comment tags inside CSS comments, the way TSDoc highlights JSDoc tags.",
5
+ "keywords": [
6
+ "codemirror",
7
+ "codemirror6",
8
+ "css",
9
+ "cssdoc",
10
+ "editor",
11
+ "syntax-highlighting"
12
+ ],
13
+ "homepage": "https://cssdoc.dev",
14
+ "bugs": "https://github.com/thedannywahl/cssdoc/issues",
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/thedannywahl/cssdoc.git",
19
+ "directory": "syntaxes/cssdoc/codemirror"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "type": "module",
25
+ "exports": {
26
+ ".": "./dist/index.mjs",
27
+ "./package.json": "./package.json"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "dependencies": {
33
+ "@codemirror/language": "^6.12.4",
34
+ "@codemirror/state": "^6.5.2",
35
+ "@codemirror/view": "^6.43.6",
36
+ "@cssdoc/spec": "0.2.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^24.13.3",
40
+ "@vitest/coverage-v8": "4.1.9",
41
+ "publint": "^0.3.21",
42
+ "typescript": "^6.0.3",
43
+ "vite": "npm:@voidzero-dev/vite-plus-core@0.2.4",
44
+ "vite-plus": "0.2.4"
45
+ },
46
+ "scripts": {
47
+ "build": "vp pack",
48
+ "dev": "vp pack --watch",
49
+ "test": "vp test",
50
+ "check": "vp check",
51
+ "publint": "publint"
52
+ }
53
+ }