From 093d425a02ff144a0f5c6475eadc537d7fd1f041 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 30 Jul 2023 18:02:30 -0300 Subject: content: transformContent: use switch/case in node -> content --- src/content/dependencies/transformContent.js | 78 ++++++++++++++-------------- 1 file changed, 40 insertions(+), 38 deletions(-) (limited to 'src/content/dependencies') diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js index 03bd7bcc..75cb4847 100644 --- a/src/content/dependencies/transformContent.js +++ b/src/content/dependencies/transformContent.js @@ -208,55 +208,57 @@ export default { // individual nodes). const contentFromNodes = data.nodes.map(node => { - if (node.type === 'text') { - return {type: 'text', data: node.data}; - } + switch (node.type) { + case 'text': + return {type: 'text', data: node.data}; + + case 'link': { + const linkNode = relations.links[linkIndex++]; + if (linkNode.type === 'text') { + return {type: 'text', data: linkNode.data}; + } - if (node.type === 'link') { - const linkNode = relations.links[linkIndex++]; - if (linkNode.type === 'text') { - return {type: 'text', data: linkNode.data}; + const {link, label, hash, toIndex} = linkNode; + + return { + type: 'link', + data: + (toIndex + ? link.slots({content: label, hash}) + : link.slots({ + content: label, hash, + preferShortName: slots.preferShortLinkNames, + })), + }; } - const {link, label, hash, toIndex} = linkNode; - - return { - type: 'link', - data: - (toIndex - ? link.slots({content: label, hash}) - : link.slots({ - content: label, hash, - preferShortName: slots.preferShortLinkNames, - })), - }; - } + case 'tag': { + const {replacerKey, replacerValue} = node.data; - if (node.type === 'tag') { - const {replacerKey, replacerValue} = node.data; + const spec = replacerSpec[replacerKey]; - const spec = replacerSpec[replacerKey]; + if (!spec) { + return getPlaceholder(node, data.content); + } - if (!spec) { - return getPlaceholder(node, data.content); - } + const {value: valueFn, html: htmlFn} = spec; - const {value: valueFn, html: htmlFn} = spec; + const value = + (valueFn + ? valueFn(replacerValue) + : replacerValue); - const value = - (valueFn - ? valueFn(replacerValue) - : replacerValue); + const contents = + (htmlFn + ? htmlFn(value, {html, language}) + : value); - const contents = - (htmlFn - ? htmlFn(value, {html, language}) - : value); + return {type: 'text', data: contents}; + } - return {type: 'text', data: contents}; + default: + return getPlaceholder(node, data.content); } - - return getPlaceholder(node, data.content); }); // In single-link mode, return the link node exactly as is - exposing -- cgit 1.3.0-6-gf8a5