From 5d5eb93baac2d413e562f193a94304ffa6ae1c2f Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 25 Mar 2022 13:35:21 +0530 Subject: [PATCH] Implement plugin --- .gitignore | 3 +- scripts/postcss/css-url-processor.js | 81 ++++++++++++++++++++++++++++ scripts/postcss/svg-colorizer.js | 42 +++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 scripts/postcss/css-url-processor.js create mode 100644 scripts/postcss/svg-colorizer.js diff --git a/.gitignore b/.gitignore index 7f6220cf..089600eb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ bundle.js target lib *.tar.gz -.eslintcache \ No newline at end of file +.eslintcache +.tmp diff --git a/scripts/postcss/css-url-processor.js b/scripts/postcss/css-url-processor.js new file mode 100644 index 00000000..854b549f --- /dev/null +++ b/scripts/postcss/css-url-processor.js @@ -0,0 +1,81 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +const valueParser = require("postcss-value-parser"); +const resolve = require("path").resolve; + +function colorsFromURL(url, colorVariables) { + const params = new URL(`file://${url}`).searchParams; + const primary = params.get("primary"); + if (!primary) { + return null; + } + const secondary = params.get("secondary"); + const primaryColor = colorVariables[primary]; + const secondaryColor = colorVariables[secondary]; + if (!primaryColor) { + throw new Error(`Variable ${primary} not found in resolved color variables!`); + } + if (!secondaryColor) { + throw new Error(`Variable ${secondary} not found in resolved color variables!`); + } + return [primaryColor, secondaryColor]; +} + +function processURL(decl, replacer, colorVariables) { + const value = decl.value; + const parsed = valueParser(value); + parsed.walk(async node => { + if (node.type !== "function" || node.value !== "url") { + return; + } + const urlStringNode = node.nodes[0]; + const oldURL = urlStringNode.value; + const cssPath = decl.source?.input.file.replace(/[^/]*$/, ""); + const oldURLAbsolute = resolve(cssPath, oldURL); + const colors = colorsFromURL(oldURLAbsolute, colorVariables); + if (!colors) { + // If no primary color is provided via url params, then this url need not be handled. + return; + } + const newURL = replacer(oldURLAbsolute.replace(/\?.+/, ""), ...colors); + if (!newURL) { + throw new Error("Replacer failed to produce a replacement URL!"); + } + urlStringNode.value = newURL; + }); + decl.assign({prop: decl.prop, value: parsed.toString()}) +} + +/* * + * @type {import('postcss').PluginCreator} + */ +module.exports = (opts = {}) => { + return { + postcssPlugin: "postcss-url-to-variable", + + Once(root) { + /* + Go through each declaration and if it contains an URL, replace the url with the result + of running replacer(url) + */ + root.walkDecls(decl => processURL(decl, opts.replacer, opts.colors)); + }, + }; +}; + +module.exports.postcss = true; + diff --git a/scripts/postcss/svg-colorizer.js b/scripts/postcss/svg-colorizer.js new file mode 100644 index 00000000..1a659e6a --- /dev/null +++ b/scripts/postcss/svg-colorizer.js @@ -0,0 +1,42 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +const fs = require("fs"); +const path = require("path"); +/** + * Builds a new svg with the colors replaced and returns its location. + * @param {string} svgLocation The location of the input svg file + * @param {string} primaryColor Primary color for the new svg + * @param {string} secondaryColor Secondary color for the new svg + */ +module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondaryColor) { + const svgCode = fs.readFileSync(svgLocation, { encoding: "utf-8"}); + let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor); + coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); + const fileName = svgLocation.match(/.+\/(.+\.svg)/)[1]; + const outputPath = path.resolve(__dirname, "../../.tmp"); + try { + fs.mkdirSync(outputPath); + } + catch (e) { + if (e.code !== "EEXIST") { + throw e; + } + } + const outputFile = `${outputPath}/${fileName}`; + fs.writeFileSync(outputFile, coloredSVGCode); + return outputFile; +}