@virgo-nsh/highlighter 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +70 -2
- package/dist/catalog/index.d.ts +1 -0
- package/dist/catalog/index.js +2 -0
- package/dist/highlighter.d.ts +32 -0
- package/dist/highlighter.js +134 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/internal/browser/ONNX-RUNTIME-LICENSE.txt +21 -0
- package/dist/internal/browser/assets.d.ts +52 -0
- package/dist/internal/browser/assets.js +181 -0
- package/dist/internal/browser/decode.d.ts +16 -0
- package/dist/internal/browser/decode.js +75 -0
- package/dist/internal/browser/engine.d.ts +4 -0
- package/dist/internal/browser/engine.js +107 -0
- package/dist/internal/browser/index.d.ts +13 -0
- package/dist/internal/browser/index.js +135 -0
- package/dist/internal/browser/ort-wasm-simd-threaded.mjs +60 -0
- package/dist/internal/browser/ort-wasm-simd-threaded.wasm +0 -0
- package/dist/internal/browser/sessions.d.ts +9 -0
- package/dist/internal/browser/sessions.js +57 -0
- package/dist/internal/browser/structured.d.ts +9 -0
- package/dist/internal/browser/structured.js +88 -0
- package/dist/internal/browser/types.d.ts +37 -0
- package/dist/internal/browser/types.js +1 -0
- package/dist/internal/browser/windowing.d.ts +7 -0
- package/dist/internal/browser/windowing.js +81 -0
- package/dist/internal/browser/worker.d.ts +1 -0
- package/dist/internal/browser/worker.js +22 -0
- package/dist/internal/core/catalog/code.d.ts +2 -0
- package/dist/internal/core/catalog/code.js +371 -0
- package/dist/internal/core/catalog/formats.d.ts +2 -0
- package/dist/internal/core/catalog/formats.js +144 -0
- package/dist/internal/core/catalog/index.d.ts +15 -0
- package/dist/internal/core/catalog/index.js +64 -0
- package/dist/internal/core/catalog/types.d.ts +20 -0
- package/dist/internal/core/catalog/types.js +1 -0
- package/dist/internal/core/composition/html.d.ts +11 -0
- package/dist/internal/core/composition/html.js +61 -0
- package/dist/internal/core/composition/markdown.d.ts +10 -0
- package/dist/internal/core/composition/markdown.js +66 -0
- package/dist/internal/core/composition/php.d.ts +9 -0
- package/dist/internal/core/composition/php.js +54 -0
- package/dist/internal/core/composition/svelte.d.ts +9 -0
- package/dist/internal/core/composition/svelte.js +67 -0
- package/dist/internal/core/composition/vue.d.ts +9 -0
- package/dist/internal/core/composition/vue.js +69 -0
- package/dist/internal/core/highlighter.d.ts +11 -0
- package/dist/internal/core/highlighter.js +26 -0
- package/dist/internal/core/index.d.ts +7 -0
- package/dist/internal/core/index.js +3 -0
- package/dist/internal/core/language.d.ts +2 -0
- package/dist/internal/core/language.js +2 -0
- package/dist/internal/core/manifest.d.ts +8 -0
- package/dist/internal/core/manifest.js +1 -0
- package/dist/internal/core/pack.d.ts +23 -0
- package/dist/internal/core/pack.js +1 -0
- package/dist/internal/core/registry.d.ts +6 -0
- package/dist/internal/core/registry.js +14 -0
- package/dist/internal/core/result.d.ts +30 -0
- package/dist/internal/core/result.js +1 -0
- package/dist/internal/core/schema.d.ts +10 -0
- package/dist/internal/core/schema.js +1 -0
- package/dist/internal/html/index.d.ts +1 -0
- package/dist/internal/html/index.js +1 -0
- package/dist/internal/html/render.d.ts +8 -0
- package/dist/internal/html/render.js +71 -0
- package/dist/model-assets.d.ts +14 -0
- package/dist/model-assets.js +895 -0
- package/dist/remote.d.ts +24 -0
- package/dist/remote.js +129 -0
- package/dist/theme.css +1344 -0
- package/dist/themes/all.css +2150 -0
- package/dist/themes/base.css +942 -0
- package/dist/themes/contrast.css +1344 -0
- package/dist/themes/default.css +1344 -0
- package/dist/themes/index.d.ts +2 -0
- package/dist/themes/index.js +17 -0
- package/dist/themes/languages/typescript.css +2 -0
- package/dist/themes/muted.css +1344 -0
- package/package.json +42 -4
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { codeTypes } from "./code.js";
|
|
2
|
+
import { fileFormats } from "./formats.js";
|
|
3
|
+
export const contentCategories = Object.freeze([
|
|
4
|
+
Object.freeze({ id: "code", label: "Code languages" }),
|
|
5
|
+
Object.freeze({ id: "formats", label: "File formats" }),
|
|
6
|
+
]);
|
|
7
|
+
export const contentTypes = Object.freeze([...codeTypes, ...fileFormats].map((entry) => Object.freeze({
|
|
8
|
+
...entry,
|
|
9
|
+
aliases: Object.freeze([...entry.aliases]),
|
|
10
|
+
extensions: Object.freeze([...entry.extensions]),
|
|
11
|
+
frameworks: entry.frameworks && Object.freeze([...entry.frameworks]),
|
|
12
|
+
modelDependencies: entry.modelDependencies && Object.freeze([...entry.modelDependencies]),
|
|
13
|
+
fileVariants: entry.fileVariants &&
|
|
14
|
+
Object.freeze(entry.fileVariants.map((variant) => Object.freeze({ ...variant, extensions: Object.freeze([...variant.extensions]) }))),
|
|
15
|
+
})));
|
|
16
|
+
const names = new Map();
|
|
17
|
+
for (const entry of contentTypes) {
|
|
18
|
+
const register = (name, kind) => {
|
|
19
|
+
const key = name.toLowerCase();
|
|
20
|
+
if (names.has(key))
|
|
21
|
+
throw new Error(`Duplicate content name: ${name}`);
|
|
22
|
+
names.set(key, { kind, entry });
|
|
23
|
+
};
|
|
24
|
+
const kind = entry.documentKind ?? entry.id;
|
|
25
|
+
register(entry.id, kind);
|
|
26
|
+
for (const alias of entry.aliases)
|
|
27
|
+
register(alias, kind);
|
|
28
|
+
for (const variant of entry.fileVariants ?? [])
|
|
29
|
+
register(variant.id, variant.id);
|
|
30
|
+
}
|
|
31
|
+
/** Resolve only explicitly registered names; unknown names remain unsupported. */
|
|
32
|
+
export function normalizeLanguageId(language) {
|
|
33
|
+
const id = language.toLowerCase();
|
|
34
|
+
return names.get(id)?.kind ?? id;
|
|
35
|
+
}
|
|
36
|
+
export function getContentType(name) {
|
|
37
|
+
return names.get(name.toLowerCase())?.entry;
|
|
38
|
+
}
|
|
39
|
+
const filenames = contentTypes
|
|
40
|
+
.flatMap((entry) => {
|
|
41
|
+
const variants = entry.fileVariants ?? [];
|
|
42
|
+
const variantExtensions = new Set(variants.flatMap((variant) => [...variant.extensions]));
|
|
43
|
+
return [
|
|
44
|
+
...entry.extensions
|
|
45
|
+
.filter((extension) => !variantExtensions.has(extension))
|
|
46
|
+
.map((extension) => ({ extension, kind: entry.documentKind ?? entry.id, entry })),
|
|
47
|
+
...variants.flatMap((variant) => variant.extensions.map((extension) => ({ extension, kind: variant.id, entry }))),
|
|
48
|
+
];
|
|
49
|
+
})
|
|
50
|
+
.sort((a, b) => b.extension.length - a.extension.length);
|
|
51
|
+
/** Explicit filename helper, not content/language auto-detection by the highlighter. */
|
|
52
|
+
export function documentKindForFilename(filename, contentTypeId) {
|
|
53
|
+
const entry = contentTypeId ? getContentType(contentTypeId) : undefined;
|
|
54
|
+
if (contentTypeId && !entry)
|
|
55
|
+
return undefined;
|
|
56
|
+
// JSONC can use .json; dotenv examples can use .env.local or extensionless names.
|
|
57
|
+
if (entry?.id === "jsonc" || entry?.id === "dotenv")
|
|
58
|
+
return entry.id;
|
|
59
|
+
const match = filenames.find((item) => (!entry || item.entry.modelId === entry.modelId) &&
|
|
60
|
+
(item.extension.startsWith(".")
|
|
61
|
+
? filename.endsWith(item.extension)
|
|
62
|
+
: filename === item.extension));
|
|
63
|
+
return match?.kind ?? (contentTypeId ? normalizeLanguageId(contentTypeId) : undefined);
|
|
64
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type ContentCategory = "code" | "formats";
|
|
2
|
+
/** Display/input metadata only: never imports a model or inference runtime. */
|
|
3
|
+
export interface ContentType {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly label: string;
|
|
6
|
+
readonly category: ContentCategory;
|
|
7
|
+
readonly aliases: readonly string[];
|
|
8
|
+
readonly modelId: string;
|
|
9
|
+
readonly extensions: readonly string[];
|
|
10
|
+
readonly iconId: string;
|
|
11
|
+
/** Named framework profile, e.g. Flutter, handled as its underlying source kind. */
|
|
12
|
+
readonly documentKind?: string;
|
|
13
|
+
readonly frameworks?: readonly string[];
|
|
14
|
+
readonly modelDependencies?: readonly string[];
|
|
15
|
+
/** Filename variants stay internal rather than becoming separate menu entries. */
|
|
16
|
+
readonly fileVariants?: readonly {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly extensions: readonly string[];
|
|
19
|
+
}[];
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HighlightRequest, SemanticPack } from "../pack.js";
|
|
2
|
+
import type { HighlightResult } from "../result.js";
|
|
3
|
+
export interface HtmlCompositionOptions {
|
|
4
|
+
base: SemanticPack;
|
|
5
|
+
highlightEmbedded(request: HighlightRequest): Promise<HighlightResult>;
|
|
6
|
+
}
|
|
7
|
+
/** Compose existing neural packs only inside regions predicted by the HTML model.
|
|
8
|
+
* No source parsing, tag scanning, entities decoding, execution or Gold cuts.
|
|
9
|
+
* The callback can use any runtime/transport implementing the existing contract.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createCompositeHtmlPack({ base, highlightEmbedded, }: HtmlCompositionOptions): SemanticPack;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
function validate(source, result) {
|
|
2
|
+
if (result.offsetEncoding !== "utf-16")
|
|
3
|
+
throw new Error("HTML composition requires UTF-16 model offsets");
|
|
4
|
+
const boundaries = new Set([0]);
|
|
5
|
+
let unit = 0;
|
|
6
|
+
for (const character of source) {
|
|
7
|
+
unit += character.length;
|
|
8
|
+
boundaries.add(unit);
|
|
9
|
+
}
|
|
10
|
+
let end = 0;
|
|
11
|
+
for (const span of result.spans) {
|
|
12
|
+
if (!Number.isInteger(span.start) ||
|
|
13
|
+
!Number.isInteger(span.end) ||
|
|
14
|
+
span.start < end ||
|
|
15
|
+
span.end <= span.start ||
|
|
16
|
+
!boundaries.has(span.start) ||
|
|
17
|
+
!boundaries.has(span.end)) {
|
|
18
|
+
throw new Error("Invalid or overlapping model spans in HTML composition");
|
|
19
|
+
}
|
|
20
|
+
end = span.end;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Compose existing neural packs only inside regions predicted by the HTML model.
|
|
24
|
+
* No source parsing, tag scanning, entities decoding, execution or Gold cuts.
|
|
25
|
+
* The callback can use any runtime/transport implementing the existing contract.
|
|
26
|
+
*/
|
|
27
|
+
export function createCompositeHtmlPack({ base, highlightEmbedded, }) {
|
|
28
|
+
return {
|
|
29
|
+
manifest: base.manifest,
|
|
30
|
+
async highlight(request) {
|
|
31
|
+
if (request.documentKind !== "html")
|
|
32
|
+
throw new Error("Unsupported HTML document kind: " + request.documentKind);
|
|
33
|
+
const result = await base.highlight(request);
|
|
34
|
+
validate(request.source, result);
|
|
35
|
+
const spans = [];
|
|
36
|
+
for (const span of result.spans) {
|
|
37
|
+
const documentKind = span.tokenType === "embedded-javascript"
|
|
38
|
+
? "javascript"
|
|
39
|
+
: span.tokenType === "embedded-css"
|
|
40
|
+
? "css"
|
|
41
|
+
: undefined;
|
|
42
|
+
if (!documentKind) {
|
|
43
|
+
spans.push(span);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const source = request.source.slice(span.start, span.end);
|
|
47
|
+
const child = await highlightEmbedded({ source, documentKind });
|
|
48
|
+
validate(source, child);
|
|
49
|
+
for (const item of child.spans) {
|
|
50
|
+
spans.push({
|
|
51
|
+
...item,
|
|
52
|
+
start: span.start + item.start,
|
|
53
|
+
end: span.start + item.end,
|
|
54
|
+
documentKind: item.documentKind ?? documentKind,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { offsetEncoding: "utf-16", spans };
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { HighlightRequest, SemanticPack } from "../pack.js";
|
|
2
|
+
import type { HighlightResult } from "../result.js";
|
|
3
|
+
/** Reuse registered packs inside predicted code bodies. Only an explicit,
|
|
4
|
+
* model-delimited info word selects a child: no fence/source parser or guessing.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createCompositeMarkdownPack(options: {
|
|
7
|
+
base: SemanticPack;
|
|
8
|
+
resolveLanguage(name: string): string | undefined;
|
|
9
|
+
highlightEmbedded(request: HighlightRequest): Promise<HighlightResult>;
|
|
10
|
+
}): SemanticPack;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function validate(source, result) {
|
|
2
|
+
if (result.offsetEncoding !== "utf-16")
|
|
3
|
+
throw new Error("Markdown composition requires UTF-16 offsets");
|
|
4
|
+
const boundaries = new Set([0]);
|
|
5
|
+
let unit = 0;
|
|
6
|
+
for (const character of source) {
|
|
7
|
+
unit += character.length;
|
|
8
|
+
boundaries.add(unit);
|
|
9
|
+
}
|
|
10
|
+
let end = 0;
|
|
11
|
+
for (const span of result.spans) {
|
|
12
|
+
if (!Number.isInteger(span.start) ||
|
|
13
|
+
!Number.isInteger(span.end) ||
|
|
14
|
+
span.start < end ||
|
|
15
|
+
span.end <= span.start ||
|
|
16
|
+
!boundaries.has(span.start) ||
|
|
17
|
+
!boundaries.has(span.end)) {
|
|
18
|
+
throw new Error("Invalid Markdown base/child offsets");
|
|
19
|
+
}
|
|
20
|
+
end = span.end;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Reuse registered packs inside predicted code bodies. Only an explicit,
|
|
24
|
+
* model-delimited info word selects a child: no fence/source parser or guessing.
|
|
25
|
+
*/
|
|
26
|
+
export function createCompositeMarkdownPack(options) {
|
|
27
|
+
return {
|
|
28
|
+
manifest: options.base.manifest,
|
|
29
|
+
async highlight(request) {
|
|
30
|
+
if (request.documentKind !== "markdown")
|
|
31
|
+
throw new Error("Unsupported Markdown document kind");
|
|
32
|
+
const result = await options.base.highlight(request);
|
|
33
|
+
validate(request.source, result);
|
|
34
|
+
const spans = [];
|
|
35
|
+
let language;
|
|
36
|
+
for (const span of result.spans) {
|
|
37
|
+
if (span.tokenType === "code-fence")
|
|
38
|
+
language = undefined;
|
|
39
|
+
if (span.tokenType === "code-language") {
|
|
40
|
+
language = options.resolveLanguage(request.source.slice(span.start, span.end).trim());
|
|
41
|
+
}
|
|
42
|
+
if (span.tokenType !== "embedded-code") {
|
|
43
|
+
spans.push(span);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const documentKind = language;
|
|
47
|
+
language = undefined;
|
|
48
|
+
if (!documentKind || documentKind === "markdown") {
|
|
49
|
+
spans.push({ ...span, tokenType: "code" });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const source = request.source.slice(span.start, span.end);
|
|
53
|
+
const child = await options.highlightEmbedded({ source, documentKind });
|
|
54
|
+
validate(source, child);
|
|
55
|
+
for (const item of child.spans)
|
|
56
|
+
spans.push({
|
|
57
|
+
...item,
|
|
58
|
+
start: span.start + item.start,
|
|
59
|
+
end: span.start + item.end,
|
|
60
|
+
documentKind: item.documentKind ?? documentKind,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return { offsetEncoding: "utf-16", spans };
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HighlightRequest, SemanticPack } from "../pack.js";
|
|
2
|
+
import type { HighlightResult } from "../result.js";
|
|
3
|
+
/** Only neural PHP region predictions choose child HTML inference.
|
|
4
|
+
* No source tag scanning, runtime parser, PHP execution or Gold boundaries.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createCompositePhpPack(options: {
|
|
7
|
+
base: SemanticPack;
|
|
8
|
+
highlightEmbedded(request: HighlightRequest): Promise<HighlightResult>;
|
|
9
|
+
}): SemanticPack;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
function validate(source, result) {
|
|
2
|
+
if (result.offsetEncoding !== "utf-16")
|
|
3
|
+
throw new Error("PHP composition requires UTF-16 offsets");
|
|
4
|
+
const boundaries = new Set([0]);
|
|
5
|
+
let unit = 0;
|
|
6
|
+
for (const character of source) {
|
|
7
|
+
unit += character.length;
|
|
8
|
+
boundaries.add(unit);
|
|
9
|
+
}
|
|
10
|
+
let end = 0;
|
|
11
|
+
for (const span of result.spans) {
|
|
12
|
+
if (!Number.isInteger(span.start) ||
|
|
13
|
+
!Number.isInteger(span.end) ||
|
|
14
|
+
span.start < end ||
|
|
15
|
+
span.end <= span.start ||
|
|
16
|
+
!boundaries.has(span.start) ||
|
|
17
|
+
!boundaries.has(span.end))
|
|
18
|
+
throw new Error("Invalid or overlapping PHP composition spans");
|
|
19
|
+
end = span.end;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Only neural PHP region predictions choose child HTML inference.
|
|
23
|
+
* No source tag scanning, runtime parser, PHP execution or Gold boundaries.
|
|
24
|
+
*/
|
|
25
|
+
export function createCompositePhpPack(options) {
|
|
26
|
+
return {
|
|
27
|
+
manifest: options.base.manifest,
|
|
28
|
+
async highlight(request) {
|
|
29
|
+
if (request.documentKind !== "php")
|
|
30
|
+
throw new Error("Unsupported PHP document kind");
|
|
31
|
+
const result = await options.base.highlight(request);
|
|
32
|
+
validate(request.source, result);
|
|
33
|
+
const spans = [];
|
|
34
|
+
for (const span of result.spans) {
|
|
35
|
+
if (span.tokenType !== "embedded-html") {
|
|
36
|
+
spans.push(span);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const source = request.source.slice(span.start, span.end);
|
|
40
|
+
const child = await options.highlightEmbedded({ source, documentKind: "html" });
|
|
41
|
+
validate(source, child);
|
|
42
|
+
for (const token of child.spans) {
|
|
43
|
+
spans.push({
|
|
44
|
+
...token,
|
|
45
|
+
start: span.start + token.start,
|
|
46
|
+
end: span.start + token.end,
|
|
47
|
+
documentKind: token.documentKind ?? "html",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { offsetEncoding: "utf-16", spans };
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HighlightRequest, SemanticPack } from "../pack.js";
|
|
2
|
+
import type { HighlightResult } from "../result.js";
|
|
3
|
+
export declare const EXPRESSION_PREFIX = "const __nsh_svelte_value = (\n";
|
|
4
|
+
export declare const EXPRESSION_SUFFIX = "\n);";
|
|
5
|
+
/** Neural regions only. Reuses existing code packs without parsing Svelte source. */
|
|
6
|
+
export declare function createCompositeSveltePack(options: {
|
|
7
|
+
base: SemanticPack;
|
|
8
|
+
highlightEmbedded(request: HighlightRequest): Promise<HighlightResult>;
|
|
9
|
+
}): SemanticPack;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export const EXPRESSION_PREFIX = "const __nsh_svelte_value = (\n";
|
|
2
|
+
export const EXPRESSION_SUFFIX = "\n);";
|
|
3
|
+
const languages = {
|
|
4
|
+
"embedded-javascript": "javascript",
|
|
5
|
+
"embedded-typescript": "typescript",
|
|
6
|
+
"embedded-css": "css",
|
|
7
|
+
"embedded-scss": "scss",
|
|
8
|
+
"embedded-less": "less",
|
|
9
|
+
"embedded-expression": "typescript",
|
|
10
|
+
};
|
|
11
|
+
function validate(source, result) {
|
|
12
|
+
if (result.offsetEncoding !== "utf-16")
|
|
13
|
+
throw new Error("Svelte composition requires UTF-16 model offsets");
|
|
14
|
+
const boundaries = new Set([0]);
|
|
15
|
+
let unit = 0, end = 0;
|
|
16
|
+
for (const character of source) {
|
|
17
|
+
unit += character.length;
|
|
18
|
+
boundaries.add(unit);
|
|
19
|
+
}
|
|
20
|
+
for (const span of result.spans) {
|
|
21
|
+
if (!Number.isInteger(span.start) ||
|
|
22
|
+
!Number.isInteger(span.end) ||
|
|
23
|
+
span.start < end ||
|
|
24
|
+
span.end <= span.start ||
|
|
25
|
+
!boundaries.has(span.start) ||
|
|
26
|
+
!boundaries.has(span.end))
|
|
27
|
+
throw new Error("Invalid Svelte child/base spans");
|
|
28
|
+
end = span.end;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Neural regions only. Reuses existing code packs without parsing Svelte source. */
|
|
32
|
+
export function createCompositeSveltePack(options) {
|
|
33
|
+
return {
|
|
34
|
+
manifest: options.base.manifest,
|
|
35
|
+
async highlight(request) {
|
|
36
|
+
if (request.documentKind !== "svelte")
|
|
37
|
+
throw new Error("Unsupported Svelte document kind");
|
|
38
|
+
const result = await options.base.highlight(request);
|
|
39
|
+
validate(request.source, result);
|
|
40
|
+
const spans = [];
|
|
41
|
+
for (const span of result.spans) {
|
|
42
|
+
const documentKind = languages[span.tokenType];
|
|
43
|
+
if (!documentKind) {
|
|
44
|
+
spans.push(span);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const raw = request.source.slice(span.start, span.end);
|
|
48
|
+
const expression = span.tokenType === "embedded-expression";
|
|
49
|
+
const prefix = expression ? EXPRESSION_PREFIX : "", suffix = expression ? EXPRESSION_SUFFIX : "";
|
|
50
|
+
const source = prefix + raw + suffix;
|
|
51
|
+
const child = await options.highlightEmbedded({ source, documentKind });
|
|
52
|
+
validate(source, child);
|
|
53
|
+
for (const item of child.spans) {
|
|
54
|
+
const start = Math.max(item.start, prefix.length), end = Math.min(item.end, prefix.length + raw.length);
|
|
55
|
+
if (start < end)
|
|
56
|
+
spans.push({
|
|
57
|
+
...item,
|
|
58
|
+
documentKind: item.documentKind ?? documentKind,
|
|
59
|
+
start: span.start + start - prefix.length,
|
|
60
|
+
end: span.start + end - prefix.length,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return { offsetEncoding: "utf-16", spans };
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HighlightRequest, SemanticPack } from "../pack.js";
|
|
2
|
+
import type { HighlightResult } from "../result.js";
|
|
3
|
+
export declare const EXPRESSION_PREFIX = "const __nsh_vue_value = (\n";
|
|
4
|
+
export declare const EXPRESSION_SUFFIX = "\n);";
|
|
5
|
+
/** Neural regions only. Reuses existing code packs without parsing Vue source. */
|
|
6
|
+
export declare function createCompositeVuePack(options: {
|
|
7
|
+
base: SemanticPack;
|
|
8
|
+
highlightEmbedded(request: HighlightRequest): Promise<HighlightResult>;
|
|
9
|
+
}): SemanticPack;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export const EXPRESSION_PREFIX = "const __nsh_vue_value = (\n";
|
|
2
|
+
export const EXPRESSION_SUFFIX = "\n);";
|
|
3
|
+
const languages = {
|
|
4
|
+
"embedded-javascript": "javascript",
|
|
5
|
+
"embedded-typescript": "typescript",
|
|
6
|
+
"embedded-jsx": "jsx",
|
|
7
|
+
"embedded-tsx": "tsx",
|
|
8
|
+
"embedded-css": "css",
|
|
9
|
+
"embedded-scss": "scss",
|
|
10
|
+
"embedded-less": "less",
|
|
11
|
+
"embedded-expression": "typescript",
|
|
12
|
+
};
|
|
13
|
+
function validate(source, result) {
|
|
14
|
+
if (result.offsetEncoding !== "utf-16")
|
|
15
|
+
throw new Error("Vue composition requires UTF-16 model offsets");
|
|
16
|
+
const boundaries = new Set([0]);
|
|
17
|
+
let unit = 0, end = 0;
|
|
18
|
+
for (const character of source) {
|
|
19
|
+
unit += character.length;
|
|
20
|
+
boundaries.add(unit);
|
|
21
|
+
}
|
|
22
|
+
for (const span of result.spans) {
|
|
23
|
+
if (!Number.isInteger(span.start) ||
|
|
24
|
+
!Number.isInteger(span.end) ||
|
|
25
|
+
span.start < end ||
|
|
26
|
+
span.end <= span.start ||
|
|
27
|
+
!boundaries.has(span.start) ||
|
|
28
|
+
!boundaries.has(span.end))
|
|
29
|
+
throw new Error("Invalid Vue child/base spans");
|
|
30
|
+
end = span.end;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Neural regions only. Reuses existing code packs without parsing Vue source. */
|
|
34
|
+
export function createCompositeVuePack(options) {
|
|
35
|
+
return {
|
|
36
|
+
manifest: options.base.manifest,
|
|
37
|
+
async highlight(request) {
|
|
38
|
+
if (request.documentKind !== "vue")
|
|
39
|
+
throw new Error("Unsupported Vue document kind");
|
|
40
|
+
const result = await options.base.highlight(request);
|
|
41
|
+
validate(request.source, result);
|
|
42
|
+
const spans = [];
|
|
43
|
+
for (const span of result.spans) {
|
|
44
|
+
const documentKind = languages[span.tokenType];
|
|
45
|
+
if (!documentKind) {
|
|
46
|
+
spans.push(span);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const raw = request.source.slice(span.start, span.end);
|
|
50
|
+
const expression = span.tokenType === "embedded-expression";
|
|
51
|
+
const prefix = expression ? EXPRESSION_PREFIX : "", suffix = expression ? EXPRESSION_SUFFIX : "";
|
|
52
|
+
const source = prefix + raw + suffix;
|
|
53
|
+
const child = await options.highlightEmbedded({ source, documentKind });
|
|
54
|
+
validate(source, child);
|
|
55
|
+
for (const item of child.spans) {
|
|
56
|
+
const start = Math.max(item.start, prefix.length), end = Math.min(item.end, prefix.length + raw.length);
|
|
57
|
+
if (start < end)
|
|
58
|
+
spans.push({
|
|
59
|
+
...item,
|
|
60
|
+
documentKind: item.documentKind ?? documentKind,
|
|
61
|
+
start: span.start + start - prefix.length,
|
|
62
|
+
end: span.start + end - prefix.length,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { offsetEncoding: "utf-16", spans };
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HighlightRequest, SemanticPack } from "./pack.js";
|
|
2
|
+
import type { HighlightResult } from "./result.js";
|
|
3
|
+
export interface Highlighter {
|
|
4
|
+
highlight(request: HighlightRequest): Promise<HighlightResult>;
|
|
5
|
+
supports(documentKind: string): boolean;
|
|
6
|
+
documentKinds(): readonly string[];
|
|
7
|
+
}
|
|
8
|
+
export interface CreateHighlighterOptions {
|
|
9
|
+
packs: readonly SemanticPack[];
|
|
10
|
+
}
|
|
11
|
+
export declare function createHighlighter(options: CreateHighlighterOptions): Highlighter;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PackRegistry } from "./registry.js";
|
|
2
|
+
export function createHighlighter(options) {
|
|
3
|
+
const registry = new PackRegistry();
|
|
4
|
+
const documentKinds = new Set();
|
|
5
|
+
for (const pack of options.packs) {
|
|
6
|
+
registry.register(pack);
|
|
7
|
+
for (const documentKind of pack.manifest.documentKinds) {
|
|
8
|
+
documentKinds.add(documentKind.id);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
async highlight(request) {
|
|
13
|
+
const pack = registry.get(request.documentKind);
|
|
14
|
+
if (!pack) {
|
|
15
|
+
throw new Error(`Unsupported document kind "${request.documentKind}".`);
|
|
16
|
+
}
|
|
17
|
+
return pack.highlight(request);
|
|
18
|
+
},
|
|
19
|
+
supports(documentKind) {
|
|
20
|
+
return registry.get(documentKind) !== undefined;
|
|
21
|
+
},
|
|
22
|
+
documentKinds() {
|
|
23
|
+
return [...documentKinds];
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { createHighlighter, type CreateHighlighterOptions, type Highlighter, } from "./highlighter.js";
|
|
2
|
+
export { normalizeLanguageId } from "./language.js";
|
|
3
|
+
export type { DocumentKindManifest, PackManifest } from "./manifest.js";
|
|
4
|
+
export type { HighlightRequest, SemanticPack } from "./pack.js";
|
|
5
|
+
export { PackRegistry } from "./registry.js";
|
|
6
|
+
export type { HighlightResult, OffsetEncoding, SemanticSpan } from "./result.js";
|
|
7
|
+
export type { PackSchema, SemanticTokenModifierSchema, SemanticTokenTypeSchema } from "./schema.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PackManifest } from "./manifest.js";
|
|
2
|
+
import type { HighlightResult } from "./result.js";
|
|
3
|
+
export interface HighlightRequest {
|
|
4
|
+
source: string;
|
|
5
|
+
documentKind: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Runtime boundary between semantic inference and NSH consumers.
|
|
9
|
+
*
|
|
10
|
+
* A pack may run a model locally, inside the browser (for example through
|
|
11
|
+
* WASM/WebGPU/ONNX), or behind a remote transport. Consumers depend only on
|
|
12
|
+
* this contract and the returned HighlightResult.
|
|
13
|
+
*/
|
|
14
|
+
export interface SemanticPack {
|
|
15
|
+
/**
|
|
16
|
+
* Metadata describing this pack and its supported document kinds.
|
|
17
|
+
*/
|
|
18
|
+
manifest: PackManifest;
|
|
19
|
+
/**
|
|
20
|
+
* Analyze a source document and return semantic spans.
|
|
21
|
+
*/
|
|
22
|
+
highlight(request: HighlightRequest): Promise<HighlightResult>;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class PackRegistry {
|
|
2
|
+
packs = new Map();
|
|
3
|
+
register(pack) {
|
|
4
|
+
for (const documentKind of pack.manifest.documentKinds) {
|
|
5
|
+
if (this.packs.has(documentKind.id)) {
|
|
6
|
+
throw new Error(`Document kind "${documentKind.id}" is already registered.`);
|
|
7
|
+
}
|
|
8
|
+
this.packs.set(documentKind.id, pack);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
get(documentKind) {
|
|
12
|
+
return this.packs.get(documentKind);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type OffsetEncoding = "utf-8" | "utf-16" | "utf-32";
|
|
2
|
+
export interface SemanticSpan {
|
|
3
|
+
/**
|
|
4
|
+
* Start offset in the source document.
|
|
5
|
+
* Inclusive.
|
|
6
|
+
*/
|
|
7
|
+
start: number;
|
|
8
|
+
/**
|
|
9
|
+
* End offset in the source document.
|
|
10
|
+
* Exclusive.
|
|
11
|
+
*/
|
|
12
|
+
end: number;
|
|
13
|
+
/**
|
|
14
|
+
* Semantic token type defined by the active pack.
|
|
15
|
+
*/
|
|
16
|
+
tokenType: string;
|
|
17
|
+
/**
|
|
18
|
+
* Semantic modifiers defined by the active pack.
|
|
19
|
+
*/
|
|
20
|
+
modifiers?: readonly string[];
|
|
21
|
+
/** Embedded source language for rendering; omitted for the document's own spans. */
|
|
22
|
+
documentKind?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface HighlightResult {
|
|
25
|
+
/**
|
|
26
|
+
* Encoding used to interpret span offsets.
|
|
27
|
+
*/
|
|
28
|
+
offsetEncoding: OffsetEncoding;
|
|
29
|
+
spans: SemanticSpan[];
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface SemanticTokenTypeSchema {
|
|
2
|
+
id: string;
|
|
3
|
+
}
|
|
4
|
+
export interface SemanticTokenModifierSchema {
|
|
5
|
+
id: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PackSchema {
|
|
8
|
+
tokenTypes: readonly SemanticTokenTypeSchema[];
|
|
9
|
+
tokenModifiers: readonly SemanticTokenModifierSchema[];
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { renderCodeToHtml, renderToHtml, type RenderToHtmlOptions } from "./render.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { renderCodeToHtml, renderToHtml } from "./render.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HighlightResult } from "../core/index.js";
|
|
2
|
+
export interface RenderToHtmlOptions {
|
|
3
|
+
source: string;
|
|
4
|
+
documentKind: string;
|
|
5
|
+
result: HighlightResult;
|
|
6
|
+
}
|
|
7
|
+
export declare function renderCodeToHtml({ source, result }: RenderToHtmlOptions): string;
|
|
8
|
+
export declare function renderToHtml(options: RenderToHtmlOptions): string;
|