@satorijs/element 1.0.1
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/lib/.DS_Store +0 -0
- package/lib/index.cjs +291 -0
- package/lib/index.cjs.map +7 -0
- package/lib/index.d.ts +53 -0
- package/lib/index.mjs +304 -0
- package/lib/index.mjs.map +7 -0
- package/lib/utils.d.ts +7 -0
- package/package.json +36 -0
package/lib/.DS_Store
ADDED
|
Binary file
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// satori/packages/element/src/index.ts
|
|
5
|
+
var import_cosmokit = require("cosmokit");
|
|
6
|
+
|
|
7
|
+
// satori/packages/element/src/utils.ts
|
|
8
|
+
var root = typeof self !== "undefined" ? self : global;
|
|
9
|
+
function isType(type, value) {
|
|
10
|
+
return type in root && value instanceof root[type] || Object.prototype.toString.call(value).slice(8, -1) === type;
|
|
11
|
+
}
|
|
12
|
+
__name(isType, "isType");
|
|
13
|
+
|
|
14
|
+
// satori/packages/element/src/index.ts
|
|
15
|
+
var kElement = Symbol("element");
|
|
16
|
+
function isElement(source) {
|
|
17
|
+
return source && typeof source === "object" && source[kElement];
|
|
18
|
+
}
|
|
19
|
+
__name(isElement, "isElement");
|
|
20
|
+
function toElement(content) {
|
|
21
|
+
if (typeof content !== "string")
|
|
22
|
+
return content;
|
|
23
|
+
return Element("text", { content });
|
|
24
|
+
}
|
|
25
|
+
__name(toElement, "toElement");
|
|
26
|
+
function toElementArray(input) {
|
|
27
|
+
if (Array.isArray(input)) {
|
|
28
|
+
return input.map(toElement);
|
|
29
|
+
} else if (typeof input === "string") {
|
|
30
|
+
return [toElement(input)];
|
|
31
|
+
} else if (!input.type) {
|
|
32
|
+
return input.children;
|
|
33
|
+
} else {
|
|
34
|
+
return [input];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
__name(toElementArray, "toElementArray");
|
|
38
|
+
var ElementConstructor = class {
|
|
39
|
+
get data() {
|
|
40
|
+
return this.attrs;
|
|
41
|
+
}
|
|
42
|
+
toString(strip = false) {
|
|
43
|
+
const inner = this.children.map((child) => child.toString(strip)).join("");
|
|
44
|
+
if (!this.type || strip)
|
|
45
|
+
return inner;
|
|
46
|
+
if (this.type === "text")
|
|
47
|
+
return Element.escape(this.attrs.content);
|
|
48
|
+
const attrs = Object.entries(this.attrs).map(([key, value]) => {
|
|
49
|
+
if ((0, import_cosmokit.isNullable)(value))
|
|
50
|
+
return "";
|
|
51
|
+
key = (0, import_cosmokit.hyphenate)(key);
|
|
52
|
+
if (value === "")
|
|
53
|
+
return ` ${key}`;
|
|
54
|
+
return ` ${key}="${Element.escape(value, true)}"`;
|
|
55
|
+
}).join("");
|
|
56
|
+
if (!this.children.length)
|
|
57
|
+
return `<${this.type}${attrs}/>`;
|
|
58
|
+
return `<${this.type}${attrs}>${inner}</${this.type}>`;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
__name(ElementConstructor, "ElementConstructor");
|
|
62
|
+
(0, import_cosmokit.defineProperty)(ElementConstructor, "name", "Element");
|
|
63
|
+
function Element(type, ...args) {
|
|
64
|
+
const el = Object.create(ElementConstructor.prototype);
|
|
65
|
+
let attrs = {}, children = [];
|
|
66
|
+
if (args[0] && typeof args[0] === "object" && !isElement(args[0]) && !Array.isArray(args[0])) {
|
|
67
|
+
for (const [key, value] of Object.entries(args.shift())) {
|
|
68
|
+
if ((0, import_cosmokit.isNullable)(value))
|
|
69
|
+
continue;
|
|
70
|
+
if (value === true) {
|
|
71
|
+
attrs[key] = "";
|
|
72
|
+
} else if (value === false) {
|
|
73
|
+
attrs["no" + (0, import_cosmokit.capitalize)(key)] = "";
|
|
74
|
+
} else {
|
|
75
|
+
attrs[key] = "" + value;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (args[0])
|
|
80
|
+
children = toElementArray(args[0]);
|
|
81
|
+
return Object.assign(el, { type, attrs, children });
|
|
82
|
+
}
|
|
83
|
+
__name(Element, "Element");
|
|
84
|
+
((Element2) => {
|
|
85
|
+
function normalize(source) {
|
|
86
|
+
if (typeof source !== "string")
|
|
87
|
+
return Element2(null, source);
|
|
88
|
+
return Element2.parse(source, true);
|
|
89
|
+
}
|
|
90
|
+
Element2.normalize = normalize;
|
|
91
|
+
__name(normalize, "normalize");
|
|
92
|
+
function escape(source, inline = false) {
|
|
93
|
+
const result = source.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
94
|
+
return inline ? result.replace(/"/g, """) : result;
|
|
95
|
+
}
|
|
96
|
+
Element2.escape = escape;
|
|
97
|
+
__name(escape, "escape");
|
|
98
|
+
function unescape(source) {
|
|
99
|
+
return source.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/&/g, "&");
|
|
100
|
+
}
|
|
101
|
+
Element2.unescape = unescape;
|
|
102
|
+
__name(unescape, "unescape");
|
|
103
|
+
function from(source, options = {}) {
|
|
104
|
+
var _a;
|
|
105
|
+
const elements = parse(source);
|
|
106
|
+
if (options.caret) {
|
|
107
|
+
if (options.type && ((_a = elements[0]) == null ? void 0 : _a.type) !== options.type)
|
|
108
|
+
return;
|
|
109
|
+
return elements[0];
|
|
110
|
+
}
|
|
111
|
+
return select(elements, options.type || "*")[0];
|
|
112
|
+
}
|
|
113
|
+
Element2.from = from;
|
|
114
|
+
__name(from, "from");
|
|
115
|
+
const combRegExp = / *([ >+~]) */g;
|
|
116
|
+
function parseSelector(input) {
|
|
117
|
+
return input.split(",").map((query) => {
|
|
118
|
+
const selectors = [];
|
|
119
|
+
query = query.trim();
|
|
120
|
+
let combCap, combinator = " ";
|
|
121
|
+
while (combCap = combRegExp.exec(query)) {
|
|
122
|
+
selectors.push({ type: query.slice(0, combCap.index), combinator });
|
|
123
|
+
combinator = combCap[1];
|
|
124
|
+
query = query.slice(combCap.index + combCap[0].length);
|
|
125
|
+
}
|
|
126
|
+
selectors.push({ type: query, combinator });
|
|
127
|
+
return selectors;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
Element2.parseSelector = parseSelector;
|
|
131
|
+
__name(parseSelector, "parseSelector");
|
|
132
|
+
function select(source, query) {
|
|
133
|
+
if (typeof source === "string")
|
|
134
|
+
source = parse(source);
|
|
135
|
+
if (typeof query === "string")
|
|
136
|
+
query = parseSelector(query);
|
|
137
|
+
if (!query.length)
|
|
138
|
+
return;
|
|
139
|
+
let adjacent = [];
|
|
140
|
+
const results = [];
|
|
141
|
+
for (const [index, element] of source.entries()) {
|
|
142
|
+
const inner = [];
|
|
143
|
+
const local = [...query, ...adjacent];
|
|
144
|
+
adjacent = [];
|
|
145
|
+
let matched = false;
|
|
146
|
+
for (const group of local) {
|
|
147
|
+
const { type, combinator } = group[0];
|
|
148
|
+
if (type === element.type || type === "*") {
|
|
149
|
+
if (group.length === 1) {
|
|
150
|
+
matched = true;
|
|
151
|
+
} else if ([" ", ">"].includes(group[1].combinator)) {
|
|
152
|
+
inner.push(group.slice(1));
|
|
153
|
+
} else if (group[1].combinator === "+") {
|
|
154
|
+
adjacent.push(group.slice(1));
|
|
155
|
+
} else {
|
|
156
|
+
query.push(group.slice(1));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (combinator === " ") {
|
|
160
|
+
inner.push(group);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (matched)
|
|
164
|
+
results.push(source[index]);
|
|
165
|
+
results.push(...select(element.children, inner));
|
|
166
|
+
}
|
|
167
|
+
return results;
|
|
168
|
+
}
|
|
169
|
+
Element2.select = select;
|
|
170
|
+
__name(select, "select");
|
|
171
|
+
const tagRegExp = /<(\/?)\s*([^\s>]+)([^>]*?)\s*(\/?)>/;
|
|
172
|
+
const attrRegExp = /([^\s=]+)(?:="([^"]*)"|=([^"\s]+))?/g;
|
|
173
|
+
function parse(source, fragment = false) {
|
|
174
|
+
const tokens = [];
|
|
175
|
+
let tagCap;
|
|
176
|
+
while (tagCap = tagRegExp.exec(source)) {
|
|
177
|
+
if (tagCap.index) {
|
|
178
|
+
tokens.push(unescape(source.slice(0, tagCap.index)));
|
|
179
|
+
}
|
|
180
|
+
const [_, close, tag, attrs, empty] = tagCap;
|
|
181
|
+
const token = { tag, close, empty, attrs: {} };
|
|
182
|
+
let attrCap;
|
|
183
|
+
while (attrCap = attrRegExp.exec(attrs)) {
|
|
184
|
+
const [_2, key, v1 = "", v2 = v1] = attrCap;
|
|
185
|
+
token.attrs[(0, import_cosmokit.camelize)(key)] = unescape(v2);
|
|
186
|
+
}
|
|
187
|
+
tokens.push(token);
|
|
188
|
+
source = source.slice(tagCap.index + tagCap[0].length);
|
|
189
|
+
}
|
|
190
|
+
if (source)
|
|
191
|
+
tokens.push(unescape(source));
|
|
192
|
+
const stack = [Element2(null)];
|
|
193
|
+
for (const token of tokens) {
|
|
194
|
+
if (typeof token === "string") {
|
|
195
|
+
stack[0].children.push(Element2("text", { content: token }));
|
|
196
|
+
} else if (token.close) {
|
|
197
|
+
stack.shift();
|
|
198
|
+
} else {
|
|
199
|
+
const element = Element2(token.tag, token.attrs);
|
|
200
|
+
stack[0].children.push(element);
|
|
201
|
+
if (!token.empty)
|
|
202
|
+
stack.unshift(element);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const root2 = stack[stack.length - 1];
|
|
206
|
+
return fragment ? root2 : root2.children;
|
|
207
|
+
}
|
|
208
|
+
Element2.parse = parse;
|
|
209
|
+
__name(parse, "parse");
|
|
210
|
+
function transform(source, rules) {
|
|
211
|
+
const elements = typeof source === "string" ? parse(source) : source;
|
|
212
|
+
const output = [];
|
|
213
|
+
elements.forEach((element, index, elements2) => {
|
|
214
|
+
var _a, _b;
|
|
215
|
+
let result = (_b = (_a = rules[element.type]) != null ? _a : rules.default) != null ? _b : true;
|
|
216
|
+
if (typeof result === "function") {
|
|
217
|
+
result = result(element.attrs, index, elements2);
|
|
218
|
+
}
|
|
219
|
+
if (result === true) {
|
|
220
|
+
const { type, attrs, children } = element;
|
|
221
|
+
output.push(Element2(type, attrs, transform(children, rules)));
|
|
222
|
+
} else if (result !== false) {
|
|
223
|
+
output.push(...toElementArray(result));
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
return typeof source === "string" ? output.join("") : output;
|
|
227
|
+
}
|
|
228
|
+
Element2.transform = transform;
|
|
229
|
+
__name(transform, "transform");
|
|
230
|
+
async function transformAsync(source, rules) {
|
|
231
|
+
const elements = typeof source === "string" ? parse(source) : source;
|
|
232
|
+
const children = (await Promise.all(elements.map(async (element, index, elements2) => {
|
|
233
|
+
var _a, _b;
|
|
234
|
+
let result = (_b = (_a = rules[element.type]) != null ? _a : rules.default) != null ? _b : true;
|
|
235
|
+
if (typeof result === "function") {
|
|
236
|
+
result = await result(element.attrs, index, elements2);
|
|
237
|
+
}
|
|
238
|
+
if (result === true) {
|
|
239
|
+
const { type, attrs, children: children2 } = element;
|
|
240
|
+
return [Element2(type, attrs, await transformAsync(children2, rules))];
|
|
241
|
+
} else if (result !== false) {
|
|
242
|
+
return toElementArray(result);
|
|
243
|
+
} else {
|
|
244
|
+
return [];
|
|
245
|
+
}
|
|
246
|
+
}))).flat(1);
|
|
247
|
+
return typeof source === "string" ? children.join("") : children;
|
|
248
|
+
}
|
|
249
|
+
Element2.transformAsync = transformAsync;
|
|
250
|
+
__name(transformAsync, "transformAsync");
|
|
251
|
+
function join(elements) {
|
|
252
|
+
return elements.join("");
|
|
253
|
+
}
|
|
254
|
+
Element2.join = join;
|
|
255
|
+
__name(join, "join");
|
|
256
|
+
function createFactory(type, ...keys) {
|
|
257
|
+
return (...args) => {
|
|
258
|
+
const element = Element2(type);
|
|
259
|
+
keys.forEach((key, index) => {
|
|
260
|
+
if (!(0, import_cosmokit.isNullable)(args[index])) {
|
|
261
|
+
element.attrs[key] = args[index];
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
if (args[keys.length]) {
|
|
265
|
+
Object.assign(element.attrs, args[keys.length]);
|
|
266
|
+
}
|
|
267
|
+
return element;
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
__name(createFactory, "createFactory");
|
|
271
|
+
function createAssetFactory(type) {
|
|
272
|
+
return (value, attrs = {}) => {
|
|
273
|
+
if (isType("Buffer", value)) {
|
|
274
|
+
value = "base64://" + value.toString("base64");
|
|
275
|
+
} else if (isType("ArrayBuffer", value)) {
|
|
276
|
+
value = "base64://" + Buffer.from(value).toString("base64");
|
|
277
|
+
}
|
|
278
|
+
return Element2(type, { ...attrs, url: value });
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
__name(createAssetFactory, "createAssetFactory");
|
|
282
|
+
Element2.at = createFactory("at", "id");
|
|
283
|
+
Element2.sharp = createFactory("sharp", "id");
|
|
284
|
+
Element2.quote = createFactory("quote", "id");
|
|
285
|
+
Element2.image = createAssetFactory("image");
|
|
286
|
+
Element2.video = createAssetFactory("video");
|
|
287
|
+
Element2.audio = createAssetFactory("audio");
|
|
288
|
+
Element2.file = createAssetFactory("file");
|
|
289
|
+
})(Element || (Element = {}));
|
|
290
|
+
module.exports = Element;
|
|
291
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/utils.ts"],
|
|
4
|
+
"sourcesContent": ["import { Awaitable, camelize, capitalize, defineProperty, Dict, hyphenate, isNullable } from 'cosmokit'\nimport { isType } from './utils'\n\nconst kElement = Symbol('element')\n\nfunction isElement(source: any): source is Element {\n return source && typeof source === 'object' && source[kElement]\n}\n\nfunction toElement(content: string | Element) {\n if (typeof content !== 'string') return content\n return Element('text', { content })\n}\n\nfunction toElementArray(input: Element.Content) {\n if (Array.isArray(input)) {\n return input.map(toElement)\n } else if (typeof input === 'string') {\n return [toElement(input)]\n } else if (!input.type) {\n return input.children\n } else {\n return [input]\n }\n}\n\ninterface Element {\n [kElement]: true\n type: string\n attrs: Dict<string>\n /** @deprecated use `attrs` instead */\n data: Dict<string>\n children: Element[]\n toString(strip?: boolean): string\n}\n\ninterface ElementConstructor extends Element {}\n\nclass ElementConstructor {\n get data() {\n return this.attrs\n }\n\n toString(strip = false) {\n const inner = this.children.map(child => child.toString(strip)).join('')\n if (!this.type || strip) return inner\n if (this.type === 'text') return Element.escape(this.attrs.content)\n const attrs = Object.entries(this.attrs).map(([key, value]) => {\n if (isNullable(value)) return ''\n key = hyphenate(key)\n if (value === '') return ` ${key}`\n return ` ${key}=\"${Element.escape(value, true)}\"`\n }).join('')\n if (!this.children.length) return `<${this.type}${attrs}/>`\n return `<${this.type}${attrs}>${inner}</${this.type}>`\n }\n}\n\ndefineProperty(ElementConstructor, 'name', 'Element')\n\nfunction Element(type: string, children?: Element.Content): Element\nfunction Element(type: string, attrs: Dict<any>, children?: Element.Content): Element\nfunction Element(type: string, ...args: any[]) {\n const el = Object.create(ElementConstructor.prototype)\n let attrs: Dict<string> = {}, children: Element[] = []\n if (args[0] && typeof args[0] === 'object' && !isElement(args[0]) && !Array.isArray(args[0])) {\n for (const [key, value] of Object.entries(args.shift())) {\n if (isNullable(value)) continue\n if (value === true) {\n attrs[key] = ''\n } else if (value === false) {\n attrs['no' + capitalize(key)] = ''\n } else {\n attrs[key] = '' + value\n }\n }\n }\n if (args[0]) children = toElementArray(args[0])\n return Object.assign(el, { type, attrs, children })\n}\n\nnamespace Element {\n export type Content = string | Element | (string | Element)[]\n export type Transformer = boolean | Content | ((attrs: Dict<string>, index: number, array: Element[]) => boolean | Content)\n export type AsyncTransformer = boolean | Content | ((attrs: Dict<string>, index: number, array: Element[]) => Awaitable<boolean | Content>)\n\n export function normalize(source: string | Element) {\n if (typeof source !== 'string') return Element(null, source)\n return Element.parse(source, true)\n }\n\n export function escape(source: string, inline = false) {\n const result = source\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n return inline\n ? result.replace(/\"/g, '"')\n : result\n }\n\n export function unescape(source: string) {\n return source\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/&/g, '&')\n }\n\n export interface FindOptions {\n type?: string\n caret?: boolean\n }\n\n /** @deprecated use `Element.select()` instead */\n export function from(source: string, options: FindOptions = {}): Element {\n const elements = parse(source)\n if (options.caret) {\n if (options.type && elements[0]?.type !== options.type) return\n return elements[0]\n }\n return select(elements, options.type || '*')[0]\n }\n\n type Combinator = ' ' | '>' | '+' | '~'\n\n export interface Selector {\n type: string\n combinator: Combinator\n }\n\n const combRegExp = / *([ >+~]) */g\n\n export function parseSelector(input: string): Selector[][] {\n return input.split(',').map((query) => {\n const selectors: Selector[] = []\n query = query.trim()\n let combCap: RegExpExecArray, combinator: Combinator = ' '\n while ((combCap = combRegExp.exec(query))) {\n selectors.push({ type: query.slice(0, combCap.index), combinator })\n combinator = combCap[1] as Combinator\n query = query.slice(combCap.index + combCap[0].length)\n }\n selectors.push({ type: query, combinator })\n return selectors\n })\n }\n\n export function select(source: string | Element[], query: string | Selector[][]): Element[] {\n if (typeof source === 'string') source = parse(source)\n if (typeof query === 'string') query = parseSelector(query)\n if (!query.length) return\n let adjacent: Selector[][] = []\n const results: Element[] = []\n for (const [index, element] of source.entries()) {\n const inner: Selector[][] = []\n const local = [...query, ...adjacent]\n adjacent = []\n let matched = false\n for (const group of local) {\n const { type, combinator } = group[0]\n if (type === element.type || type === '*') {\n if (group.length === 1) {\n matched = true\n } else if ([' ', '>'].includes(group[1].combinator)) {\n inner.push(group.slice(1))\n } else if (group[1].combinator === '+') {\n adjacent.push(group.slice(1))\n } else {\n query.push(group.slice(1))\n }\n }\n if (combinator === ' ') {\n inner.push(group)\n }\n }\n if (matched) results.push(source[index])\n results.push(...select(element.children, inner))\n }\n return results\n }\n\n const tagRegExp = /<(\\/?)\\s*([^\\s>]+)([^>]*?)\\s*(\\/?)>/\n const attrRegExp = /([^\\s=]+)(?:=\"([^\"]*)\"|=([^\"\\s]+))?/g\n\n interface Token {\n tag: string\n close: string\n empty: string\n attrs: Dict<string>\n }\n\n export function parse(source: string): Element[]\n export function parse(source: string, fragment: true): Element\n export function parse(source: string, fragment = false) {\n const tokens: (string | Token)[] = []\n let tagCap: RegExpExecArray\n while ((tagCap = tagRegExp.exec(source))) {\n if (tagCap.index) {\n tokens.push(unescape(source.slice(0, tagCap.index)))\n }\n const [_, close, tag, attrs, empty] = tagCap\n const token: Token = { tag, close, empty, attrs: {} }\n let attrCap: RegExpExecArray\n while ((attrCap = attrRegExp.exec(attrs))) {\n const [_, key, v1 = '', v2 = v1] = attrCap\n token.attrs[camelize(key)] = unescape(v2)\n }\n tokens.push(token)\n source = source.slice(tagCap.index + tagCap[0].length)\n }\n if (source) tokens.push(unescape(source))\n const stack = [Element(null)]\n for (const token of tokens) {\n if (typeof token === 'string') {\n stack[0].children.push(Element('text', { content: token }))\n } else if (token.close) {\n stack.shift()\n } else {\n const element = Element(token.tag, token.attrs)\n stack[0].children.push(element)\n if (!token.empty) stack.unshift(element)\n }\n }\n const root = stack[stack.length - 1]\n return fragment ? root : root.children\n }\n\n export function transform(source: string, rules: Dict<Transformer>): string\n export function transform(source: Element[], rules: Dict<Transformer>): Element[]\n export function transform(source: string | Element[], rules: Dict<Transformer>) {\n const elements = typeof source === 'string' ? parse(source) : source\n const output: Element[] = []\n elements.forEach((element, index, elements) => {\n let result = rules[element.type] ?? rules.default ?? true\n if (typeof result === 'function') {\n result = result(element.attrs, index, elements)\n }\n if (result === true) {\n const { type, attrs, children } = element\n output.push(Element(type, attrs, transform(children, rules)))\n } else if (result !== false) {\n output.push(...toElementArray(result))\n }\n })\n return typeof source === 'string' ? output.join('') : output\n }\n\n export async function transformAsync(source: string, rules: Dict<AsyncTransformer>): Promise<string>\n export async function transformAsync(source: Element[], rules: Dict<AsyncTransformer>): Promise<Element[]>\n export async function transformAsync(source: string | Element[], rules: Dict<AsyncTransformer>) {\n const elements = typeof source === 'string' ? parse(source) : source\n const children = (await Promise.all(elements.map(async (element, index, elements) => {\n let result = rules[element.type] ?? rules.default ?? true\n if (typeof result === 'function') {\n result = await result(element.attrs, index, elements)\n }\n if (result === true) {\n const { type, attrs, children } = element\n return [Element(type, attrs, await transformAsync(children, rules))]\n } else if (result !== false) {\n return toElementArray(result)\n } else {\n return []\n }\n }))).flat(1)\n return typeof source === 'string' ? children.join('') : children\n }\n\n /** @deprecated use `elements.join('')` instead */\n export function join(elements: Element[]) {\n return elements.join('')\n }\n\n export type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict<any>]) => Element\n\n function createFactory<R extends any[] = any[]>(type: string, ...keys: string[]): Factory<R> {\n return (...args: any[]) => {\n const element = Element(type)\n keys.forEach((key, index) => {\n if (!isNullable(args[index])) {\n element.attrs[key] = args[index]\n }\n })\n if (args[keys.length]) {\n Object.assign(element.attrs, args[keys.length])\n }\n return element\n }\n }\n\n function createAssetFactory(type: string): Factory<[data: string | Buffer | ArrayBuffer]> {\n return (value, attrs = {}) => {\n if (isType('Buffer', value)) {\n value = 'base64://' + value.toString('base64')\n } else if (isType('ArrayBuffer', value)) {\n value = 'base64://' + Buffer.from(value).toString('base64')\n }\n return Element(type, { ...attrs, url: value })\n }\n }\n\n export const at = createFactory<[id: any]>('at', 'id')\n export const sharp = createFactory<[id: any]>('sharp', 'id')\n export const quote = createFactory<[id: any]>('quote', 'id')\n export const image = createAssetFactory('image')\n export const video = createAssetFactory('video')\n export const audio = createAssetFactory('audio')\n export const file = createAssetFactory('file')\n}\n\nexport = Element\n", "type Global = NodeJS.Global & Window & typeof globalThis\n\ntype GlobalClass = {\n [K in keyof Global]: Global[K] extends new (...args: any[]) => infer T ? T : never\n}\n\nconst root: any = typeof self !== 'undefined' ? self : global\n\nexport function isType<K extends keyof GlobalClass>(type: K, value: any): value is GlobalClass[K] {\n return type in root && value instanceof root[type]\n || Object.prototype.toString.call(value).slice(8, -1) === type\n}\n"],
|
|
5
|
+
"mappings": ";;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AAU;AAC3B,YAAI,KAAC,4BAAW,KAAK,MAAM,GAAG;AAC5B,kBAAQ,MAAM,OAAO,KAAK;AAAA,QAC5B;AAAA,MACF,CAAC;AACD,UAAI,KAAK,KAAK,SAAS;AACrB,eAAO,OAAO,QAAQ,OAAO,KAAK,KAAK,OAAO;AAAA,MAChD;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAbS;AAeT,WAAS,mBAAmB,MAA8D;AACxF,WAAO,CAAC,OAAO,QAAQ,CAAC,MAAM;AAC5B,UAAI,OAAO,UAAU,KAAK,GAAG;AAC3B,gBAAQ,cAAc,MAAM,SAAS,QAAQ;AAAA,MAC/C,WAAW,OAAO,eAAe,KAAK,GAAG;AACvC,gBAAQ,cAAc,OAAO,KAAK,KAAK,EAAE,SAAS,QAAQ;AAAA,MAC5D;AACA,aAAOA,SAAQ,MAAM,EAAE,GAAG,OAAO,KAAK,MAAM,CAAC;AAAA,IAC/C;AAAA,EACF;AATS;AAWF,EAAMA,SAAA,KAAK,cAAyB,MAAM,IAAI;AAC9C,EAAMA,SAAA,QAAQ,cAAyB,SAAS,IAAI;AACpD,EAAMA,SAAA,QAAQ,cAAyB,SAAS,IAAI;AACpD,EAAMA,SAAA,QAAQ,mBAAmB,OAAO;AACxC,EAAMA,SAAA,QAAQ,mBAAmB,OAAO;AACxC,EAAMA,SAAA,QAAQ,mBAAmB,OAAO;AACxC,EAAMA,SAAA,OAAO,mBAAmB,MAAM;AAAA,GAnOrC;AAsOV,iBAAS;",
|
|
6
|
+
"names": ["Element", "_", "root", "elements", "children"]
|
|
7
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Awaitable, Dict } from 'cosmokit';
|
|
3
|
+
declare const kElement: unique symbol;
|
|
4
|
+
interface Element {
|
|
5
|
+
[kElement]: true;
|
|
6
|
+
type: string;
|
|
7
|
+
attrs: Dict<string>;
|
|
8
|
+
/** @deprecated use `attrs` instead */
|
|
9
|
+
data: Dict<string>;
|
|
10
|
+
children: Element[];
|
|
11
|
+
toString(strip?: boolean): string;
|
|
12
|
+
}
|
|
13
|
+
declare function Element(type: string, children?: Element.Content): Element;
|
|
14
|
+
declare function Element(type: string, attrs: Dict<any>, children?: Element.Content): Element;
|
|
15
|
+
declare namespace Element {
|
|
16
|
+
export type Content = string | Element | (string | Element)[];
|
|
17
|
+
export type Transformer = boolean | Content | ((attrs: Dict<string>, index: number, array: Element[]) => boolean | Content);
|
|
18
|
+
export type AsyncTransformer = boolean | Content | ((attrs: Dict<string>, index: number, array: Element[]) => Awaitable<boolean | Content>);
|
|
19
|
+
export function normalize(source: string | Element): Element;
|
|
20
|
+
export function escape(source: string, inline?: boolean): string;
|
|
21
|
+
export function unescape(source: string): string;
|
|
22
|
+
export interface FindOptions {
|
|
23
|
+
type?: string;
|
|
24
|
+
caret?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** @deprecated use `Element.select()` instead */
|
|
27
|
+
export function from(source: string, options?: FindOptions): Element;
|
|
28
|
+
type Combinator = ' ' | '>' | '+' | '~';
|
|
29
|
+
export interface Selector {
|
|
30
|
+
type: string;
|
|
31
|
+
combinator: Combinator;
|
|
32
|
+
}
|
|
33
|
+
export function parseSelector(input: string): Selector[][];
|
|
34
|
+
export function select(source: string | Element[], query: string | Selector[][]): Element[];
|
|
35
|
+
export function parse(source: string): Element[];
|
|
36
|
+
export function parse(source: string, fragment: true): Element;
|
|
37
|
+
export function transform(source: string, rules: Dict<Transformer>): string;
|
|
38
|
+
export function transform(source: Element[], rules: Dict<Transformer>): Element[];
|
|
39
|
+
export function transformAsync(source: string, rules: Dict<AsyncTransformer>): Promise<string>;
|
|
40
|
+
export function transformAsync(source: Element[], rules: Dict<AsyncTransformer>): Promise<Element[]>;
|
|
41
|
+
/** @deprecated use `elements.join('')` instead */
|
|
42
|
+
export function join(elements: Element[]): string;
|
|
43
|
+
export type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict<any>]) => Element;
|
|
44
|
+
export const at: Factory<[id: any]>;
|
|
45
|
+
export const sharp: Factory<[id: any]>;
|
|
46
|
+
export const quote: Factory<[id: any]>;
|
|
47
|
+
export const image: Factory<[data: string | Buffer | ArrayBuffer]>;
|
|
48
|
+
export const video: Factory<[data: string | Buffer | ArrayBuffer]>;
|
|
49
|
+
export const audio: Factory<[data: string | Buffer | ArrayBuffer]>;
|
|
50
|
+
export const file: Factory<[data: string | Buffer | ArrayBuffer]>;
|
|
51
|
+
export {};
|
|
52
|
+
}
|
|
53
|
+
export = Element;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __esm = (fn, res) => function __init() {
|
|
5
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
6
|
+
};
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// satori/packages/element/src/utils.ts
|
|
12
|
+
function isType(type, value) {
|
|
13
|
+
return type in root && value instanceof root[type] || Object.prototype.toString.call(value).slice(8, -1) === type;
|
|
14
|
+
}
|
|
15
|
+
var root;
|
|
16
|
+
var init_utils = __esm({
|
|
17
|
+
"satori/packages/element/src/utils.ts"() {
|
|
18
|
+
root = typeof self !== "undefined" ? self : global;
|
|
19
|
+
__name(isType, "isType");
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// satori/packages/element/src/index.ts
|
|
24
|
+
import { camelize, capitalize, defineProperty, hyphenate, isNullable } from "cosmokit";
|
|
25
|
+
var require_src = __commonJS({
|
|
26
|
+
"satori/packages/element/src/index.ts"(exports, module) {
|
|
27
|
+
init_utils();
|
|
28
|
+
var kElement = Symbol("element");
|
|
29
|
+
function isElement(source) {
|
|
30
|
+
return source && typeof source === "object" && source[kElement];
|
|
31
|
+
}
|
|
32
|
+
__name(isElement, "isElement");
|
|
33
|
+
function toElement(content) {
|
|
34
|
+
if (typeof content !== "string")
|
|
35
|
+
return content;
|
|
36
|
+
return Element("text", { content });
|
|
37
|
+
}
|
|
38
|
+
__name(toElement, "toElement");
|
|
39
|
+
function toElementArray(input) {
|
|
40
|
+
if (Array.isArray(input)) {
|
|
41
|
+
return input.map(toElement);
|
|
42
|
+
} else if (typeof input === "string") {
|
|
43
|
+
return [toElement(input)];
|
|
44
|
+
} else if (!input.type) {
|
|
45
|
+
return input.children;
|
|
46
|
+
} else {
|
|
47
|
+
return [input];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
__name(toElementArray, "toElementArray");
|
|
51
|
+
var ElementConstructor = class {
|
|
52
|
+
get data() {
|
|
53
|
+
return this.attrs;
|
|
54
|
+
}
|
|
55
|
+
toString(strip = false) {
|
|
56
|
+
const inner = this.children.map((child) => child.toString(strip)).join("");
|
|
57
|
+
if (!this.type || strip)
|
|
58
|
+
return inner;
|
|
59
|
+
if (this.type === "text")
|
|
60
|
+
return Element.escape(this.attrs.content);
|
|
61
|
+
const attrs = Object.entries(this.attrs).map(([key, value]) => {
|
|
62
|
+
if (isNullable(value))
|
|
63
|
+
return "";
|
|
64
|
+
key = hyphenate(key);
|
|
65
|
+
if (value === "")
|
|
66
|
+
return ` ${key}`;
|
|
67
|
+
return ` ${key}="${Element.escape(value, true)}"`;
|
|
68
|
+
}).join("");
|
|
69
|
+
if (!this.children.length)
|
|
70
|
+
return `<${this.type}${attrs}/>`;
|
|
71
|
+
return `<${this.type}${attrs}>${inner}</${this.type}>`;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
__name(ElementConstructor, "ElementConstructor");
|
|
75
|
+
defineProperty(ElementConstructor, "name", "Element");
|
|
76
|
+
function Element(type, ...args) {
|
|
77
|
+
const el = Object.create(ElementConstructor.prototype);
|
|
78
|
+
let attrs = {}, children = [];
|
|
79
|
+
if (args[0] && typeof args[0] === "object" && !isElement(args[0]) && !Array.isArray(args[0])) {
|
|
80
|
+
for (const [key, value] of Object.entries(args.shift())) {
|
|
81
|
+
if (isNullable(value))
|
|
82
|
+
continue;
|
|
83
|
+
if (value === true) {
|
|
84
|
+
attrs[key] = "";
|
|
85
|
+
} else if (value === false) {
|
|
86
|
+
attrs["no" + capitalize(key)] = "";
|
|
87
|
+
} else {
|
|
88
|
+
attrs[key] = "" + value;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (args[0])
|
|
93
|
+
children = toElementArray(args[0]);
|
|
94
|
+
return Object.assign(el, { type, attrs, children });
|
|
95
|
+
}
|
|
96
|
+
__name(Element, "Element");
|
|
97
|
+
((Element2) => {
|
|
98
|
+
function normalize(source) {
|
|
99
|
+
if (typeof source !== "string")
|
|
100
|
+
return Element2(null, source);
|
|
101
|
+
return Element2.parse(source, true);
|
|
102
|
+
}
|
|
103
|
+
Element2.normalize = normalize;
|
|
104
|
+
__name(normalize, "normalize");
|
|
105
|
+
function escape(source, inline = false) {
|
|
106
|
+
const result = source.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
107
|
+
return inline ? result.replace(/"/g, """) : result;
|
|
108
|
+
}
|
|
109
|
+
Element2.escape = escape;
|
|
110
|
+
__name(escape, "escape");
|
|
111
|
+
function unescape(source) {
|
|
112
|
+
return source.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/&/g, "&");
|
|
113
|
+
}
|
|
114
|
+
Element2.unescape = unescape;
|
|
115
|
+
__name(unescape, "unescape");
|
|
116
|
+
function from(source, options = {}) {
|
|
117
|
+
const elements = parse(source);
|
|
118
|
+
if (options.caret) {
|
|
119
|
+
if (options.type && elements[0]?.type !== options.type)
|
|
120
|
+
return;
|
|
121
|
+
return elements[0];
|
|
122
|
+
}
|
|
123
|
+
return select(elements, options.type || "*")[0];
|
|
124
|
+
}
|
|
125
|
+
Element2.from = from;
|
|
126
|
+
__name(from, "from");
|
|
127
|
+
const combRegExp = / *([ >+~]) */g;
|
|
128
|
+
function parseSelector(input) {
|
|
129
|
+
return input.split(",").map((query) => {
|
|
130
|
+
const selectors = [];
|
|
131
|
+
query = query.trim();
|
|
132
|
+
let combCap, combinator = " ";
|
|
133
|
+
while (combCap = combRegExp.exec(query)) {
|
|
134
|
+
selectors.push({ type: query.slice(0, combCap.index), combinator });
|
|
135
|
+
combinator = combCap[1];
|
|
136
|
+
query = query.slice(combCap.index + combCap[0].length);
|
|
137
|
+
}
|
|
138
|
+
selectors.push({ type: query, combinator });
|
|
139
|
+
return selectors;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
Element2.parseSelector = parseSelector;
|
|
143
|
+
__name(parseSelector, "parseSelector");
|
|
144
|
+
function select(source, query) {
|
|
145
|
+
if (typeof source === "string")
|
|
146
|
+
source = parse(source);
|
|
147
|
+
if (typeof query === "string")
|
|
148
|
+
query = parseSelector(query);
|
|
149
|
+
if (!query.length)
|
|
150
|
+
return;
|
|
151
|
+
let adjacent = [];
|
|
152
|
+
const results = [];
|
|
153
|
+
for (const [index, element] of source.entries()) {
|
|
154
|
+
const inner = [];
|
|
155
|
+
const local = [...query, ...adjacent];
|
|
156
|
+
adjacent = [];
|
|
157
|
+
let matched = false;
|
|
158
|
+
for (const group of local) {
|
|
159
|
+
const { type, combinator } = group[0];
|
|
160
|
+
if (type === element.type || type === "*") {
|
|
161
|
+
if (group.length === 1) {
|
|
162
|
+
matched = true;
|
|
163
|
+
} else if ([" ", ">"].includes(group[1].combinator)) {
|
|
164
|
+
inner.push(group.slice(1));
|
|
165
|
+
} else if (group[1].combinator === "+") {
|
|
166
|
+
adjacent.push(group.slice(1));
|
|
167
|
+
} else {
|
|
168
|
+
query.push(group.slice(1));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (combinator === " ") {
|
|
172
|
+
inner.push(group);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (matched)
|
|
176
|
+
results.push(source[index]);
|
|
177
|
+
results.push(...select(element.children, inner));
|
|
178
|
+
}
|
|
179
|
+
return results;
|
|
180
|
+
}
|
|
181
|
+
Element2.select = select;
|
|
182
|
+
__name(select, "select");
|
|
183
|
+
const tagRegExp = /<(\/?)\s*([^\s>]+)([^>]*?)\s*(\/?)>/;
|
|
184
|
+
const attrRegExp = /([^\s=]+)(?:="([^"]*)"|=([^"\s]+))?/g;
|
|
185
|
+
function parse(source, fragment = false) {
|
|
186
|
+
const tokens = [];
|
|
187
|
+
let tagCap;
|
|
188
|
+
while (tagCap = tagRegExp.exec(source)) {
|
|
189
|
+
if (tagCap.index) {
|
|
190
|
+
tokens.push(unescape(source.slice(0, tagCap.index)));
|
|
191
|
+
}
|
|
192
|
+
const [_, close, tag, attrs, empty] = tagCap;
|
|
193
|
+
const token = { tag, close, empty, attrs: {} };
|
|
194
|
+
let attrCap;
|
|
195
|
+
while (attrCap = attrRegExp.exec(attrs)) {
|
|
196
|
+
const [_2, key, v1 = "", v2 = v1] = attrCap;
|
|
197
|
+
token.attrs[camelize(key)] = unescape(v2);
|
|
198
|
+
}
|
|
199
|
+
tokens.push(token);
|
|
200
|
+
source = source.slice(tagCap.index + tagCap[0].length);
|
|
201
|
+
}
|
|
202
|
+
if (source)
|
|
203
|
+
tokens.push(unescape(source));
|
|
204
|
+
const stack = [Element2(null)];
|
|
205
|
+
for (const token of tokens) {
|
|
206
|
+
if (typeof token === "string") {
|
|
207
|
+
stack[0].children.push(Element2("text", { content: token }));
|
|
208
|
+
} else if (token.close) {
|
|
209
|
+
stack.shift();
|
|
210
|
+
} else {
|
|
211
|
+
const element = Element2(token.tag, token.attrs);
|
|
212
|
+
stack[0].children.push(element);
|
|
213
|
+
if (!token.empty)
|
|
214
|
+
stack.unshift(element);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const root2 = stack[stack.length - 1];
|
|
218
|
+
return fragment ? root2 : root2.children;
|
|
219
|
+
}
|
|
220
|
+
Element2.parse = parse;
|
|
221
|
+
__name(parse, "parse");
|
|
222
|
+
function transform(source, rules) {
|
|
223
|
+
const elements = typeof source === "string" ? parse(source) : source;
|
|
224
|
+
const output = [];
|
|
225
|
+
elements.forEach((element, index, elements2) => {
|
|
226
|
+
let result = rules[element.type] ?? rules.default ?? true;
|
|
227
|
+
if (typeof result === "function") {
|
|
228
|
+
result = result(element.attrs, index, elements2);
|
|
229
|
+
}
|
|
230
|
+
if (result === true) {
|
|
231
|
+
const { type, attrs, children } = element;
|
|
232
|
+
output.push(Element2(type, attrs, transform(children, rules)));
|
|
233
|
+
} else if (result !== false) {
|
|
234
|
+
output.push(...toElementArray(result));
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
return typeof source === "string" ? output.join("") : output;
|
|
238
|
+
}
|
|
239
|
+
Element2.transform = transform;
|
|
240
|
+
__name(transform, "transform");
|
|
241
|
+
async function transformAsync(source, rules) {
|
|
242
|
+
const elements = typeof source === "string" ? parse(source) : source;
|
|
243
|
+
const children = (await Promise.all(elements.map(async (element, index, elements2) => {
|
|
244
|
+
let result = rules[element.type] ?? rules.default ?? true;
|
|
245
|
+
if (typeof result === "function") {
|
|
246
|
+
result = await result(element.attrs, index, elements2);
|
|
247
|
+
}
|
|
248
|
+
if (result === true) {
|
|
249
|
+
const { type, attrs, children: children2 } = element;
|
|
250
|
+
return [Element2(type, attrs, await transformAsync(children2, rules))];
|
|
251
|
+
} else if (result !== false) {
|
|
252
|
+
return toElementArray(result);
|
|
253
|
+
} else {
|
|
254
|
+
return [];
|
|
255
|
+
}
|
|
256
|
+
}))).flat(1);
|
|
257
|
+
return typeof source === "string" ? children.join("") : children;
|
|
258
|
+
}
|
|
259
|
+
Element2.transformAsync = transformAsync;
|
|
260
|
+
__name(transformAsync, "transformAsync");
|
|
261
|
+
function join(elements) {
|
|
262
|
+
return elements.join("");
|
|
263
|
+
}
|
|
264
|
+
Element2.join = join;
|
|
265
|
+
__name(join, "join");
|
|
266
|
+
function createFactory(type, ...keys) {
|
|
267
|
+
return (...args) => {
|
|
268
|
+
const element = Element2(type);
|
|
269
|
+
keys.forEach((key, index) => {
|
|
270
|
+
if (!isNullable(args[index])) {
|
|
271
|
+
element.attrs[key] = args[index];
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
if (args[keys.length]) {
|
|
275
|
+
Object.assign(element.attrs, args[keys.length]);
|
|
276
|
+
}
|
|
277
|
+
return element;
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
__name(createFactory, "createFactory");
|
|
281
|
+
function createAssetFactory(type) {
|
|
282
|
+
return (value, attrs = {}) => {
|
|
283
|
+
if (isType("Buffer", value)) {
|
|
284
|
+
value = "base64://" + value.toString("base64");
|
|
285
|
+
} else if (isType("ArrayBuffer", value)) {
|
|
286
|
+
value = "base64://" + Buffer.from(value).toString("base64");
|
|
287
|
+
}
|
|
288
|
+
return Element2(type, { ...attrs, url: value });
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
__name(createAssetFactory, "createAssetFactory");
|
|
292
|
+
Element2.at = createFactory("at", "id");
|
|
293
|
+
Element2.sharp = createFactory("sharp", "id");
|
|
294
|
+
Element2.quote = createFactory("quote", "id");
|
|
295
|
+
Element2.image = createAssetFactory("image");
|
|
296
|
+
Element2.video = createAssetFactory("video");
|
|
297
|
+
Element2.audio = createAssetFactory("audio");
|
|
298
|
+
Element2.file = createAssetFactory("file");
|
|
299
|
+
})(Element || (Element = {}));
|
|
300
|
+
module.exports = Element;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
export default require_src();
|
|
304
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/utils.ts", "../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["type Global = NodeJS.Global & Window & typeof globalThis\n\ntype GlobalClass = {\n [K in keyof Global]: Global[K] extends new (...args: any[]) => infer T ? T : never\n}\n\nconst root: any = typeof self !== 'undefined' ? self : global\n\nexport function isType<K extends keyof GlobalClass>(type: K, value: any): value is GlobalClass[K] {\n return type in root && value instanceof root[type]\n || Object.prototype.toString.call(value).slice(8, -1) === type\n}\n", "import { Awaitable, camelize, capitalize, defineProperty, Dict, hyphenate, isNullable } from 'cosmokit'\nimport { isType } from './utils'\n\nconst kElement = Symbol('element')\n\nfunction isElement(source: any): source is Element {\n return source && typeof source === 'object' && source[kElement]\n}\n\nfunction toElement(content: string | Element) {\n if (typeof content !== 'string') return content\n return Element('text', { content })\n}\n\nfunction toElementArray(input: Element.Content) {\n if (Array.isArray(input)) {\n return input.map(toElement)\n } else if (typeof input === 'string') {\n return [toElement(input)]\n } else if (!input.type) {\n return input.children\n } else {\n return [input]\n }\n}\n\ninterface Element {\n [kElement]: true\n type: string\n attrs: Dict<string>\n /** @deprecated use `attrs` instead */\n data: Dict<string>\n children: Element[]\n toString(strip?: boolean): string\n}\n\ninterface ElementConstructor extends Element {}\n\nclass ElementConstructor {\n get data() {\n return this.attrs\n }\n\n toString(strip = false) {\n const inner = this.children.map(child => child.toString(strip)).join('')\n if (!this.type || strip) return inner\n if (this.type === 'text') return Element.escape(this.attrs.content)\n const attrs = Object.entries(this.attrs).map(([key, value]) => {\n if (isNullable(value)) return ''\n key = hyphenate(key)\n if (value === '') return ` ${key}`\n return ` ${key}=\"${Element.escape(value, true)}\"`\n }).join('')\n if (!this.children.length) return `<${this.type}${attrs}/>`\n return `<${this.type}${attrs}>${inner}</${this.type}>`\n }\n}\n\ndefineProperty(ElementConstructor, 'name', 'Element')\n\nfunction Element(type: string, children?: Element.Content): Element\nfunction Element(type: string, attrs: Dict<any>, children?: Element.Content): Element\nfunction Element(type: string, ...args: any[]) {\n const el = Object.create(ElementConstructor.prototype)\n let attrs: Dict<string> = {}, children: Element[] = []\n if (args[0] && typeof args[0] === 'object' && !isElement(args[0]) && !Array.isArray(args[0])) {\n for (const [key, value] of Object.entries(args.shift())) {\n if (isNullable(value)) continue\n if (value === true) {\n attrs[key] = ''\n } else if (value === false) {\n attrs['no' + capitalize(key)] = ''\n } else {\n attrs[key] = '' + value\n }\n }\n }\n if (args[0]) children = toElementArray(args[0])\n return Object.assign(el, { type, attrs, children })\n}\n\nnamespace Element {\n export type Content = string | Element | (string | Element)[]\n export type Transformer = boolean | Content | ((attrs: Dict<string>, index: number, array: Element[]) => boolean | Content)\n export type AsyncTransformer = boolean | Content | ((attrs: Dict<string>, index: number, array: Element[]) => Awaitable<boolean | Content>)\n\n export function normalize(source: string | Element) {\n if (typeof source !== 'string') return Element(null, source)\n return Element.parse(source, true)\n }\n\n export function escape(source: string, inline = false) {\n const result = source\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n return inline\n ? result.replace(/\"/g, '"')\n : result\n }\n\n export function unescape(source: string) {\n return source\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/&/g, '&')\n }\n\n export interface FindOptions {\n type?: string\n caret?: boolean\n }\n\n /** @deprecated use `Element.select()` instead */\n export function from(source: string, options: FindOptions = {}): Element {\n const elements = parse(source)\n if (options.caret) {\n if (options.type && elements[0]?.type !== options.type) return\n return elements[0]\n }\n return select(elements, options.type || '*')[0]\n }\n\n type Combinator = ' ' | '>' | '+' | '~'\n\n export interface Selector {\n type: string\n combinator: Combinator\n }\n\n const combRegExp = / *([ >+~]) */g\n\n export function parseSelector(input: string): Selector[][] {\n return input.split(',').map((query) => {\n const selectors: Selector[] = []\n query = query.trim()\n let combCap: RegExpExecArray, combinator: Combinator = ' '\n while ((combCap = combRegExp.exec(query))) {\n selectors.push({ type: query.slice(0, combCap.index), combinator })\n combinator = combCap[1] as Combinator\n query = query.slice(combCap.index + combCap[0].length)\n }\n selectors.push({ type: query, combinator })\n return selectors\n })\n }\n\n export function select(source: string | Element[], query: string | Selector[][]): Element[] {\n if (typeof source === 'string') source = parse(source)\n if (typeof query === 'string') query = parseSelector(query)\n if (!query.length) return\n let adjacent: Selector[][] = []\n const results: Element[] = []\n for (const [index, element] of source.entries()) {\n const inner: Selector[][] = []\n const local = [...query, ...adjacent]\n adjacent = []\n let matched = false\n for (const group of local) {\n const { type, combinator } = group[0]\n if (type === element.type || type === '*') {\n if (group.length === 1) {\n matched = true\n } else if ([' ', '>'].includes(group[1].combinator)) {\n inner.push(group.slice(1))\n } else if (group[1].combinator === '+') {\n adjacent.push(group.slice(1))\n } else {\n query.push(group.slice(1))\n }\n }\n if (combinator === ' ') {\n inner.push(group)\n }\n }\n if (matched) results.push(source[index])\n results.push(...select(element.children, inner))\n }\n return results\n }\n\n const tagRegExp = /<(\\/?)\\s*([^\\s>]+)([^>]*?)\\s*(\\/?)>/\n const attrRegExp = /([^\\s=]+)(?:=\"([^\"]*)\"|=([^\"\\s]+))?/g\n\n interface Token {\n tag: string\n close: string\n empty: string\n attrs: Dict<string>\n }\n\n export function parse(source: string): Element[]\n export function parse(source: string, fragment: true): Element\n export function parse(source: string, fragment = false) {\n const tokens: (string | Token)[] = []\n let tagCap: RegExpExecArray\n while ((tagCap = tagRegExp.exec(source))) {\n if (tagCap.index) {\n tokens.push(unescape(source.slice(0, tagCap.index)))\n }\n const [_, close, tag, attrs, empty] = tagCap\n const token: Token = { tag, close, empty, attrs: {} }\n let attrCap: RegExpExecArray\n while ((attrCap = attrRegExp.exec(attrs))) {\n const [_, key, v1 = '', v2 = v1] = attrCap\n token.attrs[camelize(key)] = unescape(v2)\n }\n tokens.push(token)\n source = source.slice(tagCap.index + tagCap[0].length)\n }\n if (source) tokens.push(unescape(source))\n const stack = [Element(null)]\n for (const token of tokens) {\n if (typeof token === 'string') {\n stack[0].children.push(Element('text', { content: token }))\n } else if (token.close) {\n stack.shift()\n } else {\n const element = Element(token.tag, token.attrs)\n stack[0].children.push(element)\n if (!token.empty) stack.unshift(element)\n }\n }\n const root = stack[stack.length - 1]\n return fragment ? root : root.children\n }\n\n export function transform(source: string, rules: Dict<Transformer>): string\n export function transform(source: Element[], rules: Dict<Transformer>): Element[]\n export function transform(source: string | Element[], rules: Dict<Transformer>) {\n const elements = typeof source === 'string' ? parse(source) : source\n const output: Element[] = []\n elements.forEach((element, index, elements) => {\n let result = rules[element.type] ?? rules.default ?? true\n if (typeof result === 'function') {\n result = result(element.attrs, index, elements)\n }\n if (result === true) {\n const { type, attrs, children } = element\n output.push(Element(type, attrs, transform(children, rules)))\n } else if (result !== false) {\n output.push(...toElementArray(result))\n }\n })\n return typeof source === 'string' ? output.join('') : output\n }\n\n export async function transformAsync(source: string, rules: Dict<AsyncTransformer>): Promise<string>\n export async function transformAsync(source: Element[], rules: Dict<AsyncTransformer>): Promise<Element[]>\n export async function transformAsync(source: string | Element[], rules: Dict<AsyncTransformer>) {\n const elements = typeof source === 'string' ? parse(source) : source\n const children = (await Promise.all(elements.map(async (element, index, elements) => {\n let result = rules[element.type] ?? rules.default ?? true\n if (typeof result === 'function') {\n result = await result(element.attrs, index, elements)\n }\n if (result === true) {\n const { type, attrs, children } = element\n return [Element(type, attrs, await transformAsync(children, rules))]\n } else if (result !== false) {\n return toElementArray(result)\n } else {\n return []\n }\n }))).flat(1)\n return typeof source === 'string' ? children.join('') : children\n }\n\n /** @deprecated use `elements.join('')` instead */\n export function join(elements: Element[]) {\n return elements.join('')\n }\n\n export type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict<any>]) => Element\n\n function createFactory<R extends any[] = any[]>(type: string, ...keys: string[]): Factory<R> {\n return (...args: any[]) => {\n const element = Element(type)\n keys.forEach((key, index) => {\n if (!isNullable(args[index])) {\n element.attrs[key] = args[index]\n }\n })\n if (args[keys.length]) {\n Object.assign(element.attrs, args[keys.length])\n }\n return element\n }\n }\n\n function createAssetFactory(type: string): Factory<[data: string | Buffer | ArrayBuffer]> {\n return (value, attrs = {}) => {\n if (isType('Buffer', value)) {\n value = 'base64://' + value.toString('base64')\n } else if (isType('ArrayBuffer', value)) {\n value = 'base64://' + Buffer.from(value).toString('base64')\n }\n return Element(type, { ...attrs, url: value })\n }\n }\n\n export const at = createFactory<[id: any]>('at', 'id')\n export const sharp = createFactory<[id: any]>('sharp', 'id')\n export const quote = createFactory<[id: any]>('quote', 'id')\n export const image = createAssetFactory('image')\n export const video = createAssetFactory('video')\n export const audio = createAssetFactory('audio')\n export const file = createAssetFactory('file')\n}\n\nexport = Element\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;AAQO,SAAS,OAAoC,MAAS,OAAqC;AAChG,SAAO,QAAQ,QAAQ,iBAAiB,KAAK,SACxC,OAAO,UAAU,SAAS,KAAK,KAAK,EAAE,MAAM,GAAG,EAAE,MAAM;AAC9D;AAXA,IAMM;AANN;AAAA;AAMA,IAAM,OAAY,OAAO,SAAS,cAAc,OAAO;AAEvC;AAAA;AAAA;;;ACRhB,SAAoB,UAAU,YAAY,gBAAsB,WAAW,kBAAkB;AAA7F;AAAA;AACA;AAEA,QAAM,WAAW,OAAO,SAAS;AAEjC,aAAS,UAAU,QAAgC;AACjD,aAAO,UAAU,OAAO,WAAW,YAAY,OAAO;AAAA,IACxD;AAFS;AAIT,aAAS,UAAU,SAA2B;AAC5C,UAAI,OAAO,YAAY;AAAU,eAAO;AACxC,aAAO,QAAQ,QAAQ,EAAE,QAAQ,CAAC;AAAA,IACpC;AAHS;AAKT,aAAS,eAAe,OAAwB;AAC9C,UAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,eAAO,MAAM,IAAI,SAAS;AAAA,MAC5B,WAAW,OAAO,UAAU,UAAU;AACpC,eAAO,CAAC,UAAU,KAAK,CAAC;AAAA,MAC1B,WAAW,CAAC,MAAM,MAAM;AACtB,eAAO,MAAM;AAAA,MACf,OAAO;AACL,eAAO,CAAC,KAAK;AAAA,MACf;AAAA,IACF;AAVS;AAwBT,QAAM,qBAAN,MAAyB;AAAA,MACvB,IAAI,OAAO;AAC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mBAAmB,OAAO;AACxC,MAAMA,SAAA,QAAQ,mBAAmB,OAAO;AACxC,MAAMA,SAAA,OAAO,mBAAmB,MAAM;AAAA,OAnOrC;AAsOV,qBAAS;AAAA;AAAA;",
|
|
6
|
+
"names": ["Element", "_", "root", "elements", "children"]
|
|
7
|
+
}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="mocha" />
|
|
2
|
+
declare type Global = NodeJS.Global & Window & typeof globalThis;
|
|
3
|
+
declare type GlobalClass = {
|
|
4
|
+
[K in keyof Global]: Global[K] extends new (...args: any[]) => infer T ? T : never;
|
|
5
|
+
};
|
|
6
|
+
export declare function isType<K extends keyof GlobalClass>(type: K, value: any): value is GlobalClass[K];
|
|
7
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@satorijs/element",
|
|
3
|
+
"description": "Element Manipulation",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"main": "lib/index.cjs",
|
|
6
|
+
"module": "lib/index.mjs",
|
|
7
|
+
"typings": "lib/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib"
|
|
10
|
+
],
|
|
11
|
+
"author": "Shigma <shigma10826@gmail.com>",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/satorijs/satori.git",
|
|
16
|
+
"directory": "packages/message"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/satorijs/satori/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://satori.js.org/",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"satori",
|
|
24
|
+
"element",
|
|
25
|
+
"segment",
|
|
26
|
+
"component",
|
|
27
|
+
"message",
|
|
28
|
+
"utilities"
|
|
29
|
+
],
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"chai-shape": "^1.0.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"cosmokit": "^1.2.1"
|
|
35
|
+
}
|
|
36
|
+
}
|