@satorijs/element 2.5.1 → 2.6.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/src/index.ts ADDED
@@ -0,0 +1,499 @@
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
+ while ((tagCap = tagRegExp.exec(source))) {
274
+ parseContent(source.slice(0, tagCap.index))
275
+ source = source.slice(tagCap.index + tagCap[0].length)
276
+ const [_, , , close, type, extra, empty] = tagCap
277
+ if (tagCap.groups.comment) continue
278
+ if (tagCap.groups.curly) {
279
+ let name = '', position = Position.EMPTY
280
+ if (tagCap.groups.derivative) {
281
+ name = tagCap.groups.derivative.slice(1)
282
+ position = {
283
+ '@': Position.EMPTY,
284
+ '#': Position.OPEN,
285
+ '/': Position.CLOSE,
286
+ ':': Position.CONTINUE,
287
+ }[tagCap.groups.derivative[0]]
288
+ }
289
+ tokens.push({
290
+ type: 'curly',
291
+ name,
292
+ position,
293
+ source: tagCap.groups.curly,
294
+ extra: tagCap.groups.curly.slice(1 + (tagCap.groups.derivative ?? '').length, -1),
295
+ })
296
+ continue
297
+ }
298
+ tokens.push({
299
+ type: 'angle',
300
+ source: _,
301
+ name: type || Fragment,
302
+ position: close ? Position.CLOSE : empty ? Position.EMPTY : Position.OPEN,
303
+ extra,
304
+ })
305
+ }
306
+
307
+ parseContent(source)
308
+ function parseContent(source: string) {
309
+ pushText(unescape(source
310
+ .replace(/^\s*\n\s*/, '')
311
+ .replace(/\s*\n\s*$/, '')))
312
+ }
313
+
314
+ return parseTokens(foldTokens(tokens), context)
315
+ }
316
+
317
+ function foldTokens(tokens: (string | Token)[]) {
318
+ const stack: [Token, string][] = [[{
319
+ type: 'angle',
320
+ name: Fragment,
321
+ position: Position.OPEN,
322
+ source: '',
323
+ extra: '',
324
+ children: { default: [] },
325
+ }, 'default']]
326
+
327
+ function pushToken(...tokens: (string | Token)[]) {
328
+ const [token, slot] = stack[0]
329
+ token.children[slot].push(...tokens)
330
+ }
331
+
332
+ for (const token of tokens) {
333
+ if (typeof token === 'string') {
334
+ pushToken(token)
335
+ continue
336
+ }
337
+ const { name, position } = token
338
+ if (position === Position.CLOSE) {
339
+ if (stack[0][0].name === name) {
340
+ stack.shift()
341
+ }
342
+ } else if (position === Position.CONTINUE) {
343
+ stack[0][0].children[name] = []
344
+ stack[0][1] = name
345
+ } else if (position === Position.OPEN) {
346
+ pushToken(token)
347
+ token.children = { default: [] }
348
+ stack.unshift([token, 'default'])
349
+ } else {
350
+ pushToken(token)
351
+ }
352
+ }
353
+
354
+ return stack[stack.length - 1][0].children.default
355
+ }
356
+
357
+ function parseTokens(tokens: (string | Token)[], context?: any) {
358
+ const result: Element[] = []
359
+ for (const token of tokens) {
360
+ if (typeof token === 'string') {
361
+ result.push(Element('text', { content: token }))
362
+ } else if (token.type === 'angle') {
363
+ const attrs = {}
364
+ const attrRegExp = context ? attrRegExp2 : attrRegExp1
365
+ let attrCap: RegExpExecArray
366
+ while ((attrCap = attrRegExp.exec(token.extra))) {
367
+ const [, key, v1, v2 = v1, v3] = attrCap
368
+ if (v3) {
369
+ attrs[key] = interpolate(v3, context)
370
+ } else if (!isNullable(v2)) {
371
+ attrs[key] = unescape(v2)
372
+ } else if (key.startsWith('no-')) {
373
+ attrs[key.slice(3)] = false
374
+ } else {
375
+ attrs[key] = true
376
+ }
377
+ }
378
+ result.push(Element(token.name, attrs, token.children && parseTokens(token.children.default, context)))
379
+ } else if (!token.name) {
380
+ result.push(...toElementArray(interpolate(token.extra, context)))
381
+ } else if (token.name === 'if') {
382
+ if (evaluate(token.extra, context)) {
383
+ result.push(...parseTokens(token.children.default, context))
384
+ } else {
385
+ result.push(...parseTokens(token.children.else || [], context))
386
+ }
387
+ } else if (token.name === 'each') {
388
+ const [expr, ident] = token.extra.split(/\s+as\s+/)
389
+ const items = interpolate(expr, context)
390
+ if (!items || !items[Symbol.iterator]) continue
391
+ for (const item of items) {
392
+ result.push(...parseTokens(token.children.default, { ...context, [ident]: item }))
393
+ }
394
+ }
395
+ }
396
+ return result
397
+ }
398
+
399
+ function visit<S>(element: Element, rules: Visitor<S>, session: S) {
400
+ const { type, attrs, children } = element
401
+ if (typeof rules === 'function') {
402
+ return rules(element, session)
403
+ } else {
404
+ let result: any = rules[typeof type === 'string' ? type : ''] ?? rules.default ?? true
405
+ if (typeof result === 'function') {
406
+ result = result(attrs, children, session)
407
+ }
408
+ return result
409
+ }
410
+ }
411
+
412
+ export function transform<S = never>(source: string, rules: SyncVisitor<S>, session?: S): string
413
+ export function transform<S = never>(source: Element[], rules: SyncVisitor<S>, session?: S): Element[]
414
+ export function transform<S>(source: string | Element[], rules: SyncVisitor<S>, session?: S) {
415
+ const elements = typeof source === 'string' ? parse(source) : source
416
+ const output: Element[] = []
417
+ elements.forEach((element) => {
418
+ const { type, attrs, children } = element
419
+ const result = visit(element, rules, session)
420
+ if (result === true) {
421
+ output.push(Element(type, attrs, transform(children, rules, session)))
422
+ } else if (result !== false) {
423
+ output.push(...toElementArray(result))
424
+ }
425
+ })
426
+ return typeof source === 'string' ? output.join('') : output
427
+ }
428
+
429
+ export async function transformAsync<S = never>(source: string, rules: Visitor<S>, session?: S): Promise<string>
430
+ export async function transformAsync<S = never>(source: Element[], rules: Visitor<S>, session?: S): Promise<Element[]>
431
+ export async function transformAsync<S>(source: string | Element[], rules: Visitor<S>, session?: S) {
432
+ const elements = typeof source === 'string' ? parse(source) : source
433
+ const children = (await Promise.all(elements.map(async (element) => {
434
+ const { type, attrs, children } = element
435
+ const result = await visit(element, rules, session)
436
+ if (result === true) {
437
+ return [Element(type, attrs, await transformAsync(children, rules, session))]
438
+ } else if (result !== false) {
439
+ return toElementArray(result)
440
+ } else {
441
+ return []
442
+ }
443
+ }))).flat(1)
444
+ return typeof source === 'string' ? children.join('') : children
445
+ }
446
+
447
+ export type Factory<R extends any[]> = (...args: [...rest: R, attrs?: Dict]) => Element
448
+
449
+ function createFactory<R extends any[] = any[]>(type: string, ...keys: string[]): Factory<R> {
450
+ return (...args: any[]) => {
451
+ const element = Element(type)
452
+ keys.forEach((key, index) => {
453
+ if (!isNullable(args[index])) {
454
+ element.attrs[key] = args[index]
455
+ }
456
+ })
457
+ if (args[keys.length]) {
458
+ Object.assign(element.attrs, args[keys.length])
459
+ }
460
+ return element
461
+ }
462
+ }
463
+
464
+ // eslint-disable-next-line prefer-const
465
+ export let warn: (message: string) => void = () => {}
466
+
467
+ function createAssetFactory(type: string): Factory<[data: string] | [data: Buffer | ArrayBuffer, type: string]> {
468
+ return (url, ...args) => {
469
+ let prefix = 'base64://'
470
+ if (typeof args[0] === 'string') {
471
+ prefix = `data:${args.shift()};base64,`
472
+ }
473
+ if (is('Buffer', url)) {
474
+ url = prefix + url.toString('base64')
475
+ } else if (is('ArrayBuffer', url)) {
476
+ url = prefix + arrayBufferToBase64(url)
477
+ }
478
+ if (url.startsWith('base64://')) {
479
+ warn(`protocol "base64:" is deprecated and will be removed in the future, please use "data:" instead`)
480
+ }
481
+ return Element(type, { ...args[0] as {}, url })
482
+ }
483
+ }
484
+
485
+ export const text = createFactory<[content: any]>('text', 'content')
486
+ export const at = createFactory<[id: any]>('at', 'id')
487
+ export const sharp = createFactory<[id: any]>('sharp', 'id')
488
+ export const quote = createFactory<[id: any]>('quote', 'id')
489
+ export const image = createAssetFactory('image')
490
+ export const video = createAssetFactory('video')
491
+ export const audio = createAssetFactory('audio')
492
+ export const file = createAssetFactory('file')
493
+
494
+ export function i18n(path: string | Dict, children?: any[]) {
495
+ return Element('i18n', typeof path === 'string' ? { path } : path, children)
496
+ }
497
+ }
498
+
499
+ export = Element
package/lib/.DS_Store DELETED
Binary file