-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
marko.ts
20 lines (18 loc) · 674 Bytes
/
marko.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { Compiler } from './types'
export const MarkoCompiler = ((svg: string) => {
const openTagEnd = svg.indexOf('>', svg.indexOf('<svg '))
const closeTagStart = svg.lastIndexOf('</svg')
const openTag = `${svg.slice(0, openTagEnd)} ...input>`
const content = `$!{\`${escapeTemplateLiteral(svg.slice(openTagEnd + 1, closeTagStart))}\`}`
const closeTag = svg.slice(closeTagStart)
return `${openTag}${content}${closeTag}`
}) as Compiler
export function escapeTemplateLiteral(str: string): string {
return str.replace(/\\.|[$`]/g, (m) => {
switch (m) {
case '$': return '$'
case '`': return '`'
default: return m
}
})
}