-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
58 lines (54 loc) · 1.67 KB
/
mod.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
// deno-lint-ignore-file prefer-const
import { readAll } from "https://deno.land/[email protected]/io/read_all.ts";
import { optimize } from "npm:svgo"
export function createUUID(n=7){
let arr = new Array<number>(n)
let newArr = arr.fill(0).map(_=>Math.floor(Math.random()*36))
let r = newArr.map(a=>a.toString(36)).join("")
return r
}
function renderReactSvg(svg:string){
let result1 = optimize(svg,{
})
let result = optimize(result1.data,{
multipass: true,
js2svg:{
indent: 4,
pretty: true
},
plugins: [{
name: 'prefixIds',
params: {
delim: '',
prefix: () => createUUID(3),
}
} as any]
})
let outputStr = result.data
let lines = outputStr.replaceAll(/\b[a-z0-9]+([-:][a-z0-9]+)+=/g,(a:any)=>{
let xs = a.split(/[-:]/)
let [head,...rest] = xs
return head + rest.map((word:string)=>{
return word.slice(0,1).toUpperCase() + word.slice(1)
}).join("")
}).split("\n")
let [head,...rest] = lines
let w = /width="([0-9]+)/.exec(head )?.[1] || ""
let h= /height="([0-9]+)/.exec(head )?.[1] || ""
let out = `<svg viewBox="0 0 ${w} ${h}" {...props}>\n` + rest.map(line=>" "+line).join("\n")
return `import { SVGProps } from "react"
export function DiqyeSvg(props: SVGProps<SVGSVGElement>) {
return ${out}
}
`
}
async function readTextAll(){
let data = await readAll(Deno.stdin)
return new TextDecoder().decode(data)
}
async function main(){
let text = await readTextAll()
let outText = renderReactSvg(text)
console.log(outText)
}
main()