@willa-ui/shared 0.0.2 → 0.0.3-alpha.284d401
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/index.cjs +98 -1
- package/dist/index.d.cts +17 -2
- package/dist/index.global.js +49340 -1
- package/dist/index.js +85 -1
- package/dist/index.mjs +72 -2
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @willa-ui/shared.js v0.0.
|
|
2
|
+
* @willa-ui/shared.js v0.0.3-alpha.284d401
|
|
3
3
|
* (c) 2026 chentao.arthur
|
|
4
4
|
*/
|
|
5
|
+
import hljs from "highlight.js";
|
|
5
6
|
import { isValidElement } from "react";
|
|
6
7
|
import { isArray, isNil, isString } from "aidly";
|
|
7
8
|
//#region src/clipboard.ts
|
|
@@ -29,6 +30,85 @@ async function copyToClipboard(text) {
|
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
//#endregion
|
|
33
|
+
//#region src/codeHighlight.ts
|
|
34
|
+
const parseCodeMeta = (className) => {
|
|
35
|
+
const [rawLanguage = "text", rawMeta = ""] = (
|
|
36
|
+
/language-([^\s]+)/.exec(className ?? "")?.[1] ?? "text"
|
|
37
|
+
).split("--meta-");
|
|
38
|
+
const highlightLines = /* @__PURE__ */ new Set();
|
|
39
|
+
const normalizedMeta = rawMeta.replace(/_/g, " ");
|
|
40
|
+
const showLineNumbers = /(?:^|[^A-Za-z0-9-])ln(?:$|[^A-Za-z0-9-])/.test(
|
|
41
|
+
normalizedMeta,
|
|
42
|
+
);
|
|
43
|
+
for (const match of rawMeta.matchAll(/\{([^}]+)\}/g))
|
|
44
|
+
addCodeHighlightRange(highlightLines, match[1]);
|
|
45
|
+
return {
|
|
46
|
+
rawLanguage,
|
|
47
|
+
highlightLines,
|
|
48
|
+
showLineNumbers,
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const normalizeHljsLanguage = (language) => {
|
|
52
|
+
const key = language.toLowerCase();
|
|
53
|
+
return (
|
|
54
|
+
{
|
|
55
|
+
c: "c",
|
|
56
|
+
cc: "cpp",
|
|
57
|
+
"c++": "cpp",
|
|
58
|
+
cpp: "cpp",
|
|
59
|
+
cxx: "cpp",
|
|
60
|
+
css: "css",
|
|
61
|
+
go: "go",
|
|
62
|
+
golang: "go",
|
|
63
|
+
html: "xml",
|
|
64
|
+
js: "javascript",
|
|
65
|
+
jsx: "javascript",
|
|
66
|
+
rs: "rust",
|
|
67
|
+
rust: "rust",
|
|
68
|
+
ts: "typescript",
|
|
69
|
+
tsx: "typescript",
|
|
70
|
+
sh: "bash",
|
|
71
|
+
bash: "bash",
|
|
72
|
+
shell: "bash",
|
|
73
|
+
yml: "yaml",
|
|
74
|
+
md: "markdown",
|
|
75
|
+
}[key] ?? key
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
const highlightCodeToHtml = (code, rawLanguage) => {
|
|
79
|
+
const language = normalizeHljsLanguage(rawLanguage);
|
|
80
|
+
if (hljs.getLanguage(language))
|
|
81
|
+
return {
|
|
82
|
+
html: hljs.highlight(code, { language }).value,
|
|
83
|
+
display: rawLanguage,
|
|
84
|
+
};
|
|
85
|
+
const result = hljs.highlightAuto(code);
|
|
86
|
+
return {
|
|
87
|
+
html: result.value,
|
|
88
|
+
display: result.language ?? rawLanguage,
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
const createCodeHighlightLines = (ranges) => {
|
|
92
|
+
const highlightLines = /* @__PURE__ */ new Set();
|
|
93
|
+
for (const range of ranges ?? [])
|
|
94
|
+
if (typeof range === "number") {
|
|
95
|
+
if (!Number.isInteger(range) || range <= 0) continue;
|
|
96
|
+
highlightLines.add(range);
|
|
97
|
+
} else addCodeHighlightRange(highlightLines, range.join("-"));
|
|
98
|
+
return highlightLines;
|
|
99
|
+
};
|
|
100
|
+
const addCodeHighlightRange = (highlightLines, value) => {
|
|
101
|
+
for (const part of value.split(",")) {
|
|
102
|
+
const rangeMatch = /^\s*(\d+)(?:-(\d+))?\s*$/.exec(part);
|
|
103
|
+
if (!rangeMatch) continue;
|
|
104
|
+
const start = Number(rangeMatch[1]);
|
|
105
|
+
const end = Number(rangeMatch[2] ?? rangeMatch[1]);
|
|
106
|
+
if (!Number.isInteger(start) || !Number.isInteger(end)) continue;
|
|
107
|
+
for (let line = start; line <= end; line += 1)
|
|
108
|
+
if (line > 0) highlightLines.add(line);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
//#endregion
|
|
32
112
|
//#region src/nodes.ts
|
|
33
113
|
const toNodeArray = (children) => {
|
|
34
114
|
return (isArray(children) ? children : [children]).filter((item) => {
|
|
@@ -106,8 +186,12 @@ function extractHeadings(source) {
|
|
|
106
186
|
//#endregion
|
|
107
187
|
export {
|
|
108
188
|
copyToClipboard,
|
|
189
|
+
createCodeHighlightLines,
|
|
109
190
|
createHeadingIdFactory,
|
|
110
191
|
extractHeadings,
|
|
111
192
|
flattenText,
|
|
193
|
+
highlightCodeToHtml,
|
|
112
194
|
isMediaOnlyParagraph,
|
|
195
|
+
normalizeHljsLanguage,
|
|
196
|
+
parseCodeMeta,
|
|
113
197
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @willa-ui/shared.js v0.0.
|
|
2
|
+
* @willa-ui/shared.js v0.0.3-alpha.284d401
|
|
3
3
|
* (c) 2026 chentao.arthur
|
|
4
4
|
*/
|
|
5
|
+
import hljs from "highlight.js";
|
|
5
6
|
import { isValidElement } from "react";
|
|
6
7
|
import { isArray, isNil, isString } from "aidly";
|
|
7
8
|
//#region src/clipboard.ts
|
|
@@ -29,6 +30,75 @@ async function copyToClipboard(text) {
|
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
//#endregion
|
|
33
|
+
//#region src/codeHighlight.ts
|
|
34
|
+
const parseCodeMeta = (className) => {
|
|
35
|
+
const [rawLanguage = "text", rawMeta = ""] = (/language-([^\s]+)/.exec(className ?? "")?.[1] ?? "text").split("--meta-");
|
|
36
|
+
const highlightLines = /* @__PURE__ */ new Set();
|
|
37
|
+
const normalizedMeta = rawMeta.replace(/_/g, " ");
|
|
38
|
+
const showLineNumbers = /(?:^|[^A-Za-z0-9-])ln(?:$|[^A-Za-z0-9-])/.test(normalizedMeta);
|
|
39
|
+
for (const match of rawMeta.matchAll(/\{([^}]+)\}/g)) addCodeHighlightRange(highlightLines, match[1]);
|
|
40
|
+
return {
|
|
41
|
+
rawLanguage,
|
|
42
|
+
highlightLines,
|
|
43
|
+
showLineNumbers
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
const normalizeHljsLanguage = (language) => {
|
|
47
|
+
const key = language.toLowerCase();
|
|
48
|
+
return {
|
|
49
|
+
c: "c",
|
|
50
|
+
cc: "cpp",
|
|
51
|
+
"c++": "cpp",
|
|
52
|
+
cpp: "cpp",
|
|
53
|
+
cxx: "cpp",
|
|
54
|
+
css: "css",
|
|
55
|
+
go: "go",
|
|
56
|
+
golang: "go",
|
|
57
|
+
html: "xml",
|
|
58
|
+
js: "javascript",
|
|
59
|
+
jsx: "javascript",
|
|
60
|
+
rs: "rust",
|
|
61
|
+
rust: "rust",
|
|
62
|
+
ts: "typescript",
|
|
63
|
+
tsx: "typescript",
|
|
64
|
+
sh: "bash",
|
|
65
|
+
bash: "bash",
|
|
66
|
+
shell: "bash",
|
|
67
|
+
yml: "yaml",
|
|
68
|
+
md: "markdown"
|
|
69
|
+
}[key] ?? key;
|
|
70
|
+
};
|
|
71
|
+
const highlightCodeToHtml = (code, rawLanguage) => {
|
|
72
|
+
const language = normalizeHljsLanguage(rawLanguage);
|
|
73
|
+
if (hljs.getLanguage(language)) return {
|
|
74
|
+
html: hljs.highlight(code, { language }).value,
|
|
75
|
+
display: rawLanguage
|
|
76
|
+
};
|
|
77
|
+
const result = hljs.highlightAuto(code);
|
|
78
|
+
return {
|
|
79
|
+
html: result.value,
|
|
80
|
+
display: result.language ?? rawLanguage
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const createCodeHighlightLines = (ranges) => {
|
|
84
|
+
const highlightLines = /* @__PURE__ */ new Set();
|
|
85
|
+
for (const range of ranges ?? []) if (typeof range === "number") {
|
|
86
|
+
if (!Number.isInteger(range) || range <= 0) continue;
|
|
87
|
+
highlightLines.add(range);
|
|
88
|
+
} else addCodeHighlightRange(highlightLines, range.join("-"));
|
|
89
|
+
return highlightLines;
|
|
90
|
+
};
|
|
91
|
+
const addCodeHighlightRange = (highlightLines, value) => {
|
|
92
|
+
for (const part of value.split(",")) {
|
|
93
|
+
const rangeMatch = /^\s*(\d+)(?:-(\d+))?\s*$/.exec(part);
|
|
94
|
+
if (!rangeMatch) continue;
|
|
95
|
+
const start = Number(rangeMatch[1]);
|
|
96
|
+
const end = Number(rangeMatch[2] ?? rangeMatch[1]);
|
|
97
|
+
if (!Number.isInteger(start) || !Number.isInteger(end)) continue;
|
|
98
|
+
for (let line = start; line <= end; line += 1) if (line > 0) highlightLines.add(line);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
32
102
|
//#region src/nodes.ts
|
|
33
103
|
const toNodeArray = (children) => {
|
|
34
104
|
return (isArray(children) ? children : [children]).filter((item) => {
|
|
@@ -93,4 +163,4 @@ function extractHeadings(source) {
|
|
|
93
163
|
return headings;
|
|
94
164
|
}
|
|
95
165
|
//#endregion
|
|
96
|
-
export { copyToClipboard, createHeadingIdFactory, extractHeadings, flattenText, isMediaOnlyParagraph };
|
|
166
|
+
export { copyToClipboard, createCodeHighlightLines, createHeadingIdFactory, extractHeadings, flattenText, highlightCodeToHtml, isMediaOnlyParagraph, normalizeHljsLanguage, parseCodeMeta };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willa-ui/shared",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3-alpha.284d401",
|
|
4
4
|
"description": "Shared utilities and types for Willa UI.",
|
|
5
5
|
"author": "chentao.arthur",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"aidly": "1.37.0"
|
|
27
|
+
"aidly": "1.37.0",
|
|
28
|
+
"highlight.js": "11.11.1"
|
|
28
29
|
},
|
|
29
30
|
"peerDependencies": {
|
|
30
31
|
"react": "19.2.6"
|
|
@@ -32,7 +33,7 @@
|
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@types/react": "19.2.14",
|
|
34
35
|
"@vitest/coverage-v8": "1.6.0",
|
|
35
|
-
"auklet": "0.1.
|
|
36
|
+
"auklet": "0.1.8",
|
|
36
37
|
"vitest": "1.6.0",
|
|
37
38
|
"typescript": "6.0.3"
|
|
38
39
|
},
|