-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizeSVG.js
executable file
·76 lines (67 loc) · 1.92 KB
/
optimizeSVG.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
import cliProgress from "cli-progress"
import kanjivgIndex from "../kanjivg/kvg-index.json"
import svgo from "svgo"
const kanjivgPath = "./kanjivg/kanji"
const outputPath = "./public/k"
const indexOutput = "./public/kg-index.json"
const reducedIndex = Object.fromEntries(
Object.entries(kanjivgIndex).map(([char, files]) => {
const file = files.findLast((name) => !name.includes("-")) || files.at(-1)
return [char, file]
})
)
const filenames = Object.values(reducedIndex)
const bar = new cliProgress.SingleBar({
etaBuffer: 300,
})
console.log("Optimizing SVG files:")
bar.start(filenames.length)
for (const filename of filenames) {
await proccessFile(filename)
bar.increment()
}
bar.stop()
console.log(`Saving index in ${indexOutput}`)
Bun.write(indexOutput, JSON.stringify(reducedIndex))
console.log("Process finished.")
async function proccessFile(filename) {
const file = Bun.file(`${kanjivgPath}/${filename}`)
const originalSVG = (await file.text())
// remove kvg: namespaced attributes wich breaks svgo parser
.replace(/\s*kvg:\w+="[^"]*"/g, "")
// use classes intead of ids for the Paths and Numbers groups
.replace(/id="kvg:Stroke(Paths|Numbers)\w*"/g, `class="kg$1"`)
const optimizedSVG = svgo.optimize(originalSVG, {
plugins: [
{
name: "preset-default",
params: {
overrides: {
convertColors: false,
removeViewBox: false,
mergePaths: false,
},
},
},
"removeDimensions",
{
name: "addClassesToSVGElement",
params: {
className: "kan-g",
},
},
"convertStyleToAttrs",
{
name: "convertColors",
params: {
currentColor: "#000",
names2hex: true,
rgb2hex: true,
shorthex: true,
shortname: true,
},
},
],
})
await Bun.write(`${outputPath}/${filename}`, optimizedSVG.data)
}