ntk 7.7.0 → 8.0.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/README.md +9 -10
- package/lib/index.js +2 -29
- package/lib/text/layout.js +4 -4
- package/lib/widgets/svgview.js +5 -4
- package/package.json +4 -9
- package/lib/widgets/css.js +0 -673
- package/lib/widgets/highlight.js +0 -97
- package/lib/widgets/htmlview.js +0 -893
- package/lib/widgets/markdown.js +0 -148
- package/lib/widgets/markdownview.js +0 -492
- package/lib/widgets/tex.js +0 -679
- package/lib/yoga.js +0 -119
package/lib/widgets/highlight.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// Fence syntax highlighting for MarkdownView — a thin adapter over
|
|
2
|
-
// highlight.js (the `common` build: ~36 languages plus their aliases),
|
|
3
|
-
// mapping its output onto flat { text, kind } tokens. The tokenizer used
|
|
4
|
-
// to be hand-rolled here; highlight.js is maintained, pure JS and far more
|
|
5
|
-
// accurate.
|
|
6
|
-
//
|
|
7
|
-
// tokenize(code, lang) returns [{ text, kind }] with kinds:
|
|
8
|
-
// 'keyword' | 'literal' | 'string' | 'number' | 'comment' | 'tag'
|
|
9
|
-
// | 'attr' | 'function' | 'plain'
|
|
10
|
-
// Token texts concatenate back to the exact input.
|
|
11
|
-
|
|
12
|
-
import hljs from 'highlight.js/lib/common';
|
|
13
|
-
|
|
14
|
-
// highlight.js scope -> token kind; unlisted scopes inherit the enclosing
|
|
15
|
-
// span's kind (nested spans) or fall back to 'plain'
|
|
16
|
-
const SCOPE_KINDS = {
|
|
17
|
-
keyword: 'keyword',
|
|
18
|
-
built_in: 'keyword',
|
|
19
|
-
type: 'keyword',
|
|
20
|
-
section: 'keyword',
|
|
21
|
-
meta: 'keyword',
|
|
22
|
-
'selector-tag': 'keyword',
|
|
23
|
-
literal: 'literal',
|
|
24
|
-
symbol: 'literal',
|
|
25
|
-
string: 'string',
|
|
26
|
-
regexp: 'string',
|
|
27
|
-
addition: 'string',
|
|
28
|
-
number: 'number',
|
|
29
|
-
comment: 'comment',
|
|
30
|
-
quote: 'comment',
|
|
31
|
-
doctag: 'comment',
|
|
32
|
-
deletion: 'comment',
|
|
33
|
-
name: 'tag',
|
|
34
|
-
tag: 'tag',
|
|
35
|
-
'selector-id': 'tag',
|
|
36
|
-
'selector-class': 'tag',
|
|
37
|
-
attr: 'attr',
|
|
38
|
-
attribute: 'attr',
|
|
39
|
-
property: 'attr',
|
|
40
|
-
variable: 'attr',
|
|
41
|
-
'template-variable': 'attr',
|
|
42
|
-
title: 'function'
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const ENTITIES = { '&': '&', '<': '<', '>': '>', '"': '"', ''': "'", ''': "'" };
|
|
46
|
-
|
|
47
|
-
function unescapeHtml(text) {
|
|
48
|
-
return text.replace(/&(?:amp|lt|gt|quot|#x27|#39);/g, (m) => ENTITIES[m]);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Tokenize source code for a fence language tag. Unknown languages fall
|
|
53
|
-
* back to a single plain token, so callers can highlight unconditionally.
|
|
54
|
-
*
|
|
55
|
-
* @param {string} code
|
|
56
|
-
* @param {string} [lang] fence tag, e.g. 'js', 'python'; hljs aliases work
|
|
57
|
-
* @returns {Array<{text: string, kind: string}>}
|
|
58
|
-
*/
|
|
59
|
-
export function tokenize(code, lang) {
|
|
60
|
-
code = String(code ?? '');
|
|
61
|
-
if (!code) return [];
|
|
62
|
-
const name = String(lang ?? '').toLowerCase();
|
|
63
|
-
if (!name || !hljs.getLanguage(name)) return [{ text: code, kind: 'plain' }];
|
|
64
|
-
|
|
65
|
-
let html;
|
|
66
|
-
try {
|
|
67
|
-
html = hljs.highlight(code, { language: name, ignoreIllegals: true }).value;
|
|
68
|
-
} catch {
|
|
69
|
-
return [{ text: code, kind: 'plain' }];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// flatten the emitted <span class="hljs-scope"> tree, tracking nesting
|
|
73
|
-
const tokens = [];
|
|
74
|
-
const stack = ['plain'];
|
|
75
|
-
const emit = (text, kind) => {
|
|
76
|
-
if (!text) return;
|
|
77
|
-
const last = tokens[tokens.length - 1];
|
|
78
|
-
if (last && last.kind === kind) last.text += text;
|
|
79
|
-
else tokens.push({ text, kind });
|
|
80
|
-
};
|
|
81
|
-
const re = /<span class="([^"]*)">|<\/span>|([^<]+)|</g;
|
|
82
|
-
let m;
|
|
83
|
-
while ((m = re.exec(html))) {
|
|
84
|
-
if (m[1] !== undefined) {
|
|
85
|
-
const scope = m[1]
|
|
86
|
-
.split(/\s+/)
|
|
87
|
-
.map((c) => (c.startsWith('hljs-') ? c.slice(5) : null))
|
|
88
|
-
.find(Boolean);
|
|
89
|
-
stack.push(SCOPE_KINDS[scope] ?? stack[stack.length - 1]);
|
|
90
|
-
} else if (m[0] === '</span>') {
|
|
91
|
-
if (stack.length > 1) stack.pop();
|
|
92
|
-
} else {
|
|
93
|
-
emit(unescapeHtml(m[0]), stack[stack.length - 1]);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return tokens;
|
|
97
|
-
}
|