mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
ported to static site, removed _ext folder, split hbs files into partials
This commit is contained in:
41
helpers/svg.js
Normal file
41
helpers/svg.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const fs = require("fs");
|
||||
const handlebars = require("handlebars");
|
||||
const ltx = require("ltx");
|
||||
const resolve = require("resolve");
|
||||
|
||||
const path = "../svg/";
|
||||
const nameToModule = {};
|
||||
const cache = {};
|
||||
|
||||
module.exports = function (name, opts) {
|
||||
name = path + name;
|
||||
|
||||
const mod =
|
||||
nameToModule[name] ||
|
||||
(nameToModule[name] = resolve.sync(name, {
|
||||
extensions: [".svg"],
|
||||
}));
|
||||
|
||||
const content =
|
||||
cache[name] || (cache[name] = fs.readFileSync(mod, "utf-8"));
|
||||
|
||||
const svg = parse(content);
|
||||
|
||||
Object.assign(svg.attrs, opts.hash);
|
||||
|
||||
return new handlebars.SafeString(svg.root().toString());
|
||||
};
|
||||
|
||||
module.exports.cache = cache;
|
||||
|
||||
function parse(xml, mod) {
|
||||
const svg = ltx.parse(xml);
|
||||
if (svg.name != "svg") {
|
||||
throw new TypeError("Input must be an SVG");
|
||||
}
|
||||
|
||||
delete svg.attrs.xmlns;
|
||||
delete svg.attrs["xmlns:xlink"];
|
||||
|
||||
return svg;
|
||||
}
|
Reference in New Issue
Block a user