Skip to content

Commit aa8b1af

Browse files
committed
Make output svg pretty
1 parent 2a063f2 commit aa8b1af

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/routes/+page.svelte

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
7575
let mounted = false;
7676
77-
$: svgCode = svg(code);
77+
$: svgCode = prettySVG(svg(code));
7878
7979
$: full = /preserveAspectRatio/i.test(code);
8080
@@ -85,6 +85,32 @@
8585
}
8686
}
8787
88+
function prettySVG(input) {
89+
try {
90+
let xmlDoc = new DOMParser().parseFromString(input, 'application/xml');
91+
let xsltDoc = new DOMParser().parseFromString(`
92+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
93+
<xsl:strip-space elements="*"/>
94+
<xsl:template match="para[content-style][not(text())]">
95+
<xsl:value-of select="normalize-space(.)"/>
96+
</xsl:template>
97+
<xsl:template match="node()|@*">
98+
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
99+
</xsl:template>
100+
<xsl:output indent="yes"/>
101+
</xsl:stylesheet>
102+
`,'application/xml');
103+
104+
let xsltProcessor = new XSLTProcessor();
105+
xsltProcessor.importStylesheet(xsltDoc);
106+
let resultDoc = xsltProcessor.transformToDocument(xmlDoc);
107+
let resultXml = new XMLSerializer().serializeToString(resultDoc);
108+
return resultXml;
109+
} catch (e) {
110+
return input;
111+
}
112+
}
113+
88114
function isNull(value) {
89115
return value === null || value === undefined;
90116
}
@@ -319,6 +345,7 @@
319345
320346
.svg {
321347
display: block;
348+
place-items: normal;
322349
padding: 1.6em;
323350
}
324351

0 commit comments

Comments
 (0)