-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
63 lines (55 loc) · 1.67 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* edge-iconify
*
* (c) Edge
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { edgeGlobals } from 'edge.js'
import type { PluginFn } from 'edge.js/types'
import type { IconifyIconCustomisations } from '@iconify/iconify'
import { SvgGenerator } from './src/svg_generator.js'
import { EdgeIconifyOptions } from './src/types.js'
/**
* Edge plugin to work with Iconify icon sets
*/
export const edgeIconify: PluginFn<EdgeIconifyOptions> = (edge, _, options) => {
const svgGenerator = new SvgGenerator(options)
/**
* Register the `svg` global helper
*/
edge.global(
'svg',
function (name: string, props?: IconifyIconCustomisations & Record<string, any>) {
return edgeGlobals.html.safe(svgGenerator.generate(name, props))
}
)
/**
* Register the svg tag
*/
edge.registerTag({
block: false,
seekable: true,
tagName: 'svg',
compile(parser, buffer, token) {
const parsed = parser.utils.transformAst(
parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename),
token.filename,
parser
)
/**
* For non sequence expression we have to wrap the args inside parenthesis
*/
const openingBrace = parsed.type !== 'SequenceExpression' ? '(' : ''
const closingBrace = parsed.type !== 'SequenceExpression' ? ')' : ''
buffer.outputExpression(
`state.svg${openingBrace}${parser.utils.stringify(parsed)}${closingBrace}.value`,
token.filename,
token.loc.start.line,
false
)
},
})
}
export { addCollection, addIcon } from '@iconify/iconify'