jinja_svg_catalog.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Edge } from "edge.js";
  2. import fs from "fs";
  3. import { dirname, resolve } from "path";
  4. import { optimize as svgo } from "svgo";
  5. import { fileURLToPath } from "url";
  6. const __dirname = dirname(fileURLToPath(import.meta.url));
  7. const __jinja_class_placeholder__ = "__jinja_class_placeholder__";
  8. // -- types
  9. /**
  10. * @typedef {object} IconSet - A set of icons
  11. * @property {object[]} set - Array of SVG icons, where property name is the
  12. * name of the icon and value is the src of the SVG (relative to base).
  13. * @property {string} base - Folder in which the SVG src files are located.
  14. * @property {import("svgo").Config} svgo_opts - svgo options for this set.
  15. */
  16. /**
  17. * @typedef {object} IconSVG - Mapping of icon name to SVG source file.
  18. * @property {string} name - Name of the icon isource file.
  19. * @property {string} src - Name of the destination file.
  20. * @property {import("svgo").Config} svgo_opts - Options passed to svgo.
  21. */
  22. /**
  23. * @typedef {object} JinjaMacro - Arguments to create a jinja macro
  24. * @property {string} name - Name of the jinja macro.
  25. * @property {string} class - SVG's class name (value of XML class attribute)
  26. */
  27. // -- functions
  28. /**
  29. * Generate a jinja template with a catalog of SVG icons that can be
  30. * used in in other HTML jinja templates.
  31. *
  32. * @param {string} dest - filename of the generate jinja template.
  33. * @param {JinjaMacro} macros - Jinja macros to create.
  34. * @param {IconSVG[]} items - Array of SVG items.
  35. */
  36. function jinja_svg_catalog(dest, macros, items) {
  37. const svg_catalog = {};
  38. const edge_template = resolve(__dirname, "jinja_svg_catalog.html.edge");
  39. items.forEach((item) => {
  40. /** @type {import("svgo").Config} */
  41. // JSON.stringify & JSON.parse are used to create a deep copy of the
  42. // item.svgo_opts object
  43. const svgo_opts = JSON.parse(JSON.stringify(item.svgo_opts));
  44. svgo_opts.plugins.push({
  45. name: "addClassesToSVGElement",
  46. params: {
  47. classNames: [__jinja_class_placeholder__]
  48. }
  49. });
  50. try {
  51. const raw = fs.readFileSync(item.src, "utf8");
  52. const opt = svgo(raw, svgo_opts);
  53. svg_catalog[item.name] = opt.data;
  54. } catch (err) {
  55. console.error(`ERROR: jinja_svg_catalog processing ${item.name} src: ${item.src} -- ${err}`);
  56. throw err;
  57. }
  58. });
  59. fs.mkdir(dirname(dest), { recursive: true }, (err) => {
  60. if (err) throw err;
  61. });
  62. const ctx = {
  63. svg_catalog: svg_catalog,
  64. macros: macros,
  65. edge_template: edge_template,
  66. __jinja_class_placeholder__: __jinja_class_placeholder__,
  67. // see https://github.com/edge-js/edge/issues/162
  68. open_curly_brace: "{{",
  69. close_curly_brace: "}}"
  70. };
  71. const jinjatmpl = Edge.create().renderRawSync(fs.readFileSync(edge_template, "utf-8"), ctx);
  72. fs.writeFileSync(dest, jinjatmpl);
  73. console.log(`[jinja_svg_catalog] created: ${dest}`);
  74. }
  75. /**
  76. * Calls jinja_svg_catalog for a collection of icon sets where each set has its
  77. * own parameters.
  78. *
  79. * @param {string} dest - filename of the generate jinja template.
  80. * @param {JinjaMacro} macros - Jinja macros to create.
  81. * @param {IconSet[]} sets - Array of SVG sets.
  82. */
  83. function jinja_svg_sets(dest, macros, sets) {
  84. /** @type IconSVG[] */
  85. const items = [];
  86. const all = [];
  87. for (const obj of sets) {
  88. for (const [name, file] of Object.entries(obj.set)) {
  89. if (all.includes(name)) {
  90. throw new Error(`ERROR: ${name} has already been defined`);
  91. }
  92. items.push({
  93. name: name,
  94. src: resolve(obj.base, file),
  95. svgo_opts: obj.svgo_opts
  96. });
  97. }
  98. jinja_svg_catalog(dest, macros, items);
  99. }
  100. }
  101. // -- exports
  102. export { jinja_svg_sets, jinja_svg_catalog };