@minnalstack/editor-plugin-code-highlight 0.0.0-stage → 0.3.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 Aranghat
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 CHANGED
@@ -1,3 +1,9 @@
1
- # Temporary Holding Version
1
+ # @minnalstack/editor-plugin-code-highlight
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Colours the text of code blocks by its language, without changing the document or the page.
4
+
5
+ ```sh
6
+ npm install @minnalstack/editor-plugin-code-highlight
7
+ ```
8
+
9
+ MIT licensed.
@@ -0,0 +1,36 @@
1
+ import type { Block, BlockId, Editor } from '@minnalstack/editor-core';
2
+ import type { LanguageFn } from 'highlight.js';
3
+ /** The kinds of text that are coloured. Each is the CSS highlight `mn-code-<kind>`. */
4
+ export declare const codeKinds: readonly ['keyword', 'string', 'comment', 'number', 'title', 'type', 'attr', 'addition', 'deletion'];
5
+ export type CodeKind = (typeof codeKinds)[number];
6
+ export declare function registerLanguage(name: string, language: LanguageFn): void;
7
+ export declare function isRegistered(name: string): boolean;
8
+ /** A stretch of a block's text and what kind of text it is. */
9
+ export interface CodeRun {
10
+ readonly from: number;
11
+ readonly to: number;
12
+ readonly kind: CodeKind;
13
+ }
14
+ /**
15
+ * The runs of text highlight.js colours in `text`, read from the HTML it writes, with the innermost span deciding. Null when that HTML doesn't add up to the text, so that
16
+ * nothing is painted in the wrong place.
17
+ */
18
+ export declare function runsOf(text: string, language: string): readonly CodeRun[] | null;
19
+ /** The ranges one editor has painted, in the page's highlights, which every editor on the page shares. */
20
+ export declare class Painter {
21
+ private readonly editor;
22
+ private readonly highlights;
23
+ private readonly painted;
24
+ constructor(editor: Editor, view: Window & typeof globalThis);
25
+ /** Whether a block has ranges painted. */
26
+ has(id: BlockId): boolean;
27
+ /** Whether a language is here to paint with. */
28
+ knows(language: string): boolean;
29
+ /** Paints a block's text in a registered language, in place of what it had. */
30
+ paint(block: Block, language: string): void;
31
+ /** Takes a block's ranges off the page. */
32
+ clear(id: BlockId): void;
33
+ /** Takes every range this editor painted off the page. The highlights stay registered, because another editor on the page may be using them. */
34
+ dispose(): void;
35
+ }
36
+ //# sourceMappingURL=highlighting.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"highlighting.d.ts","sourceRoot":"","sources":["../src/highlighting.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAEvE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,uFAAuF;AACvF,eAAO,MAAM,SAAS,YAAI,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM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js/lib/core';
2
+ /** The kinds of text that are coloured. Each is the CSS highlight `mn-code-<kind>`. */
3
+ export const codeKinds = ['keyword', 'string', 'comment', 'number', 'title', 'type', 'attr', 'addition', 'deletion'];
4
+ // highlight.js's own instance, so that a page's use of highlight.js and ours don't change each other's languages or options.
5
+ const engine = hljs.newInstance();
6
+ export function registerLanguage(name, language) {
7
+ if (engine.getLanguage(name) === undefined)
8
+ engine.registerLanguage(name, language);
9
+ }
10
+ export function isRegistered(name) {
11
+ return engine.getLanguage(name) !== undefined;
12
+ }
13
+ // The kind of each highlight.js scope. A scope that isn't here takes the kind of the one around it, and `null` is plain text, whatever is around it.
14
+ const kindOfScope = new Map([
15
+ ['keyword', 'keyword'], ['doctag', 'keyword'], ['template-tag', 'keyword'], ['selector-tag', 'keyword'], ['name', 'keyword'], ['meta', 'keyword'], ['bullet', 'keyword'],
16
+ ['string', 'string'], ['regexp', 'string'], ['char', 'string'], ['symbol', 'string'], ['link', 'string'], ['code', 'string'],
17
+ ['comment', 'comment'], ['quote', 'comment'],
18
+ ['number', 'number'], ['literal', 'number'],
19
+ ['title', 'title'], ['section', 'title'],
20
+ ['type', 'type'], ['built_in', 'type'],
21
+ ['attr', 'attr'], ['attribute', 'attr'], ['property', 'attr'], ['variable', 'attr'], ['template-variable', 'attr'],
22
+ ['selector-attr', 'attr'], ['selector-class', 'attr'], ['selector-id', 'attr'], ['selector-pseudo', 'attr'],
23
+ ['addition', 'addition'], ['deletion', 'deletion'],
24
+ // What is put into a string, as in `${name}`, is code again.
25
+ ['subst', null],
26
+ ]);
27
+ /** The kind for a span's class, such as `hljs-keyword` or `hljs-title function_`; `undefined` takes the kind around it. */
28
+ function kindOfClass(className) {
29
+ const [scope = '', ...parts] = className.split(' ');
30
+ // Another language inside this one, such as `language-css` in HTML, is only a container.
31
+ if (!scope.startsWith('hljs-'))
32
+ return undefined;
33
+ const name = scope.slice('hljs-'.length);
34
+ if (name === 'title' && parts.includes('class_'))
35
+ return 'type';
36
+ if (name === 'variable' && parts.includes('language_'))
37
+ return 'keyword';
38
+ if (name === 'variable' && parts.includes('constant_'))
39
+ return 'number';
40
+ return kindOfScope.get(name);
41
+ }
42
+ // highlight.js's HTML has only these: a span with a class, the end of one, the five characters it escapes, and text.
43
+ const htmlPiece = /<span class="([^"]*)">|<\/span>|&(?:amp|lt|gt|quot|#x27);|[^<&]+/g;
44
+ /**
45
+ * The runs of text highlight.js colours in `text`, read from the HTML it writes, with the innermost span deciding. Null when that HTML doesn't add up to the text, so that
46
+ * nothing is painted in the wrong place.
47
+ */
48
+ export function runsOf(text, language) {
49
+ let html;
50
+ try {
51
+ html = engine.highlight(text, { language, ignoreIllegals: true }).value;
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ const runs = [];
57
+ const open = [];
58
+ let offset = 0;
59
+ for (const [piece, className] of html.matchAll(htmlPiece)) {
60
+ if (className !== undefined) {
61
+ const kind = kindOfClass(className);
62
+ open.push(kind === undefined ? (open.at(-1) ?? null) : kind);
63
+ continue;
64
+ }
65
+ if (piece === '</span>') {
66
+ open.pop();
67
+ continue;
68
+ }
69
+ const length = piece.startsWith('&') ? 1 : piece.length;
70
+ const kind = open.at(-1) ?? null;
71
+ const last = runs.at(-1);
72
+ if (kind !== null && last?.kind === kind && last.to === offset)
73
+ runs[runs.length - 1] = { from: last.from, to: offset + length, kind };
74
+ else if (kind !== null)
75
+ runs.push({ from: offset, to: offset + length, kind });
76
+ offset += length;
77
+ }
78
+ return offset === text.length ? runs : null;
79
+ }
80
+ /**
81
+ * The text nodes that show a block's text, found from one range for all of it. `getDomRange` looks for the block from the start of the document each time, so asking it for
82
+ * each run made a 20,000-character block in a document of 1,000 paragraphs take 47 ms to paint, and this takes as long whatever the document's size. Null when the page's text
83
+ * there isn't exactly the block's, as when invisible characters are shown, and then each run asks `getDomRange` for itself.
84
+ */
85
+ function textOnPage(editor, block) {
86
+ const whole = editor.getDomRange({ block: block.id, from: 0, to: block.text.length });
87
+ if (whole === null)
88
+ return null;
89
+ const root = whole.commonAncestorContainer;
90
+ const nodes = [];
91
+ if (root.nodeType === Node.TEXT_NODE)
92
+ nodes.push(root);
93
+ else {
94
+ const walker = root.ownerDocument.createTreeWalker(root, NodeFilter.SHOW_TEXT);
95
+ for (let node = walker.nextNode(); node !== null; node = walker.nextNode())
96
+ if (whole.intersectsNode(node))
97
+ nodes.push(node);
98
+ }
99
+ const pieces = [];
100
+ let shown = '';
101
+ for (const node of nodes) {
102
+ const start = node === whole.startContainer ? whole.startOffset : 0;
103
+ const end = node === whole.endContainer ? whole.endOffset : node.length;
104
+ if (end <= start)
105
+ continue;
106
+ pieces.push({ node, start, from: shown.length, length: end - start });
107
+ shown += node.data.slice(start, end);
108
+ }
109
+ return shown === block.text ? pieces : null;
110
+ }
111
+ /** The ranges one editor has painted, in the page's highlights, which every editor on the page shares. */
112
+ export class Painter {
113
+ editor;
114
+ highlights = new Map();
115
+ painted = new Map();
116
+ constructor(editor, view) {
117
+ this.editor = editor;
118
+ for (const kind of codeKinds) {
119
+ const name = `mn-code-${kind}`;
120
+ let highlight = view.CSS.highlights.get(name);
121
+ if (highlight === undefined) {
122
+ highlight = new view.Highlight();
123
+ view.CSS.highlights.set(name, highlight);
124
+ }
125
+ this.highlights.set(kind, highlight);
126
+ }
127
+ }
128
+ /** Whether a block has ranges painted. */
129
+ has(id) {
130
+ return this.painted.has(id);
131
+ }
132
+ /** Whether a language is here to paint with. */
133
+ knows(language) {
134
+ return isRegistered(language);
135
+ }
136
+ /** Paints a block's text in a registered language, in place of what it had. */
137
+ paint(block, language) {
138
+ const runs = runsOf(block.text, language) ?? [];
139
+ const pieces = runs.length === 0 ? null : textOnPage(this.editor, block);
140
+ const ranges = [];
141
+ let piece = 0;
142
+ for (const run of runs) {
143
+ let range;
144
+ if (pieces === null)
145
+ range = this.editor.getDomRange({ block: block.id, from: run.from, to: run.to });
146
+ else {
147
+ // The runs come in order, so the piece holding each one's start is this one or a later one.
148
+ while (pieces[piece].from + pieces[piece].length <= run.from)
149
+ piece++;
150
+ let last = piece;
151
+ while (pieces[last].from + pieces[last].length < run.to)
152
+ last++;
153
+ range = pieces[piece].node.ownerDocument.createRange();
154
+ range.setStart(pieces[piece].node, pieces[piece].start + run.from - pieces[piece].from);
155
+ range.setEnd(pieces[last].node, pieces[last].start + run.to - pieces[last].from);
156
+ }
157
+ if (range !== null)
158
+ ranges.push([this.highlights.get(run.kind), range]);
159
+ }
160
+ this.clear(block.id);
161
+ for (const [highlight, range] of ranges)
162
+ highlight.add(range);
163
+ if (ranges.length > 0)
164
+ this.painted.set(block.id, ranges);
165
+ }
166
+ /** Takes a block's ranges off the page. */
167
+ clear(id) {
168
+ for (const [highlight, range] of this.painted.get(id) ?? [])
169
+ highlight.delete(range);
170
+ this.painted.delete(id);
171
+ }
172
+ /** Takes every range this editor painted off the page. The highlights stay registered, because another editor on the page may be using them. */
173
+ dispose() {
174
+ for (const id of [...this.painted.keys()])
175
+ this.clear(id);
176
+ }
177
+ }
178
+ //# sourceMappingURL=highlighting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"highlighting.js","sourceRoot":"","sources":["../src/highlighting.ts"],"names":[],"mappings":"AAEA,OAAO,IAAI,MAAM,uBAAuB,CAAC;AAGzC,uFAAuF;AACvF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,CAAU,CAAC;AAG9H,6HAA6H;AAC7H,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;AAElC,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,QAAoB;IACjE,IAAI,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,SAAS;QAAE,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AACtF,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,O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AAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;YAAE,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAC5D,CAAC;CACF"}
@@ -0,0 +1,34 @@
1
+ import { type EditorPage, EditorPlugin, type PluginContext } from '@minnalstack/editor-core';
2
+ /**
3
+ * Colours the text of code blocks by their `language`, with highlight.js. It paints with the CSS Custom Highlight API, so the document and the page's elements stay exactly as
4
+ * they were: nothing is saved, and the caret, selection and typing are untouched.
5
+ *
6
+ * - **What is coloured** goes into a few highlights the page's stylesheet colours: `mn-code-keyword`, `mn-code-string`, `mn-code-comment`, `mn-code-number`, `mn-code-title`
7
+ * (function names and headings), `mn-code-type` (types, classes and built-ins), `mn-code-attr` (attributes, properties and variables), `mn-code-addition` and `mn-code-deletion`
8
+ * (a diff's lines). Restyle one with `::highlight(mn-code-keyword) { color: … }`.
9
+ * - **The languages** are bash, C, C++, C#, CSS, diff, Dockerfile, Go, GraphQL, INI and TOML, Java, JavaScript, JSON, Kotlin, Less, Lua, Makefile, Markdown, PHP, PowerShell,
10
+ * Python, R, Ruby, Rust, SCSS, shell sessions, SQL, Swift, TypeScript, XML and HTML, and YAML, by their names or usual short names (`ts`, `js`, `py`, `cs`, `sh`, `yml`, …),
11
+ * in any case. A block with no language, or one not listed, stays plain: the language is never guessed.
12
+ * - **Nothing loads until it is needed.** highlight.js and each language are fetched the first time a code block in that language is on the page, so a page with no code block
13
+ * fetches nothing. A failed fetch leaves the code plain and is tried again on a later change.
14
+ * - **It works after the frame.** A block is coloured once the editor is on a page, then again after each change to it, and only the blocks that changed are coloured again.
15
+ * - **A code block of more than 20,000 characters stays plain**, so that typing in it stays quick.
16
+ * - **In a browser without the CSS Custom Highlight API**, and in an editor with no page, it does nothing.
17
+ */
18
+ export declare class CodeHighlightPlugin extends EditorPlugin {
19
+ readonly name = "code-highlight";
20
+ readonly requires: {
21
+ blockTypes: string[];
22
+ };
23
+ private editor;
24
+ private view;
25
+ private painter;
26
+ private changes;
27
+ private isDisposed;
28
+ configure(context: PluginContext): void;
29
+ attach(page: EditorPage): void;
30
+ dispose(): void;
31
+ /** Colours a block as it is now, or takes its colours off. When its language hasn't loaded, it is plain until the language is here. */
32
+ private refresh;
33
+ }
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0D,KAAK,UAAU,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAuCrJ;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,mBAAoB,SAAQ,YAAY;IACnD,SAAkB,IAAI,oBAAoB;IAC1C,SAAkB,QAAQ;QAAK,UAAU;MAAa;IACtD,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,IAAI,CAA6C;IACzD,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,OAAO,CAA2B;IAC1C,OAAO,CAAC,UAAU,CAAS;IAElB,SAAS,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAE/C;IAEQ,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAStC;IAEQ,OAAO,IAAI,IAAI,CAIvB;IAED,uIAAuI;IACvI,OAAO,CAAC,OAAO;CAwBhB"}
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import { EditorPlugin } from '@minnalstack/editor-core';
2
+ import { codeLanguageOf } from './languages.js';
3
+ /** The longest code block that is coloured, in characters. */
4
+ const longest = 20_000;
5
+ // Each loads once for the page, the first time a code block wants it, and every editor on the page shares it. A failed load is forgotten, so that a later change tries again.
6
+ let highlighting = null;
7
+ const languages = new Map();
8
+ function loadHighlighting() {
9
+ return (highlighting ??= import('./highlighting.js').catch((error) => {
10
+ highlighting = null;
11
+ throw error;
12
+ }));
13
+ }
14
+ function loadLanguage(language) {
15
+ let loading = languages.get(language.name);
16
+ if (loading === undefined) {
17
+ loading = Promise.all([loadHighlighting(), language.load()]).then(([module, definition]) => {
18
+ module.registerLanguage(language.name, definition.default);
19
+ return module;
20
+ });
21
+ loading.catch(() => languages.delete(language.name));
22
+ languages.set(language.name, loading);
23
+ }
24
+ return loading;
25
+ }
26
+ /** The language a block is to be coloured in, or `undefined` if it is to stay plain. */
27
+ function languageToPaint(block) {
28
+ if (block?.type !== 'code' || block.text === '' || block.text.length > longest)
29
+ return undefined;
30
+ return codeLanguageOf(block.attrs['language']);
31
+ }
32
+ /**
33
+ * Colours the text of code blocks by their `language`, with highlight.js. It paints with the CSS Custom Highlight API, so the document and the page's elements stay exactly as
34
+ * they were: nothing is saved, and the caret, selection and typing are untouched.
35
+ *
36
+ * - **What is coloured** goes into a few highlights the page's stylesheet colours: `mn-code-keyword`, `mn-code-string`, `mn-code-comment`, `mn-code-number`, `mn-code-title`
37
+ * (function names and headings), `mn-code-type` (types, classes and built-ins), `mn-code-attr` (attributes, properties and variables), `mn-code-addition` and `mn-code-deletion`
38
+ * (a diff's lines). Restyle one with `::highlight(mn-code-keyword) { color: … }`.
39
+ * - **The languages** are bash, C, C++, C#, CSS, diff, Dockerfile, Go, GraphQL, INI and TOML, Java, JavaScript, JSON, Kotlin, Less, Lua, Makefile, Markdown, PHP, PowerShell,
40
+ * Python, R, Ruby, Rust, SCSS, shell sessions, SQL, Swift, TypeScript, XML and HTML, and YAML, by their names or usual short names (`ts`, `js`, `py`, `cs`, `sh`, `yml`, …),
41
+ * in any case. A block with no language, or one not listed, stays plain: the language is never guessed.
42
+ * - **Nothing loads until it is needed.** highlight.js and each language are fetched the first time a code block in that language is on the page, so a page with no code block
43
+ * fetches nothing. A failed fetch leaves the code plain and is tried again on a later change.
44
+ * - **It works after the frame.** A block is coloured once the editor is on a page, then again after each change to it, and only the blocks that changed are coloured again.
45
+ * - **A code block of more than 20,000 characters stays plain**, so that typing in it stays quick.
46
+ * - **In a browser without the CSS Custom Highlight API**, and in an editor with no page, it does nothing.
47
+ */
48
+ export class CodeHighlightPlugin extends EditorPlugin {
49
+ name = 'code-highlight';
50
+ requires = { blockTypes: ['code'] };
51
+ editor = null;
52
+ view = null;
53
+ painter = null;
54
+ changes = null;
55
+ isDisposed = false;
56
+ configure(context) {
57
+ this.editor = context.editor;
58
+ }
59
+ attach(page) {
60
+ const view = page.root.ownerDocument.defaultView;
61
+ if (view === null || typeof view.Highlight !== 'function' || view.CSS?.highlights === undefined)
62
+ return;
63
+ this.view = view;
64
+ // Not only for code blocks: one that stops being code while keeping its id, as when a document is loaded over this one, is listed as what it became, and its colours have to come off.
65
+ this.changes = this.editor.documentChanged.subscribe((_sender, e) => {
66
+ for (const changed of e.changedBlocks)
67
+ if (changed.type === 'code' || this.painter?.has(changed.id) === true)
68
+ this.refresh(changed.id);
69
+ });
70
+ for (const block of this.editor.document.blocks('code'))
71
+ this.refresh(block.id);
72
+ }
73
+ dispose() {
74
+ this.isDisposed = true;
75
+ this.changes?.dispose();
76
+ this.painter?.dispose();
77
+ }
78
+ /** Colours a block as it is now, or takes its colours off. When its language hasn't loaded, it is plain until the language is here. */
79
+ refresh(id) {
80
+ if (this.isDisposed)
81
+ return;
82
+ const block = this.editor.document.find(id);
83
+ const language = languageToPaint(block);
84
+ if (language === undefined) {
85
+ this.painter?.clear(id);
86
+ return;
87
+ }
88
+ if (this.painter !== null && this.painter.knows(language.name)) {
89
+ this.painter.paint(block, language.name);
90
+ return;
91
+ }
92
+ // What it shows is of another language, or of text that has changed since.
93
+ this.painter?.clear(id);
94
+ loadLanguage(language).then((module) => {
95
+ if (this.isDisposed || !module.isRegistered(language.name))
96
+ return;
97
+ this.painter ??= new module.Painter(this.editor, this.view);
98
+ // As the block is by now, which may not be as it was when the load began.
99
+ this.refresh(id);
100
+ }, (error) => console.warn(`the plugin "${this.name}" could not load the ${language.name} language:`, error));
101
+ }
102
+ }
103
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],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AE,OAAO;YACnE,IAAI,CAAC,OAAO,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAO,EAAE,IAAI,CAAC,IAAK,CAAC,CAAC;YAC9D,0EAA0E;YAC1E,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACnB,CAAC,EACD,CAAC,KAAc,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,IAAI,CAAC,IAAI,wBAAwB,QAAQ,CAAC,IAAI,YAAY,EAAE,KAAK,CAAC,CACnH,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,11 @@
1
+ import type { LanguageFn } from 'highlight.js';
2
+ /** A highlight.js language: the name it is registered by, and how to fetch it. */
3
+ export interface CodeLanguage {
4
+ readonly name: string;
5
+ readonly load: () => Promise<{
6
+ readonly default: LanguageFn;
7
+ }>;
8
+ }
9
+ /** The language a code block's `language` attribute names, in any case, or `undefined` for none, or one this plugin doesn't know, which stays plain. */
10
+ export declare function codeLanguageOf(stored: unknown): CodeLanguage | undefined;
11
+ //# sourceMappingURL=languages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"languages.d.ts","sourceRoot":"","sources":["../src/languages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,kFAAkF;AAClF,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC;QAAE,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAA;KAAE,CAAC,CAAC;CAChE;AA+ED,wJAAwJ;AACxJ,wBAAgB,cAAc,CAAC,MAAM,EAAE,OAAO,GAAG,YAAY,GAAG,SAAS,CAKxE"}
@@ -0,0 +1,84 @@
1
+ // One import each, written out, so that a bundler makes each language its own file and a page fetches only the languages its code blocks use.
2
+ const loaders = {
3
+ bash: () => import('highlight.js/lib/languages/bash'),
4
+ c: () => import('highlight.js/lib/languages/c'),
5
+ cpp: () => import('highlight.js/lib/languages/cpp'),
6
+ csharp: () => import('highlight.js/lib/languages/csharp'),
7
+ css: () => import('highlight.js/lib/languages/css'),
8
+ diff: () => import('highlight.js/lib/languages/diff'),
9
+ dockerfile: () => import('highlight.js/lib/languages/dockerfile'),
10
+ go: () => import('highlight.js/lib/languages/go'),
11
+ graphql: () => import('highlight.js/lib/languages/graphql'),
12
+ ini: () => import('highlight.js/lib/languages/ini'),
13
+ java: () => import('highlight.js/lib/languages/java'),
14
+ javascript: () => import('highlight.js/lib/languages/javascript'),
15
+ json: () => import('highlight.js/lib/languages/json'),
16
+ kotlin: () => import('highlight.js/lib/languages/kotlin'),
17
+ less: () => import('highlight.js/lib/languages/less'),
18
+ lua: () => import('highlight.js/lib/languages/lua'),
19
+ makefile: () => import('highlight.js/lib/languages/makefile'),
20
+ markdown: () => import('highlight.js/lib/languages/markdown'),
21
+ php: () => import('highlight.js/lib/languages/php'),
22
+ powershell: () => import('highlight.js/lib/languages/powershell'),
23
+ python: () => import('highlight.js/lib/languages/python'),
24
+ r: () => import('highlight.js/lib/languages/r'),
25
+ ruby: () => import('highlight.js/lib/languages/ruby'),
26
+ rust: () => import('highlight.js/lib/languages/rust'),
27
+ scss: () => import('highlight.js/lib/languages/scss'),
28
+ shell: () => import('highlight.js/lib/languages/shell'),
29
+ sql: () => import('highlight.js/lib/languages/sql'),
30
+ swift: () => import('highlight.js/lib/languages/swift'),
31
+ typescript: () => import('highlight.js/lib/languages/typescript'),
32
+ xml: () => import('highlight.js/lib/languages/xml'),
33
+ yaml: () => import('highlight.js/lib/languages/yaml'),
34
+ };
35
+ // The other names a code block's language is written as, from a Markdown fence or a `language-` class, for the language they mean.
36
+ const aliases = {
37
+ sh: 'bash',
38
+ zsh: 'bash',
39
+ h: 'c',
40
+ cc: 'cpp',
41
+ 'c++': 'cpp',
42
+ cxx: 'cpp',
43
+ hpp: 'cpp',
44
+ cs: 'csharp',
45
+ 'c#': 'csharp',
46
+ patch: 'diff',
47
+ docker: 'dockerfile',
48
+ golang: 'go',
49
+ gql: 'graphql',
50
+ toml: 'ini',
51
+ js: 'javascript',
52
+ jsx: 'javascript',
53
+ mjs: 'javascript',
54
+ cjs: 'javascript',
55
+ jsonc: 'json',
56
+ kt: 'kotlin',
57
+ kts: 'kotlin',
58
+ make: 'makefile',
59
+ mk: 'makefile',
60
+ md: 'markdown',
61
+ ps1: 'powershell',
62
+ pwsh: 'powershell',
63
+ py: 'python',
64
+ rb: 'ruby',
65
+ rs: 'rust',
66
+ console: 'shell',
67
+ ts: 'typescript',
68
+ tsx: 'typescript',
69
+ mts: 'typescript',
70
+ cts: 'typescript',
71
+ html: 'xml',
72
+ xhtml: 'xml',
73
+ svg: 'xml',
74
+ yml: 'yaml',
75
+ };
76
+ /** The language a code block's `language` attribute names, in any case, or `undefined` for none, or one this plugin doesn't know, which stays plain. */
77
+ export function codeLanguageOf(stored) {
78
+ if (typeof stored !== 'string')
79
+ return undefined;
80
+ const written = stored.toLowerCase();
81
+ const name = Object.hasOwn(aliases, written) ? aliases[written] : written;
82
+ return Object.hasOwn(loaders, name) ? { name, load: loaders[name] } : undefined;
83
+ }
84
+ //# sourceMappingURL=languages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"languages.js","sourceRoot":"","sources":["../src/languages.ts"],"names":[],"mappings":"AAQA,8IAA8I;AAC9I,MAAM,OAAO,GAAmD;IAC9D,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,iCAAiC,CAAC;IACrD,CAAC,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,8BAA8B,CAAC;IAC/C,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,gCAAgC,CAAC;IACnD,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,mCAAmC,CAAC;IACzD,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,gCAAgC,CAAC;IACnD,IAAI,EAAE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AE,YAAY;IACjB,IAAI,EAAE,KAAK;IACX,KAAK,EAAE,KAAK;IACZ,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,MAAM;CACZ,CAAC;AAEF,wJAAwJ;AACxJ,MAAM,UAAU,cAAc,CAAC,MAAe;IAC5C,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IACjD,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;IACrC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAE,CAAC,CAAC,CAAC,OAAO,CAAC;IAC3E,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,CAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;AACnF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@minnalstack/editor-plugin-code-highlight",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "license": "MIT",
5
+ "description": "Colours the text of code blocks by its language, without changing the document or the page.",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "types": "./dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "sideEffects": false,
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.json"
20
+ },
21
+ "dependencies": {
22
+ "highlight.js": "11.12.0"
23
+ },
24
+ "peerDependencies": {
25
+ "@minnalstack/editor-core": "^0.3.0"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "registry": "https://registry.npmjs.org/"
30
+ }
31
+ }