@tamagui/code-to-html 1.14.0 → 1.14.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 +133 -5
- package/dist/cjs/highlightLine.js.map +2 -2
- package/dist/cjs/highlightWord.js +52 -1
- package/dist/cjs/highlightWord.js.map +2 -2
- package/dist/cjs/index.js +166 -5
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/highlightLine.js +10337 -16
- package/dist/esm/highlightLine.js.map +2 -2
- package/dist/esm/highlightLine.mjs +10337 -16
- package/dist/esm/highlightLine.mjs.map +2 -2
- package/dist/esm/highlightWord.js +10256 -12
- package/dist/esm/highlightWord.js.map +2 -2
- package/dist/esm/highlightWord.mjs +10256 -12
- package/dist/esm/highlightWord.mjs.map +2 -2
- package/dist/esm/index.js +17502 -19
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/index.mjs +17502 -19
- package/dist/esm/index.mjs.map +2 -2
- package/package.json +2 -2
|
@@ -1,6 +1,134 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/highlightLine.ts
|
|
31
|
+
var highlightLine_exports = {};
|
|
32
|
+
__export(highlightLine_exports, {
|
|
33
|
+
highlightLine: () => highlightLine
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(highlightLine_exports);
|
|
36
|
+
var import_hast_util_to_html = require("hast-util-to-html");
|
|
37
|
+
var import_rehype_parse = __toESM(require("rehype-parse"));
|
|
38
|
+
var import_unified = require("unified");
|
|
39
|
+
var lineNumberify = function lineNumberify2(ast, lineNum = 1) {
|
|
40
|
+
let lineNumber = lineNum;
|
|
41
|
+
return ast.reduce(
|
|
42
|
+
(result, node) => {
|
|
43
|
+
if (node.type === "text") {
|
|
44
|
+
if (node.value.indexOf("\n") === -1) {
|
|
45
|
+
node.lineNumber = lineNumber;
|
|
46
|
+
result.nodes.push(node);
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
const lines = node.value.split("\n");
|
|
50
|
+
for (let i = 0; i < lines.length; i++) {
|
|
51
|
+
if (i !== 0)
|
|
52
|
+
++lineNumber;
|
|
53
|
+
if (i === lines.length - 1 && lines[i].length === 0)
|
|
54
|
+
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: [], lineNumber }
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
var wrapLines = function wrapLines2(ast, linesToHighlight) {
|
|
80
|
+
const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0;
|
|
81
|
+
const allLines = Array.from(new Set(ast.map((x) => x.lineNumber)));
|
|
82
|
+
let i = 0;
|
|
83
|
+
const wrapped = allLines.reduce((nodes, marker) => {
|
|
84
|
+
const line = marker;
|
|
85
|
+
const children = [];
|
|
86
|
+
for (; i < ast.length; i++) {
|
|
87
|
+
if (ast[i].lineNumber < line) {
|
|
88
|
+
nodes.push(ast[i]);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (ast[i].lineNumber === line) {
|
|
92
|
+
children.push(ast[i]);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (ast[i].lineNumber > line) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
nodes.push({
|
|
100
|
+
type: "element",
|
|
101
|
+
tagName: "div",
|
|
102
|
+
properties: {
|
|
103
|
+
dataLine: line,
|
|
104
|
+
className: "highlight-line",
|
|
105
|
+
dataHighlighted: linesToHighlight.includes(line) || highlightAll ? "true" : "false"
|
|
106
|
+
},
|
|
107
|
+
children,
|
|
108
|
+
lineNumber: line
|
|
109
|
+
});
|
|
110
|
+
return nodes;
|
|
111
|
+
}, []);
|
|
112
|
+
return wrapped;
|
|
113
|
+
};
|
|
114
|
+
var MULTILINE_TOKEN_SPAN = /<span class="token ([^"]+)">[^<]*\n[^<]*<\/span>/g;
|
|
115
|
+
var applyMultilineFix = function(ast) {
|
|
116
|
+
let html = (0, import_hast_util_to_html.toHtml)(ast);
|
|
117
|
+
html = html.replace(
|
|
118
|
+
MULTILINE_TOKEN_SPAN,
|
|
119
|
+
(match, token) => match.replace(/\n/g, `</span>
|
|
120
|
+
<span class="token ${token}">`)
|
|
121
|
+
);
|
|
122
|
+
const hast = (0, import_unified.unified)().use(import_rehype_parse.default, { emitParseErrors: true, fragment: true }).parse(html);
|
|
123
|
+
return hast.children;
|
|
124
|
+
};
|
|
125
|
+
function highlightLine(ast, lines) {
|
|
126
|
+
const formattedAst = applyMultilineFix(ast);
|
|
127
|
+
const numbered = lineNumberify(formattedAst).nodes;
|
|
128
|
+
return wrapLines(numbered, lines);
|
|
129
|
+
}
|
|
130
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
131
|
+
0 && (module.exports = {
|
|
132
|
+
highlightLine
|
|
133
|
+
});
|
|
6
134
|
//# sourceMappingURL=highlightLine.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/highlightLine.ts"],
|
|
4
4
|
"sourcesContent": ["// Inspired by https://github.com/j0lv3r4/mdx-prism\n\nimport { toHtml } from 'hast-util-to-html'\nimport parse from 'rehype-parse'\nimport { unified } from 'unified'\n\nconst lineNumberify = function lineNumberify(ast, lineNum = 1) {\n let lineNumber = lineNum\n return ast.reduce(\n (result, node) => {\n if (node.type === 'text') {\n if (node.value.indexOf('\\n') === -1) {\n node.lineNumber = lineNumber\n result.nodes.push(node)\n return result\n }\n\n const lines = node.value.split('\\n')\n for (let i = 0; i < lines.length; i++) {\n if (i !== 0) ++lineNumber\n if (i === lines.length - 1 && lines[i].length === 0) continue\n result.nodes.push({\n type: 'text',\n value: i === lines.length - 1 ? lines[i] : `${lines[i]}\\n`,\n lineNumber: lineNumber,\n })\n }\n\n result.lineNumber = lineNumber\n return result\n }\n\n if (node.children) {\n node.lineNumber = lineNumber\n const processed = lineNumberify(node.children, lineNumber)\n node.children = processed.nodes\n result.lineNumber = processed.lineNumber\n result.nodes.push(node)\n return result\n }\n\n result.nodes.push(node)\n return result\n },\n { nodes: [], lineNumber: lineNumber },\n )\n}\n\nconst wrapLines = function wrapLines(ast: any[], linesToHighlight) {\n const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0\n const allLines: any[] = Array.from(new Set(ast.map((x) => x.lineNumber)))\n let i = 0\n const wrapped = allLines.reduce((nodes, marker) => {\n const line = marker\n const children: any[] = []\n for (; i < ast.length; i++) {\n if (ast[i].lineNumber < line) {\n nodes.push(ast[i])\n continue\n }\n\n if (ast[i].lineNumber === line) {\n children.push(ast[i])\n continue\n }\n\n if (ast[i].lineNumber > line) {\n break\n }\n }\n\n nodes.push({\n type: 'element',\n tagName: 'div',\n properties: {\n dataLine: line,\n className: 'highlight-line',\n dataHighlighted:\n linesToHighlight.includes(line) || highlightAll ? 'true' : 'false',\n },\n children,\n lineNumber: line,\n })\n\n return nodes\n }, [])\n\n return wrapped\n}\n\n// https://github.com/gatsbyjs/gatsby/pull/26161/files\nconst MULTILINE_TOKEN_SPAN = /<span class=\"token ([^\"]+)\">[^<]*\\n[^<]*<\\/span>/g\n\nconst applyMultilineFix = function (ast) {\n // AST to HTML\n let html = toHtml(ast)\n\n // Fix JSX issue\n html = html.replace(MULTILINE_TOKEN_SPAN, (match, token) =>\n match.replace(/\\n/g, `</span>\\n<span class=\"token ${token}\">`),\n )\n\n // HTML to AST\n const hast = unified()\n .use(parse, { emitParseErrors: true, fragment: true })\n .parse(html)\n\n return hast.children\n}\n\nexport function highlightLine(ast, lines) {\n const formattedAst = applyMultilineFix(ast)\n const numbered = lineNumberify(formattedAst).nodes\n return wrapLines(numbered, lines)\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,+BAAuB;AACvB,0BAAkB;AAClB,qBAAwB;AAExB,IAAM,gBAAgB,SAASA,eAAc,KAAK,UAAU,GAAG;AAC7D,MAAI,aAAa;AACjB,SAAO,IAAI;AAAA,IACT,CAAC,QAAQ,SAAS;AAChB,UAAI,KAAK,SAAS,QAAQ;AACxB,YAAI,KAAK,MAAM,QAAQ,IAAI,MAAM,IAAI;AACnC,eAAK,aAAa;AAClB,iBAAO,MAAM,KAAK,IAAI;AACtB,iBAAO;AAAA,QACT;AAEA,cAAM,QAAQ,KAAK,MAAM,MAAM,IAAI;AACnC,iBAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAI,MAAM;AAAG,cAAE;AACf,cAAI,MAAM,MAAM,SAAS,KAAK,MAAM,CAAC,EAAE,WAAW;AAAG;AACrD,iBAAO,MAAM,KAAK;AAAA,YAChB,MAAM;AAAA,YACN,OAAO,MAAM,MAAM,SAAS,IAAI,MAAM,CAAC,IAAI,GAAG,MAAM,CAAC;AAAA;AAAA,YACrD;AAAA,UACF,CAAC;AAAA,QACH;AAEA,eAAO,aAAa;AACpB,eAAO;AAAA,MACT;AAEA,UAAI,KAAK,UAAU;AACjB,aAAK,aAAa;AAClB,cAAM,YAAYA,eAAc,KAAK,UAAU,UAAU;AACzD,aAAK,WAAW,UAAU;AAC1B,eAAO,aAAa,UAAU;AAC9B,eAAO,MAAM,KAAK,IAAI;AACtB,eAAO;AAAA,MACT;AAEA,aAAO,MAAM,KAAK,IAAI;AACtB,aAAO;AAAA,IACT;AAAA,IACA,EAAE,OAAO,CAAC,GAAG,WAAuB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AAEO,SAAS,cAAc,KAAK,OAAO;AACxC,QAAM,eAAe,kBAAkB,GAAG;AAC1C,QAAM,WAAW,cAAc,YAAY,EAAE;AAC7C,SAAO,UAAU,UAAU,KAAK;AAClC;",
|
|
6
|
+
"names": ["lineNumberify", "wrapLines", "parse"]
|
|
7
7
|
}
|
|
@@ -1,2 +1,53 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/highlightWord.ts
|
|
31
|
+
var highlightWord_exports = {};
|
|
32
|
+
__export(highlightWord_exports, {
|
|
33
|
+
highlightWord: () => highlightWord
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(highlightWord_exports);
|
|
36
|
+
var import_hast_util_to_html = require("hast-util-to-html");
|
|
37
|
+
var import_rehype_parse = __toESM(require("rehype-parse"));
|
|
38
|
+
var import_unified = require("unified");
|
|
39
|
+
var CALLOUT = /__(.*?)__/g;
|
|
40
|
+
function highlightWord(code) {
|
|
41
|
+
const html = (0, import_hast_util_to_html.toHtml)(code);
|
|
42
|
+
const result = html.replace(
|
|
43
|
+
CALLOUT,
|
|
44
|
+
(_, text) => `<span class="highlight-word">${text}</span>`
|
|
45
|
+
);
|
|
46
|
+
const hast = (0, import_unified.unified)().use(import_rehype_parse.default, { emitParseErrors: true, fragment: true }).parse(result);
|
|
47
|
+
return hast.children;
|
|
48
|
+
}
|
|
49
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
50
|
+
0 && (module.exports = {
|
|
51
|
+
highlightWord
|
|
52
|
+
});
|
|
2
53
|
//# sourceMappingURL=highlightWord.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/highlightWord.ts"],
|
|
4
4
|
"sourcesContent": ["// const visit = require('unist-util-visit')\nimport { toHtml } from 'hast-util-to-html'\nimport parse from 'rehype-parse'\nimport { unified } from 'unified'\n\nconst CALLOUT = /__(.*?)__/g\n\nexport function highlightWord(code) {\n const html = toHtml(code)\n const result = html.replace(\n CALLOUT,\n (_, text) => `<span class=\"highlight-word\">${text}</span>`,\n )\n const hast = unified()\n .use(parse, { emitParseErrors: true, fragment: true })\n .parse(result)\n return hast.children\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,+BAAuB;AACvB,0BAAkB;AAClB,qBAAwB;AAExB,IAAM,UAAU;AAET,SAAS,cAAc,MAAM;AAClC,QAAM,WAAO,iCAAO,IAAI;AACxB,QAAM,SAAS,KAAK;AAAA,IAClB;AAAA,IACA,CAAC,GAAG,SAAS,gCAAgC;AAAA,EAC/C;AACA,QAAM,WAAO,wBAAQ,EAClB,IAAI,oBAAAA,SAAO,EAAE,iBAAiB,MAAM,UAAU,KAAK,CAAC,EACpD,MAAM,MAAM;AACf,SAAO,KAAK;AACd;",
|
|
6
|
+
"names": ["parse"]
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,167 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
codeToHTML: () => codeToHTML
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var import_hast_util_to_html3 = require("hast-util-to-html");
|
|
37
|
+
var import_parse_numeric_range = __toESM(require("parse-numeric-range"));
|
|
38
|
+
var import_refractor = require("refractor");
|
|
39
|
+
var import_css = __toESM(require("refractor/lang/css"));
|
|
40
|
+
var import_tsx = __toESM(require("refractor/lang/tsx"));
|
|
41
|
+
|
|
42
|
+
// src/highlightLine.ts
|
|
43
|
+
var import_hast_util_to_html = require("hast-util-to-html");
|
|
44
|
+
var import_rehype_parse = __toESM(require("rehype-parse"));
|
|
45
|
+
var import_unified = require("unified");
|
|
46
|
+
var lineNumberify = function lineNumberify2(ast, lineNum = 1) {
|
|
47
|
+
let lineNumber = lineNum;
|
|
48
|
+
return ast.reduce(
|
|
49
|
+
(result, node) => {
|
|
50
|
+
if (node.type === "text") {
|
|
51
|
+
if (node.value.indexOf("\n") === -1) {
|
|
52
|
+
node.lineNumber = lineNumber;
|
|
53
|
+
result.nodes.push(node);
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
const lines = node.value.split("\n");
|
|
57
|
+
for (let i = 0; i < lines.length; i++) {
|
|
58
|
+
if (i !== 0)
|
|
59
|
+
++lineNumber;
|
|
60
|
+
if (i === lines.length - 1 && lines[i].length === 0)
|
|
61
|
+
continue;
|
|
62
|
+
result.nodes.push({
|
|
63
|
+
type: "text",
|
|
64
|
+
value: i === lines.length - 1 ? lines[i] : `${lines[i]}
|
|
65
|
+
`,
|
|
66
|
+
lineNumber
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
result.lineNumber = lineNumber;
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
if (node.children) {
|
|
73
|
+
node.lineNumber = lineNumber;
|
|
74
|
+
const processed = lineNumberify2(node.children, lineNumber);
|
|
75
|
+
node.children = processed.nodes;
|
|
76
|
+
result.lineNumber = processed.lineNumber;
|
|
77
|
+
result.nodes.push(node);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
result.nodes.push(node);
|
|
81
|
+
return result;
|
|
82
|
+
},
|
|
83
|
+
{ nodes: [], lineNumber }
|
|
84
|
+
);
|
|
85
|
+
};
|
|
86
|
+
var wrapLines = function wrapLines2(ast, linesToHighlight) {
|
|
87
|
+
const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0;
|
|
88
|
+
const allLines = Array.from(new Set(ast.map((x) => x.lineNumber)));
|
|
89
|
+
let i = 0;
|
|
90
|
+
const wrapped = allLines.reduce((nodes, marker) => {
|
|
91
|
+
const line = marker;
|
|
92
|
+
const children = [];
|
|
93
|
+
for (; i < ast.length; i++) {
|
|
94
|
+
if (ast[i].lineNumber < line) {
|
|
95
|
+
nodes.push(ast[i]);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (ast[i].lineNumber === line) {
|
|
99
|
+
children.push(ast[i]);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (ast[i].lineNumber > line) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
nodes.push({
|
|
107
|
+
type: "element",
|
|
108
|
+
tagName: "div",
|
|
109
|
+
properties: {
|
|
110
|
+
dataLine: line,
|
|
111
|
+
className: "highlight-line",
|
|
112
|
+
dataHighlighted: linesToHighlight.includes(line) || highlightAll ? "true" : "false"
|
|
113
|
+
},
|
|
114
|
+
children,
|
|
115
|
+
lineNumber: line
|
|
116
|
+
});
|
|
117
|
+
return nodes;
|
|
118
|
+
}, []);
|
|
119
|
+
return wrapped;
|
|
120
|
+
};
|
|
121
|
+
var MULTILINE_TOKEN_SPAN = /<span class="token ([^"]+)">[^<]*\n[^<]*<\/span>/g;
|
|
122
|
+
var applyMultilineFix = function(ast) {
|
|
123
|
+
let html = (0, import_hast_util_to_html.toHtml)(ast);
|
|
124
|
+
html = html.replace(
|
|
125
|
+
MULTILINE_TOKEN_SPAN,
|
|
126
|
+
(match, token) => match.replace(/\n/g, `</span>
|
|
127
|
+
<span class="token ${token}">`)
|
|
128
|
+
);
|
|
129
|
+
const hast = (0, import_unified.unified)().use(import_rehype_parse.default, { emitParseErrors: true, fragment: true }).parse(html);
|
|
130
|
+
return hast.children;
|
|
131
|
+
};
|
|
132
|
+
function highlightLine(ast, lines) {
|
|
133
|
+
const formattedAst = applyMultilineFix(ast);
|
|
134
|
+
const numbered = lineNumberify(formattedAst).nodes;
|
|
135
|
+
return wrapLines(numbered, lines);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/highlightWord.ts
|
|
139
|
+
var import_hast_util_to_html2 = require("hast-util-to-html");
|
|
140
|
+
var import_rehype_parse2 = __toESM(require("rehype-parse"));
|
|
141
|
+
var import_unified2 = require("unified");
|
|
142
|
+
var CALLOUT = /__(.*?)__/g;
|
|
143
|
+
function highlightWord(code) {
|
|
144
|
+
const html = (0, import_hast_util_to_html2.toHtml)(code);
|
|
145
|
+
const result = html.replace(
|
|
146
|
+
CALLOUT,
|
|
147
|
+
(_, text) => `<span class="highlight-word">${text}</span>`
|
|
148
|
+
);
|
|
149
|
+
const hast = (0, import_unified2.unified)().use(import_rehype_parse2.default, { emitParseErrors: true, fragment: true }).parse(result);
|
|
150
|
+
return hast.children;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/index.ts
|
|
154
|
+
import_refractor.refractor.register(import_tsx.default);
|
|
155
|
+
import_refractor.refractor.register(import_css.default);
|
|
156
|
+
function codeToHTML(source, language, line = "0") {
|
|
157
|
+
let result = import_refractor.refractor.highlight(source, language);
|
|
158
|
+
result = highlightLine(result, (0, import_parse_numeric_range.default)(line));
|
|
159
|
+
result = highlightWord(result);
|
|
160
|
+
result = (0, import_hast_util_to_html3.toHtml)(result);
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
codeToHTML
|
|
166
|
+
});
|
|
6
167
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts", "../../src/highlightLine.ts", "../../src/highlightWord.ts"],
|
|
4
4
|
"sourcesContent": ["import { toHtml } from 'hast-util-to-html'\nimport rangeParser from 'parse-numeric-range'\nimport { refractor } from 'refractor'\nimport css from 'refractor/lang/css'\nimport tsx from 'refractor/lang/tsx'\n\nimport { highlightLine } from './highlightLine'\nimport { highlightWord } from './highlightWord'\n\nrefractor.register(tsx)\nrefractor.register(css)\n\nexport function codeToHTML(\n source: string,\n language: 'tsx' | 'css' | string,\n line = '0',\n) {\n let result: any = refractor.highlight(source, language)\n result = highlightLine(result, rangeParser(line))\n result = highlightWord(result)\n result = toHtml(result)\n return result as string\n}\n", "// Inspired by https://github.com/j0lv3r4/mdx-prism\n\nimport { toHtml } from 'hast-util-to-html'\nimport parse from 'rehype-parse'\nimport { unified } from 'unified'\n\nconst lineNumberify = function lineNumberify(ast, lineNum = 1) {\n let lineNumber = lineNum\n return ast.reduce(\n (result, node) => {\n if (node.type === 'text') {\n if (node.value.indexOf('\\n') === -1) {\n node.lineNumber = lineNumber\n result.nodes.push(node)\n return result\n }\n\n const lines = node.value.split('\\n')\n for (let i = 0; i < lines.length; i++) {\n if (i !== 0) ++lineNumber\n if (i === lines.length - 1 && lines[i].length === 0) continue\n result.nodes.push({\n type: 'text',\n value: i === lines.length - 1 ? lines[i] : `${lines[i]}\\n`,\n lineNumber: lineNumber,\n })\n }\n\n result.lineNumber = lineNumber\n return result\n }\n\n if (node.children) {\n node.lineNumber = lineNumber\n const processed = lineNumberify(node.children, lineNumber)\n node.children = processed.nodes\n result.lineNumber = processed.lineNumber\n result.nodes.push(node)\n return result\n }\n\n result.nodes.push(node)\n return result\n },\n { nodes: [], lineNumber: lineNumber },\n )\n}\n\nconst wrapLines = function wrapLines(ast: any[], linesToHighlight) {\n const highlightAll = linesToHighlight.length === 1 && linesToHighlight[0] === 0\n const allLines: any[] = Array.from(new Set(ast.map((x) => x.lineNumber)))\n let i = 0\n const wrapped = allLines.reduce((nodes, marker) => {\n const line = marker\n const children: any[] = []\n for (; i < ast.length; i++) {\n if (ast[i].lineNumber < line) {\n nodes.push(ast[i])\n continue\n }\n\n if (ast[i].lineNumber === line) {\n children.push(ast[i])\n continue\n }\n\n if (ast[i].lineNumber > line) {\n break\n }\n }\n\n nodes.push({\n type: 'element',\n tagName: 'div',\n properties: {\n dataLine: line,\n className: 'highlight-line',\n dataHighlighted:\n linesToHighlight.includes(line) || highlightAll ? 'true' : 'false',\n },\n children,\n lineNumber: line,\n })\n\n return nodes\n }, [])\n\n return wrapped\n}\n\n// https://github.com/gatsbyjs/gatsby/pull/26161/files\nconst MULTILINE_TOKEN_SPAN = /<span class=\"token ([^\"]+)\">[^<]*\\n[^<]*<\\/span>/g\n\nconst applyMultilineFix = function (ast) {\n // AST to HTML\n let html = toHtml(ast)\n\n // Fix JSX issue\n html = html.replace(MULTILINE_TOKEN_SPAN, (match, token) =>\n match.replace(/\\n/g, `</span>\\n<span class=\"token ${token}\">`),\n )\n\n // HTML to AST\n const hast = unified()\n .use(parse, { emitParseErrors: true, fragment: true })\n .parse(html)\n\n return hast.children\n}\n\nexport function highlightLine(ast, lines) {\n const formattedAst = applyMultilineFix(ast)\n const numbered = lineNumberify(formattedAst).nodes\n return wrapLines(numbered, lines)\n}\n", "// const visit = require('unist-util-visit')\nimport { toHtml } from 'hast-util-to-html'\nimport parse from 'rehype-parse'\nimport { unified } from 'unified'\n\nconst CALLOUT = /__(.*?)__/g\n\nexport function highlightWord(code) {\n const html = toHtml(code)\n const result = html.replace(\n CALLOUT,\n (_, text) => `<span class=\"highlight-word\">${text}</span>`,\n )\n const hast = unified()\n .use(parse, { emitParseErrors: true, fragment: true })\n .parse(result)\n return hast.children\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,4BAAuB;AACvB,iCAAwB;AACxB,uBAA0B;AAC1B,iBAAgB;AAChB,iBAAgB;;;ACFhB,+BAAuB;AACvB,0BAAkB;AAClB,qBAAwB;AAExB,IAAM,gBAAgB,SAASC,eAAc,KAAK,UAAU,GAAG;AAC7D,MAAI,aAAa;AACjB,SAAO,IAAI;AAAA,IACT,CAAC,QAAQ,SAAS;AAChB,UAAI,KAAK,SAAS,QAAQ;AACxB,YAAI,KAAK,MAAM,QAAQ,IAAI,MAAM,IAAI;AACnC,eAAK,aAAa;AAClB,iBAAO,MAAM,KAAK,IAAI;AACtB,iBAAO;AAAA,QACT;AAEA,cAAM,QAAQ,KAAK,MAAM,MAAM,IAAI;AACnC,iBAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAI,MAAM;AAAG,cAAE;AACf,cAAI,MAAM,MAAM,SAAS,KAAK,MAAM,CAAC,EAAE,WAAW;AAAG;AACrD,iBAAO,MAAM,KAAK;AAAA,YAChB,MAAM;AAAA,YACN,OAAO,MAAM,MAAM,SAAS,IAAI,MAAM,CAAC,IAAI,GAAG,MAAM,CA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T,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;AAGA,IAAM,uBAAuB;AAE7B,IAAM,oBAAoB,SAAU,KAAK;AAEvC,MAAI,WAAO,iCAAO,GAAG;AAGrB,SAAO,KAAK;AAAA,IAAQ;AAAA,IAAsB,CAAC,OAAO,UAChD,MAAM,QAAQ,OAAO;AAAA,qBAA+B,SAAS;AAAA,EAC/D;AAGA,QAAM,WAAO,wBAAQ,EAClB,IAAI,oBAAAC,SAAO,EAAE,iBAAiB,MAAM,UAAU,KAAK,CAAC,EACpD,MAAM,IAAI;AAEb,SAAO,KAAK;AACd;AAEO,SAAS,cAAc,KAAK,OAAO;AACxC,QAAM,eAAe,kBAAkB,GAAG;AAC1C,QAAM,WAAW,cAAc,YAAY,EAAE;AAC7C,SAAO,UAAU,UAAU,KAAK;AAClC;;;ACjHA,IAAAC,4BAAuB;AACvB,IAAAC,uBAAkB;AAClB,IAAAC,kBAAwB;AAExB,IAAM,UAAU;AAET,SAAS,cAAc,MAAM;AAClC,QAAM,WAAO,kCAAO,IAAI;AACxB,QAAM,SAAS,KAAK;AAAA,IAClB;AAAA,IACA,CAAC,GAAG,SAAS,gCAAgC;AAAA,EAC/C;AACA,QAAM,WAAO,yBAAQ,EAClB,IAAI,qBAAAC,SAAO,EAAE,iBAAiB,MAAM,UAAU,KAAK,CAAC,EACpD,MAAM,MAAM;AACf,SAAO,KAAK;AACd;;;AFRA,2BAAU,SAAS,WAAAC,OAAG;AACtB,2BAAU,SAAS,WAAAC,OAAG;AAEf,SAAS,WACd,QACA,UACA,OAAO,KACP;AACA,MAAI,SAAc,2BAAU,UAAU,QAAQ,QAAQ;AACtD,WAAS,cAAc,YAAQ,2BAAAC,SAAY,IAAI,CAAC;AAChD,WAAS,cAAc,MAAM;AAC7B,eAAS,kCAAO,MAAM;AACtB,SAAO;AACT;",
|
|
6
|
+
"names": ["import_hast_util_to_html", "lineNumberify", "wrapLines", "parse", "import_hast_util_to_html", "import_rehype_parse", "import_unified", "parse", "tsx", "css", "rangeParser"]
|
|
7
7
|
}
|