diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-07-30 18:02:30 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-07-30 18:02:30 -0300 |
commit | 093d425a02ff144a0f5c6475eadc537d7fd1f041 (patch) | |
tree | ab0ea2ed4e374f350c2144b30b466978dc6d5ecd | |
parent | 2d5e22df4b418d96a88afb4589ac326fb77b6e7a (diff) |
content: transformContent: use switch/case in node -> content
-rw-r--r-- | src/content/dependencies/transformContent.js | 78 |
1 files changed, 40 insertions, 38 deletions
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 |