@satorijs/element 2.5.1 → 2.6.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/src/index.ts ADDED
@@ -0,0 +1,503 @@
1
+ import { arrayBufferToBase64, Awaitable, camelize, defineProperty, Dict, hyphenate, is, isNullable, makeArray } from 'cosmokit'
2
+
3
+ const kElement = Symbol.for('satori.element')
4
+
5
+ interface Element {
6
+ [kElement]: true
7
+ type: string
8
+ attrs: Dict
9
+ /** @deprecated use `attrs` instead */
10
+ data: Dict
11
+ children: Element[]
12
+ source?: string
13
+ toString(strip?: boolean): string
14
+ }
15
+
16
+ interface ElementConstructor extends Element {}
17
+
18
+ class ElementConstructor {
19
+ get data() {
20
+ return this.attrs
21
+ }
22
+
23
+ getTagName() {
24
+ if (this.type === 'component') {
25
+ return this.attrs.is?.name ?? 'component'
26
+ } else {
27
+ return this.type
28
+ }
29
+ }
30
+
31
+ toAttrString() {
32
+ return Object.entries(this.attrs).map(([key, value]) => {
33
+ if (isNullable(value)) return ''
34
+ key = hyphenate(key)
35
+ if (value === true) return ` ${key}`
36
+ if (value === false) return ` no-${key}`
37
+ return ` ${key}="${Element.escape('' + value, true)}"`
38
+ }).join('')
39
+ }
40
+
41
+ toString(strip = false) {
42
+ if (this.type === 'text' && 'content' in this.attrs) {
43
+ return strip ? this.attrs.content : Element.escape(this.attrs.content)
44
+ }
45
+ const inner = this.children.map(child => child.toString(strip)).join('')
46
+ if (strip) return inner
47
+ const attrs = this.toAttrString()
48
+ const tag = this.getTagName()
49
+ if (!this.children.length) return `<${tag}${attrs}/>`
50
+ return `<${tag}${attrs}>${inner}</${tag}>`
51
+ }
52
+ }
53
+
54
+ defineProperty(ElementConstructor, 'name', 'Element')
55
+ defineProperty(ElementConstructor.prototype, kElement, true)
56
+
57
+ type RenderFunction = Element.Render<Element.Fragment, any>
58
+
59
+ function Element(type: string | RenderFunction, ...children: Element.Fragment[]): Element
60
+ function Element(type: string | RenderFunction, attrs: Dict, ...children: Element.Fragment[]): Element
61
+ function Element(type: string | RenderFunction, ...args: any[]) {
62
+ const el = Object.create(ElementConstructor.prototype)
63
+ const attrs: Dict = {}, children: Element[] = []
64
+ if (args[0] && typeof args[0] === 'object' && !Element.isElement(args[0]) && !Array.isArray(args[0])) {
65
+ const props = args.shift()
66
+ for (const [key, value] of Object.entries(props)) {
67
+ if (isNullable(value)) continue
68
+ // https://github.com/reactjs/rfcs/pull/107
69
+ if (key === 'children') {
70
+ args.push(...makeArray(value))
71
+ } else {
72
+ attrs[camelize(key)] = value
73
+ }
74
+ }
75
+ }
76
+ for (const child of args) {
77
+ children.push(...Element.toElementArray(child))
78
+ }
79
+ if (typeof type === 'function') {
80
+ attrs.is = type
81
+ type = 'component'
82
+ }
83
+ return Object.assign(el, { type, attrs, children })
84
+ }
85
+
86
+ // eslint-disable-next-line no-new-func
87
+ const evaluate = new Function('expr', 'context', `
88
+ try {
89
+ with (context) {
90
+ return eval(expr)
91
+ }
92
+ } catch {}
93
+ `) as ((expr: string, context: object) => string)
94
+
95
+ namespace Element {
96
+ export const jsx = Element
97
+ export const jsxs = Element
98
+ export const jsxDEV = Element
99
+ export const Fragment = 'template'
100
+
101
+ export type Fragment = string | Element | (string | Element)[]
102
+ export type Visit<T, S> = (element: Element, session: S) => T
103
+ export type Render<T, S> = (attrs: Dict, children: Element[], session: S) => T
104
+ export type SyncTransformer<S = never> = boolean | Fragment | Render<boolean | Fragment, S>
105
+ export type Transformer<S = never> = boolean | Fragment | Render<Awaitable<boolean | Fragment>, S>
106
+
107
+ type SyncVisitor<S> = Dict<SyncTransformer<S>> | Visit<boolean | Fragment, S>
108
+ type Visitor<S> = Dict<Transformer<S>> | Visit<Awaitable<boolean | Fragment>, S>
109
+
110
+ export function isElement(source: any): source is Element {
111
+ return source && typeof source === 'object' && source[kElement]
112
+ }
113
+
114
+ export function toElement(content: string | Element) {
115
+ if (typeof content === 'string' || typeof content === 'number' || typeof content === 'boolean') {
116
+ content = '' + content
117
+ if (content) return Element('text', { content })
118
+ } else if (isElement(content)) {
119
+ return content
120
+ } else if (!isNullable(content)) {
121
+ throw new TypeError(`Invalid content: ${content}`)
122
+ }
123
+ }
124
+
125
+ export function toElementArray(content: Element.Fragment) {
126
+ if (Array.isArray(content)) {
127
+ return content.map(toElement).filter(x => x)
128
+ } else {
129
+ return [toElement(content)].filter(x => x)
130
+ }
131
+ }
132
+
133
+ export function normalize(source: Fragment, context?: any) {
134
+ return typeof source === 'string' ? parse(source, context) : toElementArray(source)
135
+ }
136
+
137
+ export function escape(source: string, inline = false) {
138
+ const result = source
139
+ .replace(/&/g, '&amp;')
140
+ .replace(/</g, '&lt;')
141
+ .replace(/>/g, '&gt;')
142
+ return inline
143
+ ? result.replace(/"/g, '&quot;')
144
+ : result
145
+ }
146
+
147
+ export function unescape(source: string) {
148
+ return source
149
+ .replace(/&lt;/g, '<')
150
+ .replace(/&gt;/g, '>')
151
+ .replace(/&quot;/g, '"')
152
+ .replace(/&#(\d+);/g, (_, code) => code === '38' ? _ : String.fromCharCode(+code))
153
+ .replace(/&#x([0-9a-f]+);/gi, (_, code) => code === '26' ? _ : String.fromCharCode(parseInt(code, 16)))
154
+ .replace(/&(amp|#38|#x26);/g, '&')
155
+ }
156
+
157
+ export interface FindOptions {
158
+ type?: string
159
+ caret?: boolean
160
+ }
161
+
162
+ /** @deprecated use `Element.select()` instead */
163
+ export function from(source: string, options: FindOptions = {}): Element {
164
+ const elements = parse(source)
165
+ if (options.caret) {
166
+ if (options.type && elements[0]?.type !== options.type) return
167
+ return elements[0]
168
+ }
169
+ return select(elements, options.type || '*')[0]
170
+ }
171
+
172
+ type Combinator = ' ' | '>' | '+' | '~'
173
+
174
+ export interface Selector {
175
+ type: string
176
+ combinator: Combinator
177
+ }
178
+
179
+ const combRegExp = / *([ >+~]) */g
180
+
181
+ export function parseSelector(input: string): Selector[][] {
182
+ return input.split(',').map((query) => {
183
+ const selectors: Selector[] = []
184
+ query = query.trim()
185
+ let combCap: RegExpExecArray, combinator: Combinator = ' '
186
+ while ((combCap = combRegExp.exec(query))) {
187
+ selectors.push({ type: query.slice(0, combCap.index), combinator })
188
+ combinator = combCap[1] as Combinator
189
+ query = query.slice(combCap.index + combCap[0].length)
190
+ }
191
+ selectors.push({ type: query, combinator })
192
+ return selectors
193
+ })
194
+ }
195
+
196
+ export function select(source: string | Element[], query: string | Selector[][]): Element[] {
197
+ if (!source || !query) return []
198
+ if (typeof source === 'string') source = parse(source)
199
+ if (typeof query === 'string') query = parseSelector(query)
200
+ if (!query.length) return []
201
+ let adjacent: Selector[][] = []
202
+ const results: Element[] = []
203
+ for (const [index, element] of source.entries()) {
204
+ const inner: Selector[][] = []
205
+ const local = [...query, ...adjacent]
206
+ adjacent = []
207
+ let matched = false
208
+ for (const group of local) {
209
+ const { type, combinator } = group[0]
210
+ if (type === element.type || type === '*') {
211
+ if (group.length === 1) {
212
+ matched = true
213
+ } else if ([' ', '>'].includes(group[1].combinator)) {
214
+ inner.push(group.slice(1))
215
+ } else if (group[1].combinator === '+') {
216
+ adjacent.push(group.slice(1))
217
+ } else {
218
+ query.push(group.slice(1))
219
+ }
220
+ }
221
+ if (combinator === ' ') {
222
+ inner.push(group)
223
+ }
224
+ }
225
+ if (matched) results.push(source[index])
226
+ results.push(...select(element.children, inner))
227
+ }
228
+ return results
229
+ }
230
+
231
+ export function interpolate(expr: string, context: any) {
232
+ expr = expr.trim()
233
+ if (!/^[\w.]+$/.test(expr)) {
234
+ return evaluate(expr, context) ?? ''
235
+ }
236
+ let value = context
237
+ for (const part of expr.split('.')) {
238
+ value = value[part]
239
+ if (isNullable(value)) return ''
240
+ }
241
+ return value ?? ''
242
+ }
243
+
244
+ const tagRegExp1 = /(?<comment><!--[\s\S]*?-->)|(?<tag><(\/?)([^!\s>/]*)([^>]*?)\s*(\/?)>)/
245
+ const tagRegExp2 = /(?<comment><!--[\s\S]*?-->)|(?<tag><(\/?)([^!\s>/]*)([^>]*?)\s*(\/?)>)|(?<curly>\{(?<derivative>[@:/#][^\s}]*)?[\s\S]*?\})/
246
+ const attrRegExp1 = /([^\s=]+)(?:="(?<value1>[^"]*)"|='(?<value2>[^']*)')?/g
247
+ const attrRegExp2 = /([^\s=]+)(?:="(?<value1>[^"]*)"|='(?<value2>[^']*)'|=(?<curly>\{([^}]+)\}))?/g
248
+
249
+ const enum Position {
250
+ OPEN,
251
+ CLOSE,
252
+ EMPTY,
253
+ CONTINUE,
254
+ }
255
+
256
+ interface Token {
257
+ type: 'angle' | 'curly'
258
+ name: string
259
+ position: Position
260
+ source: string
261
+ extra: string
262
+ children?: Dict<(string | Token)[]>
263
+ }
264
+
265
+ export function parse(source: string, context?: any) {
266
+ const tokens: (string | Token)[] = []
267
+ function pushText(content: string) {
268
+ if (content) tokens.push(content)
269
+ }
270
+
271
+ const tagRegExp = context ? tagRegExp2 : tagRegExp1
272
+ let tagCap: RegExpExecArray
273
+ let trimStart = true
274
+ while ((tagCap = tagRegExp.exec(source))) {
275
+ const trimEnd = !tagCap.groups.curly
276
+ parseContent(source.slice(0, tagCap.index), trimStart, trimEnd)
277
+ trimStart = trimEnd
278
+ source = source.slice(tagCap.index + tagCap[0].length)
279
+ const [_, , , close, type, extra, empty] = tagCap
280
+ if (tagCap.groups.comment) continue
281
+ if (tagCap.groups.curly) {
282
+ let name = '', position = Position.EMPTY
283
+ if (tagCap.groups.derivative) {
284
+ name = tagCap.groups.derivative.slice(1)
285
+ position = {
286
+ '@': Position.EMPTY,
287
+ '#': Position.OPEN,
288
+ '/': Position.CLOSE,
289
+ ':': Position.CONTINUE,
290
+ }[tagCap.groups.derivative[0]]
291
+ }
292
+ tokens.push({
293
+ type: 'curly',
294
+ name,
295
+ position,
296
+ source: tagCap.groups.curly,
297
+ extra: tagCap.groups.curly.slice(1 + (tagCap.groups.derivative ?? '').length, -1),
298
+ })
299
+ continue
300
+ }
301
+ tokens.push({
302
+ type: 'angle',
303
+ source: _,
304
+ name: type || Fragment,
305
+ position: close ? Position.CLOSE : empty ? Position.EMPTY : Position.OPEN,
306
+ extra,
307
+ })
308
+ }
309
+
310
+ parseContent(source, trimStart, true)
311
+ function parseContent(source: string, trimStart: boolean, trimEnd: boolean) {
312
+ source = unescape(source)
313
+ if (trimStart) source = source.replace(/^\s*\n\s*/, '')
314
+ if (trimEnd) source = source.replace(/\s*\n\s*$/, '')
315
+ pushText(source)
316
+ }
317
+
318
+ return parseTokens(foldTokens(tokens), context)
319
+ }
320
+
321
+ function foldTokens(tokens: (string | Token)[]) {
322
+ const stack: [Token, string][] = [[{
323
+ type: 'angle',
324
+ name: Fragment,
325
+ position: Position.OPEN,
326
+ source: '',
327
+ extra: '',
328
+ children: { default: [] },
329
+ }, 'default']]
330
+
331
+ function pushToken(...tokens: (string | Token)[]) {
332
+ const [token, slot] = stack[0]
333
+ token.children[slot].push(...tokens)
334
+ }
335
+
336
+ for (const token of tokens) {
337
+ if (typeof token === 'string') {
338
+ pushToken(token)
339
+ continue
340
+ }
341
+ const { name, position } = token
342
+ if (position === Position.CLOSE) {
343
+ if (stack[0][0].name === name) {
344
+ stack.shift()
345
+ }
346
+ } else if (position === Position.CONTINUE) {
347
+ stack[0][0].children[name] = []
348
+ stack[0][1] = name
349
+ } else if (position === Position.OPEN) {
350
+ pushToken(token)
351
+ token.children = { default: [] }
352
+ stack.unshift([token, 'default'])
353
+ } else {
354
+ pushToken(token)
355
+ }
356
+ }
357
+
358
+ return stack[stack.length - 1][0].children.default
359
+ }
360
+
361
+ function parseTokens(tokens: (string | Token)[], context?: any) {
362
+ const result: Element[] = []
363
+ for (const token of tokens) {
364
+ if (typeof token === 'string') {
365
+ result.push(Element('text', { content: token }))
366
+ } else if (token.type === 'angle') {
367
+ const attrs = {}
368
+ const attrRegExp = context ? attrRegExp2 : attrRegExp1
369
+ let attrCap: RegExpExecArray
370
+ while ((attrCap = attrRegExp.exec(token.extra))) {
371
+ const [, key, v1, v2 = v1, v3] = attrCap
372
+ if (v3) {
373
+ attrs[key] = interpolate(v3, context)
374
+ } else if (!isNullable(v2)) {
375
+ attrs[key] = unescape(v2)
376
+ } else if (key.startsWith('no-')) {
377
+ attrs[key.slice(3)] = false
378
+ } else {
379
+ attrs[key] = true
380
+ }
381
+ }
382
+ result.push(Element(token.name, attrs, token.children && parseTokens(token.children.default, context)))
383
+ } else if (!token.name) {
384
+ result.push(...toElementArray(interpolate(token.extra, context)))
385
+ } else if (token.name === 'if') {
386
+ if (evaluate(token.extra, context)) {
387
+ result.push(...parseTokens(token.children.default, context))
388
+ } else {
389
+ result.push(...parseTokens(token.children.else || [], context))
390
+ }
391
+ } else if (token.name === 'each') {
392
+ const [expr, ident] = token.extra.split(/\s+as\s+/)
393
+ const items = interpolate(expr, context)
394
+ if (!items || !items[Symbol.iterator]) continue
395
+ for (const item of items) {
396
+ result.push(...parseTokens(token.children.default, { ...context, [ident]: item }))
397
+ }
398
+ }
399
+ }
400
+ return result
401
+ }
402
+
403
+ function visit<S>(element: Element, rules: Visitor<S>, session: S) {
404
+ const { type, attrs, children } = element
405
+ if (typeof rules === 'function') {
406
+ return rules(element, session)
407
+ } else {
408
+ let result: any = rules[typeof type === 'string' ? type : ''] ?? rules.default ?? true
409
+ if (typeof result === 'function') {
410
+ result = result(attrs, children, session)
411
+ }
412
+ return result
413
+ }
414
+ }
415
+
416
+ export function transform<S = never>(source: string, rules: SyncVisitor<S>, session?: S): string
417
+ export function transform<S = never>(source: Element[], rules: SyncVisitor<S>, session?: S): Element[]
418
+ export function transform<S>(source: string | Element[], rules: SyncVisitor<S>, session?: S) {
419
+ const elements = typeof source === 'string' ? parse(source) : source
420
+ const output: Element[] = []
421
+ elements.forEach((element) => {
422
+ const { type, attrs, children } = element
423
+ const result = visit(element, rules, session)
424
+ if (result === true) {
425
+ output.push(Element(type, attrs, transform(children, rules, session)))
426
+ } else if (result !== false) {
427
+ output.push(...toElementArray(result))
428
+ }
429
+ })
430
+ return typeof source === 'string' ? output.join('') : output
431
+ }
432
+
433
+ export async function transformAsync<S = never>(source: string, rules: Visitor<S>, session?: S): Promise<string>
434
+ export async function transformAsync<S = never>(source: Element[], rules: Visitor<S>, session?: S): Promise<Element[]>
435
+ export async function transformAsync<S>(source: string | Element[], rules: Visitor<S>, session?: S) {
436
+ const elements = typeof source === 'string' ? parse(source) : source
437
+ const children = (await Promise.all(elements.map(async (element) => {
438
+ const { type, attrs, children } = element
439
+ const result = await visit(element, rules, session)
440
+ if (result === true) {
441
+ return [Element(type, attrs, await transformAsync(children, rules, session))]
442
+ } else if (result !== false) {
443
+ return toElementArray(result)
444
+ } else {
445
+ return []
446
+ }
447
+ }))).flat(1)
448
+ return typeof source === 'string' ? children.join('') : children
449
+ }
450
+
451
+ export type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict]) => Element
452
+
453
+ function createFactory<R extends any[] = any[]>(type: string, ...keys: string[]): Factory<R> {
454
+ return (...args: any[]) => {
455
+ const element = Element(type)
456
+ keys.forEach((key, index) => {
457
+ if (!isNullable(args[index])) {
458
+ element.attrs[key] = args[index]
459
+ }
460
+ })
461
+ if (args[keys.length]) {
462
+ Object.assign(element.attrs, args[keys.length])
463
+ }
464
+ return element
465
+ }
466
+ }
467
+
468
+ // eslint-disable-next-line prefer-const
469
+ export let warn: (message: string) => void = () => {}
470
+
471
+ function createAssetFactory(type: string): Factory<[data: string] | [data: Buffer | ArrayBuffer, type: string]> {
472
+ return (url, ...args) => {
473
+ let prefix = 'base64://'
474
+ if (typeof args[0] === 'string') {
475
+ prefix = `data:${args.shift()};base64,`
476
+ }
477
+ if (is('Buffer', url)) {
478
+ url = prefix + url.toString('base64')
479
+ } else if (is('ArrayBuffer', url)) {
480
+ url = prefix + arrayBufferToBase64(url)
481
+ }
482
+ if (url.startsWith('base64://')) {
483
+ warn(`protocol "base64:" is deprecated and will be removed in the future, please use "data:" instead`)
484
+ }
485
+ return Element(type, { ...args[0] as {}, url })
486
+ }
487
+ }
488
+
489
+ export const text = createFactory<[content: any]>('text', 'content')
490
+ export const at = createFactory<[id: any]>('at', 'id')
491
+ export const sharp = createFactory<[id: any]>('sharp', 'id')
492
+ export const quote = createFactory<[id: any]>('quote', 'id')
493
+ export const image = createAssetFactory('image')
494
+ export const video = createAssetFactory('video')
495
+ export const audio = createAssetFactory('audio')
496
+ export const file = createAssetFactory('file')
497
+
498
+ export function i18n(path: string | Dict, children?: any[]) {
499
+ return Element('i18n', typeof path === 'string' ? { path } : path, children)
500
+ }
501
+ }
502
+
503
+ export = Element
package/lib/.DS_Store DELETED
Binary file