@hanzogui/code-to-html 8.3.0 → 8.3.2
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/dist/cjs/highlightLine.js +96 -0
- package/dist/cjs/highlightLine.js.map +1 -0
- package/dist/cjs/highlightWord.js +18 -0
- package/dist/cjs/highlightWord.js.map +1 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/highlightLine.js +92 -0
- package/dist/esm/highlightLine.js.map +1 -0
- package/dist/esm/highlightWord.js +14 -0
- package/dist/esm/highlightWord.js.map +1 -0
- package/dist/esm/index.js +14 -15
- package/dist/esm/index.js.map +1 -1
- package/package.json +26 -13
- package/src/index.ts +4 -4
- package/dist/cjs/highlightLine.cjs +0 -130
- package/dist/cjs/highlightLine.native.js +0 -138
- package/dist/cjs/highlightLine.native.js.map +0 -1
- package/dist/cjs/highlightWord.cjs +0 -51
- package/dist/cjs/highlightWord.native.js +0 -56
- package/dist/cjs/highlightWord.native.js.map +0 -1
- package/dist/cjs/index.cjs +0 -54
- package/dist/cjs/index.native.js +0 -58
- package/dist/cjs/index.native.js.map +0 -1
- package/dist/esm/highlightLine.mjs +0 -94
- package/dist/esm/highlightLine.mjs.map +0 -1
- package/dist/esm/highlightLine.native.js +0 -99
- package/dist/esm/highlightLine.native.js.map +0 -1
- package/dist/esm/highlightWord.mjs +0 -15
- package/dist/esm/highlightWord.mjs.map +0 -1
- package/dist/esm/highlightWord.native.js +0 -17
- package/dist/esm/highlightWord.native.js.map +0 -1
- package/dist/esm/index.mjs +0 -18
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/index.native.js +0 -19
- package/dist/esm/index.native.js.map +0 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Inspired by https://github.com/j0lv3r4/mdx-prism
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.highlightLine = highlightLine;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const hast_util_to_html_1 = require("hast-util-to-html");
|
|
7
|
+
const rehype_parse_1 = tslib_1.__importDefault(require("rehype-parse"));
|
|
8
|
+
const unified_1 = require("unified");
|
|
9
|
+
const lineNumberify = function lineNumberify(ast, lineNum = 1) {
|
|
10
|
+
let lineNumber = lineNum;
|
|
11
|
+
return ast.reduce((result, node) => {
|
|
12
|
+
if (node.type === 'text') {
|
|
13
|
+
if (node.value.indexOf('\n') === -1) {
|
|
14
|
+
node.lineNumber = lineNumber;
|
|
15
|
+
result.nodes.push(node);
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
const lines = node.value.split('\n');
|
|
19
|
+
for (let i = 0; i < lines.length; i++) {
|
|
20
|
+
if (i !== 0)
|
|
21
|
+
++lineNumber;
|
|
22
|
+
if (i === lines.length - 1 && lines[i].length === 0)
|
|
23
|
+
continue;
|
|
24
|
+
result.nodes.push({
|
|
25
|
+
type: 'text',
|
|
26
|
+
value: i === lines.length - 1 ? lines[i] : `${lines[i]}\n`,
|
|
27
|
+
lineNumber: lineNumber,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
result.lineNumber = lineNumber;
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
if (node.children) {
|
|
34
|
+
node.lineNumber = lineNumber;
|
|
35
|
+
const processed = lineNumberify(node.children, lineNumber);
|
|
36
|
+
node.children = processed.nodes;
|
|
37
|
+
result.lineNumber = processed.lineNumber;
|
|
38
|
+
result.nodes.push(node);
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
result.nodes.push(node);
|
|
42
|
+
return result;
|
|
43
|
+
}, { nodes: [], lineNumber: lineNumber });
|
|
44
|
+
};
|
|
45
|
+
const wrapLines = function wrapLines(ast, linesToHighlight) {
|
|
46
|
+
const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0;
|
|
47
|
+
const allLines = Array.from(new Set(ast.map((x) => x.lineNumber)));
|
|
48
|
+
let i = 0;
|
|
49
|
+
const wrapped = allLines.reduce((nodes, marker) => {
|
|
50
|
+
const line = marker;
|
|
51
|
+
const children = [];
|
|
52
|
+
for (; i < ast.length; i++) {
|
|
53
|
+
if (ast[i].lineNumber < line) {
|
|
54
|
+
nodes.push(ast[i]);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (ast[i].lineNumber === line) {
|
|
58
|
+
children.push(ast[i]);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (ast[i].lineNumber > line) {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
nodes.push({
|
|
66
|
+
type: 'element',
|
|
67
|
+
tagName: 'div',
|
|
68
|
+
properties: {
|
|
69
|
+
dataLine: line,
|
|
70
|
+
className: 'highlight-line',
|
|
71
|
+
dataHighlighted: linesToHighlight.includes(line) || highlightAll ? 'true' : 'false',
|
|
72
|
+
},
|
|
73
|
+
children,
|
|
74
|
+
lineNumber: line,
|
|
75
|
+
});
|
|
76
|
+
return nodes;
|
|
77
|
+
}, []);
|
|
78
|
+
return wrapped;
|
|
79
|
+
};
|
|
80
|
+
// https://github.com/gatsbyjs/gatsby/pull/26161/files
|
|
81
|
+
const MULTILINE_TOKEN_SPAN = /<span class="token ([^"]+)">[^<]*\n[^<]*<\/span>/g;
|
|
82
|
+
const applyMultilineFix = (ast) => {
|
|
83
|
+
// AST to HTML
|
|
84
|
+
let html = (0, hast_util_to_html_1.toHtml)(ast);
|
|
85
|
+
// Fix JSX issue
|
|
86
|
+
html = html.replace(MULTILINE_TOKEN_SPAN, (match, token) => match.replace(/\n/g, `</span>\n<span class="token ${token}">`));
|
|
87
|
+
// HTML to AST
|
|
88
|
+
const hast = (0, unified_1.unified)().use(rehype_parse_1.default, { emitParseErrors: true, fragment: true }).parse(html);
|
|
89
|
+
return hast['children'];
|
|
90
|
+
};
|
|
91
|
+
function highlightLine(ast, lines) {
|
|
92
|
+
const formattedAst = applyMultilineFix(ast);
|
|
93
|
+
const numbered = lineNumberify(formattedAst).nodes;
|
|
94
|
+
return wrapLines(numbered, lines);
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=highlightLine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlightLine.js","sourceRoot":"","sources":["../../src/highlightLine.ts"],"names":[],"mappings":";AAAA,mDAAmD;;;;AAEnD,yDAA0C;AAC1C,wEAAgC;AAChC,qCAAiC;AAEjC,MAAM,aAAa,GAAG,SAAS,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,CAAC;IAC3D,IAAI,UAAU,GAAG,OAAO,CAAA;IACxB,OAAO,GAAG,CAAC,MAAM,CACf,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;QACf,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YACzB,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;gBACpC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;gBAC5B,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACvB,OAAO,MAAM,CAAA;YACf,CAAC;YAED,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;gBACtC,IAAI,CAAC,KAAK,CAAC;oBAAE,EAAE,UAAU,CAAA;gBACzB,IAAI,CAAC,KAAK,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC;oBAAE,SAAQ;gBAC7D,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;oBAChB,IAAI,EAAE,MAAM;o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AAC,GAAG,CAAC,CAAA;IAEtB,gBAAgB;IAChB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,oBAAoB,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CACzD,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,+BAA+B,KAAK,IAAI,CAAC,CAC/D,CAAA;IAED,cAAc;IACd,MAAM,IAAI,GAAG,IAAA,iBAAO,GAAE,CAAC,GAAG,CAAC,sBAAK,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAExF,OAAO,IAAI,CAAC,UAAU,CAAC,CAAA;AACzB,CAAC,CAAA;AAED,uBAA8B,GAAG,EAAE,KAAK;IACtC,MAAM,YAAY,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAA;IAC3C,MAAM,QAAQ,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC,KAAK,CAAA;IAClD,OAAO,SAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;AACnC,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.highlightWord = highlightWord;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
// const visit = require('unist-util-visit')
|
|
6
|
+
const hast_util_to_html_1 = require("hast-util-to-html");
|
|
7
|
+
const rehype_parse_1 = tslib_1.__importDefault(require("rehype-parse"));
|
|
8
|
+
const unified_1 = require("unified");
|
|
9
|
+
const CALLOUT = /__(.*?)__/g;
|
|
10
|
+
function highlightWord(code) {
|
|
11
|
+
const html = (0, hast_util_to_html_1.toHtml)(code);
|
|
12
|
+
const result = html.replace(CALLOUT, (_, text) => `<span class="highlight-word">${text}</span>`);
|
|
13
|
+
const hast = (0, unified_1.unified)()
|
|
14
|
+
.use(rehype_parse_1.default, { emitParseErrors: true, fragment: true })
|
|
15
|
+
.parse(result);
|
|
16
|
+
return hast['children'];
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=highlightWord.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlightWord.js","sourceRoot":"","sources":["../../src/highlightWord.ts"],"names":[],"mappings":";;;;AAAA,4CAA4C;AAC5C,yDAA0C;AAC1C,wEAAgC;AAChC,qCAAiC;AAEjC,MAAM,OAAO,GAAG,YAAY,CAAA;AAE5B,uBAA8B,IAAI;IAChC,MAAM,IAAI,GAAG,IAAA,0BAAM,EAAC,IAAI,CAAC,CAAA;IACzB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CACzB,OAAO,EACP,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,gCAAgC,IAAI,SAAS,CAC3D,CAAA;IACD,MAAM,IAAI,GAAG,IAAA,iBAAO,GAAE;SACnB,GAAG,CAAC,sBAAK,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;SACrD,KAAK,CAAC,MAAM,CAAC,CAAA;IAChB,OAAO,IAAI,CAAC,UAAU,CAAC,CAAA;AACzB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.codeToHTML = codeToHTML;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const hast_util_to_html_1 = require("hast-util-to-html");
|
|
6
|
+
const parse_numeric_range_1 = tslib_1.__importDefault(require("parse-numeric-range"));
|
|
7
|
+
const refractor_1 = require("refractor");
|
|
8
|
+
const css_1 = tslib_1.__importDefault(require("refractor/css"));
|
|
9
|
+
const tsx_1 = tslib_1.__importDefault(require("refractor/tsx"));
|
|
10
|
+
const highlightLine_ts_1 = require("./highlightLine.js");
|
|
11
|
+
const highlightWord_ts_1 = require("./highlightWord.js");
|
|
12
|
+
refractor_1.refractor.register(tsx_1.default);
|
|
13
|
+
refractor_1.refractor.register(css_1.default);
|
|
14
|
+
function codeToHTML(source, language, line = '0') {
|
|
15
|
+
let result = refractor_1.refractor.highlight(source, language);
|
|
16
|
+
result = (0, highlightLine_ts_1.highlightLine)(result, (0, parse_numeric_range_1.default)(line));
|
|
17
|
+
result = (0, highlightWord_ts_1.highlightWord)(result);
|
|
18
|
+
result = (0, hast_util_to_html_1.toHtml)(result);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;AAAA,yDAA0C;AAC1C,sFAA6C;AAC7C,yCAAqC;AACrC,gEAA+B;AAC/B,gEAA+B;AAE/B,yDAAkD;AAClD,yDAAkD;AAElD,qBAAS,CAAC,QAAQ,CAAC,aAAG,CAAC,CAAA;AACvB,qBAAS,CAAC,QAAQ,CAAC,aAAG,CAAC,CAAA;AAEvB,oBAA2B,MAAc,EAAE,QAAgC,EAAE,IAAI,GAAG,GAAG;IACrF,IAAI,MAAM,GAAQ,qBAAS,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IACvD,MAAM,GAAG,IAAA,gCAAa,EAAC,MAAM,EAAE,IAAA,6BAAW,EAAC,IAAI,CAAC,CAAC,CAAA;IACjD,MAAM,GAAG,IAAA,gCAAa,EAAC,MAAM,CAAC,CAAA;IAC9B,MAAM,GAAG,IAAA,0BAAM,EAAC,MAAM,CAAC,CAAA;IACvB,OAAO,MAAgB,CAAA;AACzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Inspired by https://github.com/j0lv3r4/mdx-prism
|
|
2
|
+
import { toHtml } from 'hast-util-to-html';
|
|
3
|
+
import parse from 'rehype-parse';
|
|
4
|
+
import { unified } from 'unified';
|
|
5
|
+
const lineNumberify = function lineNumberify(ast, lineNum = 1) {
|
|
6
|
+
let lineNumber = lineNum;
|
|
7
|
+
return ast.reduce((result, node) => {
|
|
8
|
+
if (node.type === 'text') {
|
|
9
|
+
if (node.value.indexOf('\n') === -1) {
|
|
10
|
+
node.lineNumber = lineNumber;
|
|
11
|
+
result.nodes.push(node);
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
const lines = node.value.split('\n');
|
|
15
|
+
for (let i = 0; i < lines.length; i++) {
|
|
16
|
+
if (i !== 0)
|
|
17
|
+
++lineNumber;
|
|
18
|
+
if (i === lines.length - 1 && lines[i].length === 0)
|
|
19
|
+
continue;
|
|
20
|
+
result.nodes.push({
|
|
21
|
+
type: 'text',
|
|
22
|
+
value: i === lines.length - 1 ? lines[i] : `${lines[i]}\n`,
|
|
23
|
+
lineNumber: lineNumber,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
result.lineNumber = lineNumber;
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
if (node.children) {
|
|
30
|
+
node.lineNumber = lineNumber;
|
|
31
|
+
const processed = lineNumberify(node.children, lineNumber);
|
|
32
|
+
node.children = processed.nodes;
|
|
33
|
+
result.lineNumber = processed.lineNumber;
|
|
34
|
+
result.nodes.push(node);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
result.nodes.push(node);
|
|
38
|
+
return result;
|
|
39
|
+
}, { nodes: [], lineNumber: lineNumber });
|
|
40
|
+
};
|
|
41
|
+
const wrapLines = function wrapLines(ast, linesToHighlight) {
|
|
42
|
+
const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0;
|
|
43
|
+
const allLines = Array.from(new Set(ast.map((x) => x.lineNumber)));
|
|
44
|
+
let i = 0;
|
|
45
|
+
const wrapped = allLines.reduce((nodes, marker) => {
|
|
46
|
+
const line = marker;
|
|
47
|
+
const children = [];
|
|
48
|
+
for (; i < ast.length; i++) {
|
|
49
|
+
if (ast[i].lineNumber < line) {
|
|
50
|
+
nodes.push(ast[i]);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (ast[i].lineNumber === line) {
|
|
54
|
+
children.push(ast[i]);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (ast[i].lineNumber > line) {
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
nodes.push({
|
|
62
|
+
type: 'element',
|
|
63
|
+
tagName: 'div',
|
|
64
|
+
properties: {
|
|
65
|
+
dataLine: line,
|
|
66
|
+
className: 'highlight-line',
|
|
67
|
+
dataHighlighted: linesToHighlight.includes(line) || highlightAll ? 'true' : 'false',
|
|
68
|
+
},
|
|
69
|
+
children,
|
|
70
|
+
lineNumber: line,
|
|
71
|
+
});
|
|
72
|
+
return nodes;
|
|
73
|
+
}, []);
|
|
74
|
+
return wrapped;
|
|
75
|
+
};
|
|
76
|
+
// https://github.com/gatsbyjs/gatsby/pull/26161/files
|
|
77
|
+
const MULTILINE_TOKEN_SPAN = /<span class="token ([^"]+)">[^<]*\n[^<]*<\/span>/g;
|
|
78
|
+
const applyMultilineFix = (ast) => {
|
|
79
|
+
// AST to HTML
|
|
80
|
+
let html = toHtml(ast);
|
|
81
|
+
// Fix JSX issue
|
|
82
|
+
html = html.replace(MULTILINE_TOKEN_SPAN, (match, token) => match.replace(/\n/g, `</span>\n<span class="token ${token}">`));
|
|
83
|
+
// HTML to AST
|
|
84
|
+
const hast = unified().use(parse, { emitParseErrors: true, fragment: true }).parse(html);
|
|
85
|
+
return hast['children'];
|
|
86
|
+
};
|
|
87
|
+
export function highlightLine(ast, lines) {
|
|
88
|
+
const formattedAst = applyMultilineFix(ast);
|
|
89
|
+
const numbered = lineNumberify(formattedAst).nodes;
|
|
90
|
+
return wrapLines(numbered, lines);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=highlightLine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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E,CACzD,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,+BAA+B,KAAK,IAAI,CAAC,CAC/D,CAAA;IAED,cAAc;IACd,MAAM,IAAI,GAAG,OAAO,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAExF,OAAO,IAAI,CAAC,UAAU,CAAC,CAAA;AACzB,CAAC,CAAA;AAED,MAAM,UAAU,aAAa,CAAC,GAAG,EAAE,KAAK;IACtC,MAAM,YAAY,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAA;IAC3C,MAAM,QAAQ,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC,KAAK,CAAA;IAClD,OAAO,SAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;AACnC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// const visit = require('unist-util-visit')
|
|
2
|
+
import { toHtml } from 'hast-util-to-html';
|
|
3
|
+
import parse from 'rehype-parse';
|
|
4
|
+
import { unified } from 'unified';
|
|
5
|
+
const CALLOUT = /__(.*?)__/g;
|
|
6
|
+
export function highlightWord(code) {
|
|
7
|
+
const html = toHtml(code);
|
|
8
|
+
const result = html.replace(CALLOUT, (_, text) => `<span class="highlight-word">${text}</span>`);
|
|
9
|
+
const hast = unified()
|
|
10
|
+
.use(parse, { emitParseErrors: true, fragment: true })
|
|
11
|
+
.parse(result);
|
|
12
|
+
return hast['children'];
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=highlightWord.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlightWord.js","sourceRoot":"","sources":["../../src/highlightWord.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,KAAK,MAAM,cAAc,CAAA;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEjC,MAAM,OAAO,GAAG,YAAY,CAAA;AAE5B,MAAM,UAAU,aAAa,CAAC,IAAI;IAChC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,CAAA;IACzB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CACzB,OAAO,EACP,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,gCAAgC,IAAI,SAAS,CAC3D,CAAA;IACD,MAAM,IAAI,GAAG,OAAO,EAAE;SACnB,GAAG,CAAC,KAAK,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;SACrD,KAAK,CAAC,MAAM,CAAC,CAAA;IAChB,OAAO,IAAI,CAAC,UAAU,CAAC,CAAA;AACzB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { toHtml } from
|
|
2
|
-
import rangeParser from
|
|
3
|
-
import { refractor } from
|
|
4
|
-
import css from
|
|
5
|
-
import tsx from
|
|
6
|
-
import { highlightLine } from
|
|
7
|
-
import { highlightWord } from
|
|
1
|
+
import { toHtml } from 'hast-util-to-html';
|
|
2
|
+
import rangeParser from 'parse-numeric-range';
|
|
3
|
+
import { refractor } from 'refractor';
|
|
4
|
+
import css from 'refractor/css';
|
|
5
|
+
import tsx from 'refractor/tsx';
|
|
6
|
+
import { highlightLine } from './highlightLine.js';
|
|
7
|
+
import { highlightWord } from './highlightWord.js';
|
|
8
8
|
refractor.register(tsx);
|
|
9
9
|
refractor.register(css);
|
|
10
|
-
function codeToHTML(source, language, line =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
export function codeToHTML(source, language, line = '0') {
|
|
11
|
+
let result = refractor.highlight(source, language);
|
|
12
|
+
result = highlightLine(result, rangeParser(line));
|
|
13
|
+
result = highlightWord(result);
|
|
14
|
+
result = toHtml(result);
|
|
15
|
+
return result;
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,WAAW,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACrC,OAAO,GAAG,MAAM,eAAe,CAAA;AAC/B,OAAO,GAAG,MAAM,eAAe,CAAA;AAE/B,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AACvB,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAEvB,MAAM,UAAU,UAAU,CAAC,MAAc,EAAE,QAAgC,EAAE,IAAI,GAAG,GAAG;IACrF,IAAI,MAAM,GAAQ,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IACvD,MAAM,GAAG,aAAa,CAAC,MAAM,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC,CAAA;IACjD,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA;IAC9B,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;IACvB,OAAO,MAAgB,CAAA;AACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,37 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzogui/code-to-html",
|
|
3
|
-
"version": "8.3.
|
|
3
|
+
"version": "8.3.2",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"files": [
|
|
6
6
|
"src",
|
|
7
7
|
"types",
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/cjs/index.js",
|
|
12
|
+
"module": "./dist/esm/index.js",
|
|
12
13
|
"types": "./types/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
"./package.json": "./package.json",
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./types/index.d.ts",
|
|
18
|
+
"react-native": "./dist/esm/index.js",
|
|
19
|
+
"browser": "./dist/esm/index.js",
|
|
20
|
+
"module": "./dist/esm/index.js",
|
|
21
|
+
"import": "./dist/esm/index.js",
|
|
22
|
+
"require": "./dist/cjs/index.js",
|
|
23
|
+
"default": "./dist/esm/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
13
26
|
"publishConfig": {
|
|
14
27
|
"access": "public"
|
|
15
28
|
},
|
|
16
29
|
"scripts": {
|
|
17
|
-
"build": "hanzogui-build",
|
|
30
|
+
"build": "hanzogui-build --skip-native",
|
|
18
31
|
"watch": "hanzogui-build --watch",
|
|
19
32
|
"clean": "hanzogui-build clean",
|
|
20
33
|
"clean:build": "hanzogui-build clean:build"
|
|
21
34
|
},
|
|
22
35
|
"dependencies": {
|
|
23
|
-
"hast-util-to-html": "^
|
|
24
|
-
"hast-util-to-string": "^
|
|
36
|
+
"hast-util-to-html": "^9.0.5",
|
|
37
|
+
"hast-util-to-string": "^3.0.1",
|
|
25
38
|
"parse-numeric-range": "^1.3.0",
|
|
26
|
-
"refractor": "^
|
|
27
|
-
"rehype": "^
|
|
28
|
-
"rehype-parse": "^
|
|
29
|
-
"rehype-slug": "^
|
|
30
|
-
"unified": "^
|
|
31
|
-
"url": "^0.11.
|
|
39
|
+
"refractor": "^5.0.0",
|
|
40
|
+
"rehype": "^13.0.2",
|
|
41
|
+
"rehype-parse": "^9.0.1",
|
|
42
|
+
"rehype-slug": "^6.0.0",
|
|
43
|
+
"unified": "^11.0.5",
|
|
44
|
+
"url": "^0.11.4"
|
|
32
45
|
},
|
|
33
46
|
"devDependencies": {
|
|
34
|
-
"@hanzogui/build": "8.3.
|
|
47
|
+
"@hanzogui/build": "8.3.2"
|
|
35
48
|
},
|
|
36
49
|
"repository": {
|
|
37
50
|
"type": "git",
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { toHtml } from 'hast-util-to-html'
|
|
2
2
|
import rangeParser from 'parse-numeric-range'
|
|
3
3
|
import { refractor } from 'refractor'
|
|
4
|
-
import css from 'refractor/
|
|
5
|
-
import tsx from 'refractor/
|
|
4
|
+
import css from 'refractor/css'
|
|
5
|
+
import tsx from 'refractor/tsx'
|
|
6
6
|
|
|
7
|
-
import { highlightLine } from './highlightLine'
|
|
8
|
-
import { highlightWord } from './highlightWord'
|
|
7
|
+
import { highlightLine } from './highlightLine.ts'
|
|
8
|
+
import { highlightWord } from './highlightWord.ts'
|
|
9
9
|
|
|
10
10
|
refractor.register(tsx)
|
|
11
11
|
refractor.register(css)
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all) __defProp(target, name, {
|
|
9
|
-
get: all[name],
|
|
10
|
-
enumerable: true
|
|
11
|
-
});
|
|
12
|
-
};
|
|
13
|
-
var __copyProps = (to, from, except, desc) => {
|
|
14
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
-
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
-
get: () => from[key],
|
|
17
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
-
value: mod,
|
|
29
|
-
enumerable: true
|
|
30
|
-
}) : target, mod));
|
|
31
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
-
value: true
|
|
33
|
-
}), mod);
|
|
34
|
-
var highlightLine_exports = {};
|
|
35
|
-
__export(highlightLine_exports, {
|
|
36
|
-
highlightLine: () => highlightLine
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(highlightLine_exports);
|
|
39
|
-
var import_hast_util_to_html = require("hast-util-to-html");
|
|
40
|
-
var import_rehype_parse = __toESM(require("rehype-parse"));
|
|
41
|
-
var import_unified = require("unified");
|
|
42
|
-
const lineNumberify = function lineNumberify2(ast, lineNum = 1) {
|
|
43
|
-
let lineNumber = lineNum;
|
|
44
|
-
return ast.reduce((result, node) => {
|
|
45
|
-
if (node.type === "text") {
|
|
46
|
-
if (node.value.indexOf("\n") === -1) {
|
|
47
|
-
node.lineNumber = lineNumber;
|
|
48
|
-
result.nodes.push(node);
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
const lines = node.value.split("\n");
|
|
52
|
-
for (let i = 0; i < lines.length; i++) {
|
|
53
|
-
if (i !== 0) ++lineNumber;
|
|
54
|
-
if (i === lines.length - 1 && lines[i].length === 0) continue;
|
|
55
|
-
result.nodes.push({
|
|
56
|
-
type: "text",
|
|
57
|
-
value: i === lines.length - 1 ? lines[i] : `${lines[i]}
|
|
58
|
-
`,
|
|
59
|
-
lineNumber
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
result.lineNumber = lineNumber;
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
if (node.children) {
|
|
66
|
-
node.lineNumber = lineNumber;
|
|
67
|
-
const processed = lineNumberify2(node.children, lineNumber);
|
|
68
|
-
node.children = processed.nodes;
|
|
69
|
-
result.lineNumber = processed.lineNumber;
|
|
70
|
-
result.nodes.push(node);
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
result.nodes.push(node);
|
|
74
|
-
return result;
|
|
75
|
-
}, {
|
|
76
|
-
nodes: [],
|
|
77
|
-
lineNumber
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
const wrapLines = function wrapLines2(ast, linesToHighlight) {
|
|
81
|
-
const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0;
|
|
82
|
-
const allLines = Array.from(new Set(ast.map(x => x.lineNumber)));
|
|
83
|
-
let i = 0;
|
|
84
|
-
const wrapped = allLines.reduce((nodes, marker) => {
|
|
85
|
-
const line = marker;
|
|
86
|
-
const children = [];
|
|
87
|
-
for (; i < ast.length; i++) {
|
|
88
|
-
if (ast[i].lineNumber < line) {
|
|
89
|
-
nodes.push(ast[i]);
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (ast[i].lineNumber === line) {
|
|
93
|
-
children.push(ast[i]);
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
if (ast[i].lineNumber > line) {
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
nodes.push({
|
|
101
|
-
type: "element",
|
|
102
|
-
tagName: "div",
|
|
103
|
-
properties: {
|
|
104
|
-
dataLine: line,
|
|
105
|
-
className: "highlight-line",
|
|
106
|
-
dataHighlighted: linesToHighlight.includes(line) || highlightAll ? "true" : "false"
|
|
107
|
-
},
|
|
108
|
-
children,
|
|
109
|
-
lineNumber: line
|
|
110
|
-
});
|
|
111
|
-
return nodes;
|
|
112
|
-
}, []);
|
|
113
|
-
return wrapped;
|
|
114
|
-
};
|
|
115
|
-
const MULTILINE_TOKEN_SPAN = /<span class="token ([^"]+)">[^<]*\n[^<]*<\/span>/g;
|
|
116
|
-
const applyMultilineFix = ast => {
|
|
117
|
-
let html = (0, import_hast_util_to_html.toHtml)(ast);
|
|
118
|
-
html = html.replace(MULTILINE_TOKEN_SPAN, (match, token) => match.replace(/\n/g, `</span>
|
|
119
|
-
<span class="token ${token}">`));
|
|
120
|
-
const hast = (0, import_unified.unified)().use(import_rehype_parse.default, {
|
|
121
|
-
emitParseErrors: true,
|
|
122
|
-
fragment: true
|
|
123
|
-
}).parse(html);
|
|
124
|
-
return hast["children"];
|
|
125
|
-
};
|
|
126
|
-
function highlightLine(ast, lines) {
|
|
127
|
-
const formattedAst = applyMultilineFix(ast);
|
|
128
|
-
const numbered = lineNumberify(formattedAst).nodes;
|
|
129
|
-
return wrapLines(numbered, lines);
|
|
130
|
-
}
|