From 5b63ecbd96d79b4f81afadbc767f3a542019d24f Mon Sep 17 00:00:00 2001
From: lowlighter <22963968+lowlighter@users.noreply.github.com>
Date: Wed, 28 Apr 2021 12:59:02 +0200
Subject: [PATCH] Improve embed function input normalization
---
source/app/metrics/index.mjs | 34 ++++++++++++++++++++++++++++------
1 file changed, 28 insertions(+), 6 deletions(-)
diff --git a/source/app/metrics/index.mjs b/source/app/metrics/index.mjs
index 824cc5d1..2fe4f365 100644
--- a/source/app/metrics/index.mjs
+++ b/source/app/metrics/index.mjs
@@ -85,11 +85,33 @@
console.debug(error)
}
//Embed method
- const embed = async(name, q) => {
- q = Object.fromEntries([...Object.entries(q).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value]), ["base", false], ["config.animations", false]])
- const plugins = Object.fromEntries(Object.entries(arguments[1].plugins).map(([key, value]) => [key, {...value, enabled:true}]))
- const {rendered} = await metrics({login, q}, {...arguments[1], plugins, convert:null}, arguments[2])
- return ``
+ const embed = async(name, q = {}) => {
+ //Check arguments
+ if ((!name)||(typeof q !== "object")||(q === null)) {
+ if (die)
+ throw new Error("An error occured during embed rendering, dying")
+ return "
⚠️ Failed to execute embed function: invalid arguments
" + } + //Translate action syntax to web syntax + let parts = [] + if (q.base === true) + ({parts} = conf.settings.plugins.base) + if (typeof q.base === "string") + parts = q.base.split(",").map(x => x.trim()) + if (Array.isArray(q.base)) + parts = q.base + for (const part of conf.settings.plugins.base.parts) + q[`base.${part}`] = q[`base.${part}`] ?? parts.includes(part) + if (convert === "markdown-pdf") { + q["config.animations"] = false + q.config_animations = false + } + q = Object.fromEntries([...Object.entries(q).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value]), ["base", false]]) + //Enable required plugins + const plugins = Object.fromEntries(Object.entries(arguments[1].plugins).map(([key, value]) => [key, {...value, enabled:true}])) + //Compute rendering + const {rendered} = await metrics({login, q}, {...arguments[1], plugins, convert:null}, arguments[2]) + return `