@botfather/vite-plugin-units-tools 1.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/LICENSE +21 -0
- package/index.d.ts +9 -0
- package/index.js +154 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Units Authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { formatUnits } from "@botfather/units/print";
|
|
4
|
+
|
|
5
|
+
function tokenizeUnits(input) {
|
|
6
|
+
const s = String(input ?? "");
|
|
7
|
+
const tokens = [];
|
|
8
|
+
let i = 0;
|
|
9
|
+
|
|
10
|
+
const isWS = (ch) => ch === " " || ch === "\n" || ch === "\t" || ch === "\r";
|
|
11
|
+
const isIdentStart = (ch) => (ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || ch === "_";
|
|
12
|
+
const isIdent = (ch) => isIdentStart(ch) || (ch >= "0" && ch <= "9") || ch === ":" || ch === "." || ch === "-";
|
|
13
|
+
|
|
14
|
+
while (i < s.length) {
|
|
15
|
+
const ch = s[i];
|
|
16
|
+
if (isWS(ch)) {
|
|
17
|
+
let start = i;
|
|
18
|
+
while (i < s.length && isWS(s[i])) i++;
|
|
19
|
+
tokens.push({ type: "ws", value: s.slice(start, i) });
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (ch === "/" && s[i + 1] === "/") {
|
|
23
|
+
let start = i;
|
|
24
|
+
i += 2;
|
|
25
|
+
while (i < s.length && s[i] !== "\n") i++;
|
|
26
|
+
tokens.push({ type: "comment", value: s.slice(start, i) });
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (ch === "'") {
|
|
30
|
+
let start = i++;
|
|
31
|
+
while (i < s.length) {
|
|
32
|
+
if (s[i] === "\\" && i + 1 < s.length) { i += 2; continue; }
|
|
33
|
+
if (s[i] === "'") { i++; break; }
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
tokens.push({ type: "string", value: s.slice(start, i) });
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (ch === "#") {
|
|
40
|
+
let start = i++;
|
|
41
|
+
while (i < s.length && isIdent(s[i])) i++;
|
|
42
|
+
tokens.push({ type: "directive", value: s.slice(start, i) });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (ch === "@") {
|
|
46
|
+
let start = i++;
|
|
47
|
+
while (i < s.length && /[A-Za-z0-9_.$]/.test(s[i])) i++;
|
|
48
|
+
tokens.push({ type: "expr", value: s.slice(start, i) });
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (isIdentStart(ch)) {
|
|
52
|
+
let start = i++;
|
|
53
|
+
while (i < s.length && isIdent(s[i])) i++;
|
|
54
|
+
const value = s.slice(start, i);
|
|
55
|
+
const type = value === "text" ? "keyword" : "ident";
|
|
56
|
+
tokens.push({ type, value });
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if ((ch >= "0" && ch <= "9") || (ch === "-" && s[i + 1] >= "0" && s[i + 1] <= "9")) {
|
|
60
|
+
let start = i++;
|
|
61
|
+
while (i < s.length && /[0-9.]/.test(s[i])) i++;
|
|
62
|
+
tokens.push({ type: "number", value: s.slice(start, i) });
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const punct = "(){}[],:=!?";
|
|
67
|
+
if (punct.includes(ch)) {
|
|
68
|
+
tokens.push({ type: "punct", value: ch });
|
|
69
|
+
i++;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
tokens.push({ type: "unknown", value: ch });
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return tokens;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function escapeHtml(value) {
|
|
81
|
+
return value
|
|
82
|
+
.replace(/&/g, "&")
|
|
83
|
+
.replace(/</g, "<")
|
|
84
|
+
.replace(/>/g, ">")
|
|
85
|
+
.replace(/\"/g, """);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function highlightTokens(tokens, classPrefix) {
|
|
89
|
+
const prefix = classPrefix ? `${classPrefix}-` : "";
|
|
90
|
+
return tokens.map((t) => {
|
|
91
|
+
const cls = `${prefix}tok-${t.type}`;
|
|
92
|
+
return `<span class=\"${cls}\">${escapeHtml(t.value)}</span>`;
|
|
93
|
+
}).join("");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default function unitsTools(options = {}) {
|
|
97
|
+
const include = options.include;
|
|
98
|
+
const exclude = options.exclude;
|
|
99
|
+
const classPrefix = options.classPrefix || "";
|
|
100
|
+
|
|
101
|
+
function isRelOrAbs(id) {
|
|
102
|
+
return id.startsWith(".") || id.startsWith("/");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isAllowed(id) {
|
|
106
|
+
if (!id.endsWith(".ui")) return false;
|
|
107
|
+
if (include && !include.test(id)) return false;
|
|
108
|
+
if (exclude && exclude.test(id)) return false;
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
name: "units-tools",
|
|
114
|
+
enforce: "pre",
|
|
115
|
+
async resolveId(source, importer) {
|
|
116
|
+
if (source.endsWith(".ui?format") || source.endsWith(".ui?tokens") || source.endsWith(".ui?highlight")) {
|
|
117
|
+
const base = source.replace(/\?.*$/, "");
|
|
118
|
+
if (isRelOrAbs(base)) {
|
|
119
|
+
const resolved = importer ? path.resolve(path.dirname(importer), base) : path.resolve(base);
|
|
120
|
+
return resolved + source.slice(base.length);
|
|
121
|
+
}
|
|
122
|
+
const resolved = await this.resolve(base, importer, { skipSelf: true });
|
|
123
|
+
if (!resolved) return null;
|
|
124
|
+
return resolved.id + source.slice(base.length);
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
},
|
|
128
|
+
load(id) {
|
|
129
|
+
if (id.endsWith(".ui?format")) {
|
|
130
|
+
const file = id.replace(/\?.*$/, "");
|
|
131
|
+
if (!isAllowed(file)) return null;
|
|
132
|
+
const code = fs.readFileSync(file, "utf-8");
|
|
133
|
+
const formatted = formatUnits(code);
|
|
134
|
+
return `export default ${JSON.stringify(formatted)};\n`;
|
|
135
|
+
}
|
|
136
|
+
if (id.endsWith(".ui?tokens")) {
|
|
137
|
+
const file = id.replace(/\?.*$/, "");
|
|
138
|
+
if (!isAllowed(file)) return null;
|
|
139
|
+
const code = fs.readFileSync(file, "utf-8");
|
|
140
|
+
const tokens = tokenizeUnits(code);
|
|
141
|
+
return `export default ${JSON.stringify(tokens)};\n`;
|
|
142
|
+
}
|
|
143
|
+
if (id.endsWith(".ui?highlight")) {
|
|
144
|
+
const file = id.replace(/\?.*$/, "");
|
|
145
|
+
if (!isAllowed(file)) return null;
|
|
146
|
+
const code = fs.readFileSync(file, "utf-8");
|
|
147
|
+
const tokens = tokenizeUnits(code);
|
|
148
|
+
const html = highlightTokens(tokens, classPrefix);
|
|
149
|
+
return `export default ${JSON.stringify(html)};\n`;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@botfather/vite-plugin-units-tools",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vite dev tools for Units (.ui) formatting, tokens, and highlighting.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Botfather/units.git",
|
|
10
|
+
"directory": "packages/vite-plugin-units-tools"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/Botfather/units/tree/main/packages/vite-plugin-units-tools",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Botfather/units/issues"
|
|
15
|
+
},
|
|
16
|
+
"main": "./index.js",
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./index.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"index.js",
|
|
23
|
+
"index.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@botfather/units": "1.0.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"vite": ">=4"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|