@scalar/highlight 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/CHANGELOG.md +13 -0
- package/README.md +324 -0
- package/dist/all.d.ts +28 -0
- package/dist/all.d.ts.map +1 -0
- package/dist/all.js +105 -0
- package/dist/compat/hljs.d.ts +24 -0
- package/dist/compat/hljs.d.ts.map +1 -0
- package/dist/compat/hljs.js +95 -0
- package/dist/compat/index.d.ts +24 -0
- package/dist/compat/index.d.ts.map +1 -0
- package/dist/compat/index.js +21 -0
- package/dist/compat/languages.d.ts +46 -0
- package/dist/compat/languages.d.ts.map +1 -0
- package/dist/compat/languages.js +236 -0
- package/dist/compat/syntax-highlight.d.ts +37 -0
- package/dist/compat/syntax-highlight.d.ts.map +1 -0
- package/dist/compat/syntax-highlight.js +133 -0
- package/dist/core/compile.d.ts +3 -0
- package/dist/core/compile.d.ts.map +1 -0
- package/dist/core/compile.js +142 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +5 -0
- package/dist/core/registry.d.ts +14 -0
- package/dist/core/registry.d.ts.map +1 -0
- package/dist/core/registry.js +77 -0
- package/dist/core/render.d.ts +67 -0
- package/dist/core/render.d.ts.map +1 -0
- package/dist/core/render.js +216 -0
- package/dist/core/scopes.d.ts +94 -0
- package/dist/core/scopes.d.ts.map +1 -0
- package/dist/core/scopes.js +113 -0
- package/dist/core/tokenize.d.ts +18 -0
- package/dist/core/tokenize.d.ts.map +1 -0
- package/dist/core/tokenize.js +131 -0
- package/dist/core/types.d.ts +99 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +10 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/langs/bash.d.ts +4 -0
- package/dist/langs/bash.d.ts.map +1 -0
- package/dist/langs/bash.js +123 -0
- package/dist/langs/c.d.ts +4 -0
- package/dist/langs/c.d.ts.map +1 -0
- package/dist/langs/c.js +196 -0
- package/dist/langs/clojure.d.ts +4 -0
- package/dist/langs/clojure.d.ts.map +1 -0
- package/dist/langs/clojure.js +213 -0
- package/dist/langs/cpp.d.ts +4 -0
- package/dist/langs/cpp.d.ts.map +1 -0
- package/dist/langs/cpp.js +391 -0
- package/dist/langs/csharp.d.ts +4 -0
- package/dist/langs/csharp.d.ts.map +1 -0
- package/dist/langs/csharp.js +307 -0
- package/dist/langs/css.d.ts +4 -0
- package/dist/langs/css.d.ts.map +1 -0
- package/dist/langs/css.js +94 -0
- package/dist/langs/dart.d.ts +4 -0
- package/dist/langs/dart.d.ts.map +1 -0
- package/dist/langs/dart.js +262 -0
- package/dist/langs/diff.d.ts +10 -0
- package/dist/langs/diff.d.ts.map +1 -0
- package/dist/langs/diff.js +26 -0
- package/dist/langs/dockerfile.d.ts +4 -0
- package/dist/langs/dockerfile.d.ts.map +1 -0
- package/dist/langs/dockerfile.js +308 -0
- package/dist/langs/elixir.d.ts +4 -0
- package/dist/langs/elixir.d.ts.map +1 -0
- package/dist/langs/elixir.js +343 -0
- package/dist/langs/fsharp.d.ts +4 -0
- package/dist/langs/fsharp.d.ts.map +1 -0
- package/dist/langs/fsharp.js +330 -0
- package/dist/langs/go.d.ts +5 -0
- package/dist/langs/go.d.ts.map +1 -0
- package/dist/langs/go.js +90 -0
- package/dist/langs/graphql.d.ts +4 -0
- package/dist/langs/graphql.d.ts.map +1 -0
- package/dist/langs/graphql.js +185 -0
- package/dist/langs/haskell.d.ts +4 -0
- package/dist/langs/haskell.d.ts.map +1 -0
- package/dist/langs/haskell.js +375 -0
- package/dist/langs/html.d.ts +14 -0
- package/dist/langs/html.d.ts.map +1 -0
- package/dist/langs/html.js +71 -0
- package/dist/langs/http.d.ts +4 -0
- package/dist/langs/http.d.ts.map +1 -0
- package/dist/langs/http.js +185 -0
- package/dist/langs/ini.d.ts +4 -0
- package/dist/langs/ini.d.ts.map +1 -0
- package/dist/langs/ini.js +227 -0
- package/dist/langs/java.d.ts +4 -0
- package/dist/langs/java.d.ts.map +1 -0
- package/dist/langs/java.js +178 -0
- package/dist/langs/javascript.d.ts +4 -0
- package/dist/langs/javascript.d.ts.map +1 -0
- package/dist/langs/javascript.js +226 -0
- package/dist/langs/json.d.ts +10 -0
- package/dist/langs/json.d.ts.map +1 -0
- package/dist/langs/json.js +46 -0
- package/dist/langs/kotlin.d.ts +4 -0
- package/dist/langs/kotlin.d.ts.map +1 -0
- package/dist/langs/kotlin.js +220 -0
- package/dist/langs/lua.d.ts +4 -0
- package/dist/langs/lua.d.ts.map +1 -0
- package/dist/langs/lua.js +330 -0
- package/dist/langs/makefile.d.ts +4 -0
- package/dist/langs/makefile.d.ts.map +1 -0
- package/dist/langs/makefile.js +266 -0
- package/dist/langs/markdown.d.ts +11 -0
- package/dist/langs/markdown.d.ts.map +1 -0
- package/dist/langs/markdown.js +71 -0
- package/dist/langs/matlab.d.ts +4 -0
- package/dist/langs/matlab.d.ts.map +1 -0
- package/dist/langs/matlab.js +352 -0
- package/dist/langs/mojo.d.ts +4 -0
- package/dist/langs/mojo.d.ts.map +1 -0
- package/dist/langs/mojo.js +337 -0
- package/dist/langs/nginx.d.ts +4 -0
- package/dist/langs/nginx.d.ts.map +1 -0
- package/dist/langs/nginx.js +168 -0
- package/dist/langs/objectivec.d.ts +4 -0
- package/dist/langs/objectivec.d.ts.map +1 -0
- package/dist/langs/objectivec.js +287 -0
- package/dist/langs/ocaml.d.ts +4 -0
- package/dist/langs/ocaml.d.ts.map +1 -0
- package/dist/langs/ocaml.js +309 -0
- package/dist/langs/perl.d.ts +4 -0
- package/dist/langs/perl.d.ts.map +1 -0
- package/dist/langs/perl.js +435 -0
- package/dist/langs/php.d.ts +4 -0
- package/dist/langs/php.d.ts.map +1 -0
- package/dist/langs/php.js +325 -0
- package/dist/langs/powershell.d.ts +4 -0
- package/dist/langs/powershell.d.ts.map +1 -0
- package/dist/langs/powershell.js +308 -0
- package/dist/langs/python.d.ts +4 -0
- package/dist/langs/python.d.ts.map +1 -0
- package/dist/langs/python.js +350 -0
- package/dist/langs/r.d.ts +4 -0
- package/dist/langs/r.d.ts.map +1 -0
- package/dist/langs/r.js +265 -0
- package/dist/langs/ruby.d.ts +4 -0
- package/dist/langs/ruby.d.ts.map +1 -0
- package/dist/langs/ruby.js +365 -0
- package/dist/langs/rust.d.ts +10 -0
- package/dist/langs/rust.d.ts.map +1 -0
- package/dist/langs/rust.js +116 -0
- package/dist/langs/scala.d.ts +4 -0
- package/dist/langs/scala.d.ts.map +1 -0
- package/dist/langs/scala.js +308 -0
- package/dist/langs/sql.d.ts +5 -0
- package/dist/langs/sql.d.ts.map +1 -0
- package/dist/langs/sql.js +57 -0
- package/dist/langs/swift.d.ts +33 -0
- package/dist/langs/swift.d.ts.map +1 -0
- package/dist/langs/swift.js +256 -0
- package/dist/langs/yaml.d.ts +11 -0
- package/dist/langs/yaml.d.ts.map +1 -0
- package/dist/langs/yaml.js +123 -0
- package/dist/lazy.d.ts +69 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +200 -0
- package/dist/style.css +308 -0
- package/package.json +86 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { compile } from './compile.js';
|
|
2
|
+
/**
|
|
3
|
+
* Language registry.
|
|
4
|
+
*
|
|
5
|
+
* Registering is cheap — grammars are compiled on first use, not on import —
|
|
6
|
+
* so a bundle that registers ten languages and highlights one only pays for
|
|
7
|
+
* the one.
|
|
8
|
+
*/
|
|
9
|
+
const sources = new Map();
|
|
10
|
+
const cache = new Map();
|
|
11
|
+
export const registerLanguage = (...grammars) => {
|
|
12
|
+
for (const grammar of grammars) {
|
|
13
|
+
const name = grammar.name.toLowerCase();
|
|
14
|
+
// Re-registering replaces a language outright, so retire every name the
|
|
15
|
+
// previous grammar answered to before claiming the new one's. Evicting only
|
|
16
|
+
// the canonical name would leave each alias pointing at, and cached as, the
|
|
17
|
+
// grammar this call is replacing.
|
|
18
|
+
const previous = sources.get(name);
|
|
19
|
+
if (previous) {
|
|
20
|
+
for (const key of [previous.name, ...(previous.aliases ?? [])]) {
|
|
21
|
+
const lower = key.toLowerCase();
|
|
22
|
+
if (sources.get(lower) === previous)
|
|
23
|
+
sources.delete(lower);
|
|
24
|
+
cache.delete(lower);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
for (const key of [name, ...(grammar.aliases ?? [])]) {
|
|
28
|
+
const lower = key.toLowerCase();
|
|
29
|
+
sources.set(lower, grammar);
|
|
30
|
+
cache.delete(lower);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
export const isRegistered = (name) => {
|
|
35
|
+
return sources.has(name.toLowerCase());
|
|
36
|
+
};
|
|
37
|
+
/** Canonical names of registered languages, without aliases. */
|
|
38
|
+
export const listLanguages = () => {
|
|
39
|
+
return [...new Set([...sources.values()].map((g) => g.name))].sort();
|
|
40
|
+
};
|
|
41
|
+
const isCompiled = (value) => {
|
|
42
|
+
return 'root' in value;
|
|
43
|
+
};
|
|
44
|
+
/** Looks up and compiles a language, memoizing the compiled form. */
|
|
45
|
+
export const getLanguage = (name) => {
|
|
46
|
+
const key = name.toLowerCase();
|
|
47
|
+
const hit = cache.get(key);
|
|
48
|
+
if (hit)
|
|
49
|
+
return hit;
|
|
50
|
+
const grammar = sources.get(key);
|
|
51
|
+
if (!grammar)
|
|
52
|
+
return undefined;
|
|
53
|
+
const built = compile(grammar);
|
|
54
|
+
// Cache under every alias so repeat lookups skip the alias hop.
|
|
55
|
+
for (const alias of [grammar.name, ...(grammar.aliases ?? [])]) {
|
|
56
|
+
cache.set(alias.toLowerCase(), built);
|
|
57
|
+
}
|
|
58
|
+
return built;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Accepts a registered language name, a grammar, or an already-compiled
|
|
62
|
+
* grammar. Throws on an unknown name — silently rendering unhighlighted code
|
|
63
|
+
* hides typos in a `lang` prop for a long time.
|
|
64
|
+
*/
|
|
65
|
+
export const resolveGrammar = (lang) => {
|
|
66
|
+
if (typeof lang !== 'string') {
|
|
67
|
+
return isCompiled(lang) ? lang : compile(lang);
|
|
68
|
+
}
|
|
69
|
+
const found = getLanguage(lang);
|
|
70
|
+
if (found)
|
|
71
|
+
return found;
|
|
72
|
+
const known = listLanguages();
|
|
73
|
+
throw new Error(`Language "${lang}" is not registered. ` +
|
|
74
|
+
(known.length
|
|
75
|
+
? `Registered: ${known.join(', ')}.`
|
|
76
|
+
: `Import it and call registerLanguage(), or use loadLanguage() from "@scalar/highlight/lazy".`));
|
|
77
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { CompiledGrammar } from './types.js';
|
|
2
|
+
export type RenderOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Prefix for generated class names. Must match the stylesheet — change it
|
|
5
|
+
* only alongside a stylesheet of your own.
|
|
6
|
+
*/
|
|
7
|
+
classPrefix?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Wrap each line in its own element, which is what makes line numbers,
|
|
10
|
+
* line highlighting and per-line diff gutters possible.
|
|
11
|
+
*/
|
|
12
|
+
lines?: boolean;
|
|
13
|
+
/** Line numbers (1-based) to mark with `data-hl`. */
|
|
14
|
+
highlightLines?: readonly number[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* `s`calar `h`igh`l`ight.
|
|
18
|
+
*
|
|
19
|
+
* This string is repeated on every span in the output, so its length is a real
|
|
20
|
+
* share of the rendered HTML — on the JavaScript sample it lands on 233 of
|
|
21
|
+
* them. Short, but still namespaced: a bare `hl-` would be a plausible utility
|
|
22
|
+
* class in the page around the block, and colliding with one would restyle
|
|
23
|
+
* someone else's markup.
|
|
24
|
+
*/
|
|
25
|
+
export declare const DEFAULT_PREFIX = "shl-";
|
|
26
|
+
/**
|
|
27
|
+
* Escapes `&`, `<` and `>`.
|
|
28
|
+
*
|
|
29
|
+
* Most token text contains none of them, so the scan returns the original
|
|
30
|
+
* string without allocating. Quotes are left alone: token text only ever lands
|
|
31
|
+
* in element content, never in an attribute.
|
|
32
|
+
*/
|
|
33
|
+
export declare const escapeHtml: (s: string) => string;
|
|
34
|
+
/**
|
|
35
|
+
* Escapes a value destined for a double-quoted attribute.
|
|
36
|
+
*
|
|
37
|
+
* `escapeHtml` deliberately leaves quotes alone because token text only ever
|
|
38
|
+
* lands in element content. Options do not: `classPrefix`, `className` and the
|
|
39
|
+
* language name are interpolated into `class` and `data-lang`, where a `"`
|
|
40
|
+
* would close the attribute and let the rest of the value become markup.
|
|
41
|
+
*/
|
|
42
|
+
export declare const escapeAttribute: (s: string) => string;
|
|
43
|
+
/**
|
|
44
|
+
* Highlights `code` to an HTML fragment — the inner content of a `<code>`
|
|
45
|
+
* element, with no wrapper of its own.
|
|
46
|
+
*
|
|
47
|
+
* Adjacent ranges sharing a scope are merged into one span, and unscoped text
|
|
48
|
+
* is written bare, so the output stays close to the minimum markup needed.
|
|
49
|
+
*/
|
|
50
|
+
export declare const highlight: (code: string, grammar: CompiledGrammar, options?: RenderOptions) => string;
|
|
51
|
+
export type BlockOptions = RenderOptions & {
|
|
52
|
+
/** Render a line-number gutter. Implies `lines`. */
|
|
53
|
+
lineNumbers?: boolean;
|
|
54
|
+
/** Extra classes on the `<pre>`. */
|
|
55
|
+
className?: string;
|
|
56
|
+
/** Sets `data-lang`, which the theme can surface as a corner label. */
|
|
57
|
+
showLanguage?: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Highlights `code` into a complete, themeable `<pre>` block.
|
|
61
|
+
*
|
|
62
|
+
* The wrapper carries no colors of its own — everything comes from the
|
|
63
|
+
* stylesheet a theme generates, so the same HTML can be restyled (or switched
|
|
64
|
+
* between light and dark) without re-highlighting.
|
|
65
|
+
*/
|
|
66
|
+
export declare const highlightBlock: (code: string, grammar: CompiledGrammar, options?: BlockOptions) => string;
|
|
67
|
+
//# sourceMappingURL=render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../../src/core/render.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAE9C,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,qDAAqD;IACrD,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CACnC,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,SAAS,CAAA;AAMpC;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,GAAG,MAAM,KAAG,MAUtC,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,GAAG,MAAM,KAAG,MAE3C,CAAA;AA4BD;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,UAAS,aAAkB,KAAG,MA8B/F,CAAA;AAoFD,MAAM,MAAM,YAAY,GAAG,aAAa,GAAG;IACzC,oDAAoD;IACpD,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,oCAAoC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,uEAAuE;IACvE,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,UAAS,YAAiB,KAAG,MAkCnG,CAAA"}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { scopeClass } from './scopes.js';
|
|
2
|
+
import { tokenize } from './tokenize.js';
|
|
3
|
+
/**
|
|
4
|
+
* `s`calar `h`igh`l`ight.
|
|
5
|
+
*
|
|
6
|
+
* This string is repeated on every span in the output, so its length is a real
|
|
7
|
+
* share of the rendered HTML — on the JavaScript sample it lands on 233 of
|
|
8
|
+
* them. Short, but still namespaced: a bare `hl-` would be a plausible utility
|
|
9
|
+
* class in the page around the block, and colliding with one would restyle
|
|
10
|
+
* someone else's markup.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_PREFIX = 'shl-';
|
|
13
|
+
const AMP = 38;
|
|
14
|
+
const LT = 60;
|
|
15
|
+
const GT = 62;
|
|
16
|
+
/**
|
|
17
|
+
* Escapes `&`, `<` and `>`.
|
|
18
|
+
*
|
|
19
|
+
* Most token text contains none of them, so the scan returns the original
|
|
20
|
+
* string without allocating. Quotes are left alone: token text only ever lands
|
|
21
|
+
* in element content, never in an attribute.
|
|
22
|
+
*/
|
|
23
|
+
export const escapeHtml = (s) => {
|
|
24
|
+
let out = '';
|
|
25
|
+
let last = 0;
|
|
26
|
+
for (let i = 0; i < s.length; i++) {
|
|
27
|
+
const c = s.charCodeAt(i);
|
|
28
|
+
if (c !== AMP && c !== LT && c !== GT)
|
|
29
|
+
continue;
|
|
30
|
+
out += s.slice(last, i) + (c === AMP ? '&' : c === LT ? '<' : '>');
|
|
31
|
+
last = i + 1;
|
|
32
|
+
}
|
|
33
|
+
return last === 0 ? s : out + s.slice(last);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Escapes a value destined for a double-quoted attribute.
|
|
37
|
+
*
|
|
38
|
+
* `escapeHtml` deliberately leaves quotes alone because token text only ever
|
|
39
|
+
* lands in element content. Options do not: `classPrefix`, `className` and the
|
|
40
|
+
* language name are interpolated into `class` and `data-lang`, where a `"`
|
|
41
|
+
* would close the attribute and let the rest of the value become markup.
|
|
42
|
+
*/
|
|
43
|
+
export const escapeAttribute = (s) => {
|
|
44
|
+
return escapeHtml(s).replace(/"/g, '"');
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Opening tags, built once per scope instead of once per span.
|
|
48
|
+
*
|
|
49
|
+
* Punctuation alone accounts for a large share of the spans in a typical file,
|
|
50
|
+
* so the per-span work is worth keeping to a concatenation and nothing else.
|
|
51
|
+
*/
|
|
52
|
+
const tagCache = new Map();
|
|
53
|
+
const tagsFor = (prefix) => {
|
|
54
|
+
let tags = tagCache.get(prefix);
|
|
55
|
+
if (tags === undefined) {
|
|
56
|
+
tags = new Map();
|
|
57
|
+
tagCache.set(prefix, tags);
|
|
58
|
+
}
|
|
59
|
+
return tags;
|
|
60
|
+
};
|
|
61
|
+
const openTag = (tags, prefix, scope) => {
|
|
62
|
+
let tag = tags.get(scope);
|
|
63
|
+
if (tag === undefined) {
|
|
64
|
+
tag = `<span class="${prefix}${scopeClass(scope)}">`;
|
|
65
|
+
tags.set(scope, tag);
|
|
66
|
+
}
|
|
67
|
+
return tag;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Highlights `code` to an HTML fragment — the inner content of a `<code>`
|
|
71
|
+
* element, with no wrapper of its own.
|
|
72
|
+
*
|
|
73
|
+
* Adjacent ranges sharing a scope are merged into one span, and unscoped text
|
|
74
|
+
* is written bare, so the output stays close to the minimum markup needed.
|
|
75
|
+
*/
|
|
76
|
+
export const highlight = (code, grammar, options = {}) => {
|
|
77
|
+
const prefix = escapeAttribute(options.classPrefix ?? DEFAULT_PREFIX);
|
|
78
|
+
if (options.lines)
|
|
79
|
+
return renderLines(code, grammar, prefix, options.highlightLines).html;
|
|
80
|
+
const tags = tagsFor(prefix);
|
|
81
|
+
let out = '';
|
|
82
|
+
// Pending run of same-scoped ranges, flushed when the scope changes.
|
|
83
|
+
let scope = null;
|
|
84
|
+
let from = 0;
|
|
85
|
+
let to = 0;
|
|
86
|
+
tokenize(code, grammar, (s, start, end) => {
|
|
87
|
+
if (s === scope && start === to) {
|
|
88
|
+
to = end;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (to > from) {
|
|
92
|
+
const text = escapeHtml(code.slice(from, to));
|
|
93
|
+
out += scope === null ? text : `${openTag(tags, prefix, scope)}${text}</span>`;
|
|
94
|
+
}
|
|
95
|
+
scope = s;
|
|
96
|
+
from = start;
|
|
97
|
+
to = end;
|
|
98
|
+
});
|
|
99
|
+
if (to > from) {
|
|
100
|
+
const text = escapeHtml(code.slice(from, to));
|
|
101
|
+
out += scope === null ? text : `${openTag(tags, prefix, scope)}${text}</span>`;
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
};
|
|
105
|
+
const span = (code, scope, from, to, prefix) => {
|
|
106
|
+
const text = escapeHtml(code.slice(from, to));
|
|
107
|
+
return scope === null ? text : `${openTag(tagsFor(prefix), prefix, scope)}${text}</span>`;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Renders one element per line.
|
|
111
|
+
*
|
|
112
|
+
* The newline character lives *inside* each line element rather than between
|
|
113
|
+
* them. Both details are load-bearing:
|
|
114
|
+
*
|
|
115
|
+
* - between block-level elements, a newline in a `white-space: pre` container
|
|
116
|
+
* renders as an extra empty line, doubling the height of every line
|
|
117
|
+
* - without it entirely, an empty line contributes nothing to a selection, so
|
|
118
|
+
* copying the block silently drops every blank line
|
|
119
|
+
*
|
|
120
|
+
* Inside the element, the break is consumed by the line box it belongs to and
|
|
121
|
+
* still lands in the clipboard.
|
|
122
|
+
*/
|
|
123
|
+
const renderLines = (code, grammar, prefix, highlightLines) => {
|
|
124
|
+
const marked = highlightLines?.length ? new Set(highlightLines) : null;
|
|
125
|
+
const lines = [];
|
|
126
|
+
let line = '';
|
|
127
|
+
let scope = null;
|
|
128
|
+
let from = 0;
|
|
129
|
+
let to = 0;
|
|
130
|
+
const flush = () => {
|
|
131
|
+
if (to > from)
|
|
132
|
+
line += span(code, scope, from, to, prefix);
|
|
133
|
+
from = to;
|
|
134
|
+
};
|
|
135
|
+
const endLine = () => {
|
|
136
|
+
flush();
|
|
137
|
+
lines.push(line);
|
|
138
|
+
line = '';
|
|
139
|
+
};
|
|
140
|
+
tokenize(code, grammar, (s, start, end) => {
|
|
141
|
+
let at = start;
|
|
142
|
+
while (at < end) {
|
|
143
|
+
const nl = code.indexOf('\n', at);
|
|
144
|
+
const stop = nl === -1 || nl >= end ? end : nl;
|
|
145
|
+
if (stop > at) {
|
|
146
|
+
if (s !== scope || at !== to) {
|
|
147
|
+
flush();
|
|
148
|
+
scope = s;
|
|
149
|
+
from = at;
|
|
150
|
+
}
|
|
151
|
+
to = stop;
|
|
152
|
+
}
|
|
153
|
+
if (stop === end)
|
|
154
|
+
break;
|
|
155
|
+
endLine();
|
|
156
|
+
at = stop + 1;
|
|
157
|
+
scope = null;
|
|
158
|
+
from = at;
|
|
159
|
+
to = at;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
endLine();
|
|
163
|
+
// A trailing newline in the source closes the last line rather than starting
|
|
164
|
+
// an empty one.
|
|
165
|
+
const endsWithNewline = code.endsWith('\n');
|
|
166
|
+
if (endsWithNewline && lines[lines.length - 1] === '')
|
|
167
|
+
lines.pop();
|
|
168
|
+
let out = '';
|
|
169
|
+
for (let i = 0; i < lines.length; i++) {
|
|
170
|
+
const hl = marked?.has(i + 1) ? ' data-hl' : '';
|
|
171
|
+
const br = i < lines.length - 1 || endsWithNewline ? '\n' : '';
|
|
172
|
+
out += `<span class="${prefix}line"${hl}>${lines[i]}${br}</span>`;
|
|
173
|
+
}
|
|
174
|
+
// The count is the number of rendered line elements, which is exactly what
|
|
175
|
+
// the gutter needs — no second scan of the source to size it.
|
|
176
|
+
return { html: out, count: lines.length };
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Highlights `code` into a complete, themeable `<pre>` block.
|
|
180
|
+
*
|
|
181
|
+
* The wrapper carries no colors of its own — everything comes from the
|
|
182
|
+
* stylesheet a theme generates, so the same HTML can be restyled (or switched
|
|
183
|
+
* between light and dark) without re-highlighting.
|
|
184
|
+
*/
|
|
185
|
+
export const highlightBlock = (code, grammar, options = {}) => {
|
|
186
|
+
const prefix = escapeAttribute(options.classPrefix ?? DEFAULT_PREFIX);
|
|
187
|
+
const block = `${prefix}code`;
|
|
188
|
+
const lined = options.lines || options.lineNumbers;
|
|
189
|
+
// Render once. When lined, the renderer already splits the source into line
|
|
190
|
+
// elements, so it hands back the count too — no second scan to size the
|
|
191
|
+
// gutter, and no separate line-counter that could drift from the split.
|
|
192
|
+
let inner;
|
|
193
|
+
let count = 1;
|
|
194
|
+
if (lined) {
|
|
195
|
+
const rendered = renderLines(code, grammar, prefix, options.highlightLines);
|
|
196
|
+
inner = rendered.html;
|
|
197
|
+
count = rendered.count;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
inner = highlight(code, grammar, options);
|
|
201
|
+
}
|
|
202
|
+
const classes = [
|
|
203
|
+
block,
|
|
204
|
+
lined ? `${block}-lined` : '',
|
|
205
|
+
options.lineNumbers ? `${block}-numbered` : '',
|
|
206
|
+
options.className ? escapeAttribute(options.className) : '',
|
|
207
|
+
]
|
|
208
|
+
.filter(Boolean)
|
|
209
|
+
.join(' ');
|
|
210
|
+
const langAttr = options.showLanguage ? ` data-lang="${escapeAttribute(grammar.name)}"` : '';
|
|
211
|
+
// The gutter is sized for the widest number in the block, not per line, so
|
|
212
|
+
// the code starts at the same column on line 9 and line 10. CSS counters
|
|
213
|
+
// cannot measure that themselves — the stylesheet reads this variable.
|
|
214
|
+
const gutter = options.lineNumbers ? ` style="--shl-line-digits: ${count.toString().length}"` : '';
|
|
215
|
+
return `<pre class="${classes}"${langAttr}${gutter}><code>${inner}</code></pre>`;
|
|
216
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scope vocabulary.
|
|
3
|
+
*
|
|
4
|
+
* Every grammar emits scopes drawn from this list, and every theme styles this
|
|
5
|
+
* list. That contract is what lets a theme look coherent across languages
|
|
6
|
+
* without knowing anything about them.
|
|
7
|
+
*
|
|
8
|
+
* Each scope maps to a short CSS class so the rendered HTML stays small
|
|
9
|
+
* (`<span class="ch-kd">def</span>`). Themes never mention these codes — they
|
|
10
|
+
* are written against the readable scope names and the codes are applied here.
|
|
11
|
+
*
|
|
12
|
+
* Scopes are hierarchical: `function.builtin` falls back to `function` when a
|
|
13
|
+
* theme does not style it. Add a specific scope only when it is worth a
|
|
14
|
+
* distinct color somewhere.
|
|
15
|
+
*/
|
|
16
|
+
export declare const SCOPES: {
|
|
17
|
+
readonly comment: "cm";
|
|
18
|
+
readonly 'comment.doc': "cd";
|
|
19
|
+
readonly string: "s";
|
|
20
|
+
readonly 'string.escape': "se";
|
|
21
|
+
/** Format specs, raw-string prefixes, heredoc markers — string-ish but louder. */
|
|
22
|
+
readonly 'string.special': "ss";
|
|
23
|
+
/** The `${`/`}` of an interpolation, not its contents. */
|
|
24
|
+
readonly interpolation: "ip";
|
|
25
|
+
readonly regexp: "rx";
|
|
26
|
+
readonly number: "n";
|
|
27
|
+
readonly boolean: "bo";
|
|
28
|
+
readonly constant: "c";
|
|
29
|
+
/** `None`, `null`, `undefined`, `nil`. */
|
|
30
|
+
readonly 'constant.builtin': "cb";
|
|
31
|
+
readonly variable: "v";
|
|
32
|
+
/** `self`, `this`, `cls`, `super`. */
|
|
33
|
+
readonly 'variable.builtin': "vb";
|
|
34
|
+
readonly 'variable.parameter': "vp";
|
|
35
|
+
/** `obj.member` — the member half. */
|
|
36
|
+
readonly 'variable.member': "vm";
|
|
37
|
+
/** `__name__`, `__init__` — dunder / magic names. */
|
|
38
|
+
readonly 'variable.special': "vs";
|
|
39
|
+
readonly keyword: "k";
|
|
40
|
+
/** `if`, `for`, `return`, `await`, `yield`. */
|
|
41
|
+
readonly 'keyword.control': "kc";
|
|
42
|
+
/** `def`, `class`, `function`, `const`, `struct`. */
|
|
43
|
+
readonly 'keyword.declaration': "kd";
|
|
44
|
+
readonly 'keyword.import': "ki";
|
|
45
|
+
/** Word-shaped operators: `in`, `not`, `and`, `typeof`, `instanceof`. */
|
|
46
|
+
readonly 'keyword.operator': "ko";
|
|
47
|
+
/** Name at a definition site. */
|
|
48
|
+
readonly function: "f";
|
|
49
|
+
/** Name at a call site. */
|
|
50
|
+
readonly 'function.call': "fc";
|
|
51
|
+
/** `print`, `len`, `parseInt`. */
|
|
52
|
+
readonly 'function.builtin': "fb";
|
|
53
|
+
readonly 'function.method': "fm";
|
|
54
|
+
readonly type: "t";
|
|
55
|
+
/** `int`, `str`, `bool`, `string`, `number`. */
|
|
56
|
+
readonly 'type.builtin': "tb";
|
|
57
|
+
readonly class: "cl";
|
|
58
|
+
readonly namespace: "ns";
|
|
59
|
+
readonly decorator: "dc";
|
|
60
|
+
readonly operator: "o";
|
|
61
|
+
readonly punctuation: "p";
|
|
62
|
+
readonly 'punctuation.bracket': "pb";
|
|
63
|
+
readonly 'punctuation.delimiter': "pd";
|
|
64
|
+
readonly tag: "tg";
|
|
65
|
+
readonly 'tag.attribute': "ta";
|
|
66
|
+
/** CSS declarations, JSON keys, YAML keys. */
|
|
67
|
+
readonly property: "pr";
|
|
68
|
+
readonly selector: "sl";
|
|
69
|
+
/** CSS `px`, `rem`, `%`. */
|
|
70
|
+
readonly unit: "u";
|
|
71
|
+
readonly heading: "h";
|
|
72
|
+
readonly link: "ln";
|
|
73
|
+
readonly emphasis: "em";
|
|
74
|
+
readonly strong: "sg";
|
|
75
|
+
readonly quote: "q";
|
|
76
|
+
readonly list: "li";
|
|
77
|
+
readonly 'diff.plus': "dp";
|
|
78
|
+
readonly 'diff.minus': "dm";
|
|
79
|
+
readonly invalid: "iv";
|
|
80
|
+
};
|
|
81
|
+
export type Scope = keyof typeof SCOPES;
|
|
82
|
+
/** All scope names, ordered most-general first so fallback walks resolve. */
|
|
83
|
+
export declare const SCOPE_NAMES: Scope[];
|
|
84
|
+
/**
|
|
85
|
+
* Resolve a scope to its CSS class suffix. Unknown scopes degrade to a
|
|
86
|
+
* slugified name rather than throwing, so a custom grammar still renders.
|
|
87
|
+
*/
|
|
88
|
+
export declare const scopeClass: (scope: string) => string;
|
|
89
|
+
/**
|
|
90
|
+
* Walk a scope from most to least specific: `a.b.c` -> `a.b.c`, `a.b`, `a`.
|
|
91
|
+
* Used by themes to inherit styling from a parent scope.
|
|
92
|
+
*/
|
|
93
|
+
export declare const scopeChain: (scope: string) => string[];
|
|
94
|
+
//# sourceMappingURL=scopes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scopes.d.ts","sourceRoot":"","sources":["../../src/core/scopes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,MAAM;;;;;IAQjB,kFAAkF;;IAElF,0DAA0D;;;;;;IAQ1D,0CAA0C;;;IAG1C,sCAAsC;;;IAGtC,sCAAsC;;IAEtC,qDAAqD;;;IAKrD,+CAA+C;;IAE/C,qDAAqD;;;IAGrD,yEAAyE;;IAIzE,iCAAiC;;IAEjC,2BAA2B;;IAE3B,kCAAkC;;;;IAIlC,gDAAgD;;;;;;;;;;;IAehD,8CAA8C;;;IAG9C,4BAA4B;;;;;;;;;;;CAYpB,CAAA;AAEV,MAAM,MAAM,KAAK,GAAG,MAAM,OAAO,MAAM,CAAA;AAEvC,6EAA6E;AAC7E,eAAO,MAAM,WAAW,EAA0B,KAAK,EAAE,CAAA;AAEzD;;;GAGG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,MAAM,KAAG,MAM1C,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,MAAM,KAAG,MAAM,EAQhD,CAAA"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scope vocabulary.
|
|
3
|
+
*
|
|
4
|
+
* Every grammar emits scopes drawn from this list, and every theme styles this
|
|
5
|
+
* list. That contract is what lets a theme look coherent across languages
|
|
6
|
+
* without knowing anything about them.
|
|
7
|
+
*
|
|
8
|
+
* Each scope maps to a short CSS class so the rendered HTML stays small
|
|
9
|
+
* (`<span class="ch-kd">def</span>`). Themes never mention these codes — they
|
|
10
|
+
* are written against the readable scope names and the codes are applied here.
|
|
11
|
+
*
|
|
12
|
+
* Scopes are hierarchical: `function.builtin` falls back to `function` when a
|
|
13
|
+
* theme does not style it. Add a specific scope only when it is worth a
|
|
14
|
+
* distinct color somewhere.
|
|
15
|
+
*/
|
|
16
|
+
export const SCOPES = {
|
|
17
|
+
// --- comments -------------------------------------------------------------
|
|
18
|
+
comment: 'cm',
|
|
19
|
+
'comment.doc': 'cd',
|
|
20
|
+
// --- literals -------------------------------------------------------------
|
|
21
|
+
string: 's',
|
|
22
|
+
'string.escape': 'se',
|
|
23
|
+
/** Format specs, raw-string prefixes, heredoc markers — string-ish but louder. */
|
|
24
|
+
'string.special': 'ss',
|
|
25
|
+
/** The `${`/`}` of an interpolation, not its contents. */
|
|
26
|
+
interpolation: 'ip',
|
|
27
|
+
regexp: 'rx',
|
|
28
|
+
number: 'n',
|
|
29
|
+
boolean: 'bo',
|
|
30
|
+
// --- names ----------------------------------------------------------------
|
|
31
|
+
constant: 'c',
|
|
32
|
+
/** `None`, `null`, `undefined`, `nil`. */
|
|
33
|
+
'constant.builtin': 'cb',
|
|
34
|
+
variable: 'v',
|
|
35
|
+
/** `self`, `this`, `cls`, `super`. */
|
|
36
|
+
'variable.builtin': 'vb',
|
|
37
|
+
'variable.parameter': 'vp',
|
|
38
|
+
/** `obj.member` — the member half. */
|
|
39
|
+
'variable.member': 'vm',
|
|
40
|
+
/** `__name__`, `__init__` — dunder / magic names. */
|
|
41
|
+
'variable.special': 'vs',
|
|
42
|
+
// --- keywords -------------------------------------------------------------
|
|
43
|
+
keyword: 'k',
|
|
44
|
+
/** `if`, `for`, `return`, `await`, `yield`. */
|
|
45
|
+
'keyword.control': 'kc',
|
|
46
|
+
/** `def`, `class`, `function`, `const`, `struct`. */
|
|
47
|
+
'keyword.declaration': 'kd',
|
|
48
|
+
'keyword.import': 'ki',
|
|
49
|
+
/** Word-shaped operators: `in`, `not`, `and`, `typeof`, `instanceof`. */
|
|
50
|
+
'keyword.operator': 'ko',
|
|
51
|
+
// --- callables and types --------------------------------------------------
|
|
52
|
+
/** Name at a definition site. */
|
|
53
|
+
function: 'f',
|
|
54
|
+
/** Name at a call site. */
|
|
55
|
+
'function.call': 'fc',
|
|
56
|
+
/** `print`, `len`, `parseInt`. */
|
|
57
|
+
'function.builtin': 'fb',
|
|
58
|
+
'function.method': 'fm',
|
|
59
|
+
type: 't',
|
|
60
|
+
/** `int`, `str`, `bool`, `string`, `number`. */
|
|
61
|
+
'type.builtin': 'tb',
|
|
62
|
+
class: 'cl',
|
|
63
|
+
namespace: 'ns',
|
|
64
|
+
decorator: 'dc',
|
|
65
|
+
// --- syntax ---------------------------------------------------------------
|
|
66
|
+
operator: 'o',
|
|
67
|
+
punctuation: 'p',
|
|
68
|
+
'punctuation.bracket': 'pb',
|
|
69
|
+
'punctuation.delimiter': 'pd',
|
|
70
|
+
// --- markup ---------------------------------------------------------------
|
|
71
|
+
tag: 'tg',
|
|
72
|
+
'tag.attribute': 'ta',
|
|
73
|
+
/** CSS declarations, JSON keys, YAML keys. */
|
|
74
|
+
property: 'pr',
|
|
75
|
+
selector: 'sl',
|
|
76
|
+
/** CSS `px`, `rem`, `%`. */
|
|
77
|
+
unit: 'u',
|
|
78
|
+
heading: 'h',
|
|
79
|
+
link: 'ln',
|
|
80
|
+
emphasis: 'em',
|
|
81
|
+
strong: 'sg',
|
|
82
|
+
quote: 'q',
|
|
83
|
+
list: 'li',
|
|
84
|
+
'diff.plus': 'dp',
|
|
85
|
+
'diff.minus': 'dm',
|
|
86
|
+
invalid: 'iv',
|
|
87
|
+
};
|
|
88
|
+
/** All scope names, ordered most-general first so fallback walks resolve. */
|
|
89
|
+
export const SCOPE_NAMES = Object.keys(SCOPES);
|
|
90
|
+
/**
|
|
91
|
+
* Resolve a scope to its CSS class suffix. Unknown scopes degrade to a
|
|
92
|
+
* slugified name rather than throwing, so a custom grammar still renders.
|
|
93
|
+
*/
|
|
94
|
+
export const scopeClass = (scope) => {
|
|
95
|
+
// `hasOwn` because a scope is data a grammar supplies: a scope named
|
|
96
|
+
// `constructor` or `toString` would otherwise resolve off Object.prototype
|
|
97
|
+
// and stringify a function into the class attribute rather than slugifying.
|
|
98
|
+
const known = Object.hasOwn(SCOPES, scope) ? SCOPES[scope] : undefined;
|
|
99
|
+
return known ?? scope.replace(/[^a-zA-Z0-9]+/g, '-');
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Walk a scope from most to least specific: `a.b.c` -> `a.b.c`, `a.b`, `a`.
|
|
103
|
+
* Used by themes to inherit styling from a parent scope.
|
|
104
|
+
*/
|
|
105
|
+
export const scopeChain = (scope) => {
|
|
106
|
+
const out = [scope];
|
|
107
|
+
let i = scope.lastIndexOf('.');
|
|
108
|
+
while (i > 0) {
|
|
109
|
+
out.push(scope.slice(0, i));
|
|
110
|
+
i = scope.lastIndexOf('.', i - 1);
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CompiledGrammar, Emit, Token } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Runs the state machine over `code`, calling `emit` for every range.
|
|
4
|
+
*
|
|
5
|
+
* Ranges are emitted in order and cover the input exactly — concatenating
|
|
6
|
+
* every emitted slice reproduces the source. Nothing is allocated per token,
|
|
7
|
+
* which is why the HTML renderer can build its output in a single pass.
|
|
8
|
+
*/
|
|
9
|
+
export declare const tokenize: (code: string, grammar: CompiledGrammar, emit: Emit) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Collects tokens into an array.
|
|
12
|
+
*
|
|
13
|
+
* Prefer `highlight()` when the destination is HTML — it skips this
|
|
14
|
+
* intermediate representation entirely. Use this to render into React
|
|
15
|
+
* elements, a canvas, or anything else.
|
|
16
|
+
*/
|
|
17
|
+
export declare const tokenizeToArray: (code: string, grammar: CompiledGrammar) => Token[];
|
|
18
|
+
//# sourceMappingURL=tokenize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokenize.d.ts","sourceRoot":"","sources":["../../src/core/tokenize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAA+B,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAExF;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,EAAE,MAAM,IAAI,KAAG,IAoE7E,CAAA;AA2CD;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,eAAe,KAAG,KAAK,EAM7E,CAAA"}
|