From e2ec9421f9acdef3be6172829b870c97bef1fca9 Mon Sep 17 00:00:00 2001 From: April Sylph <28949509+AprilSylph@users.noreply.github.com> Date: Wed, 28 Jun 2023 23:19:57 +0100 Subject: [PATCH] add fallback renderers for blocks and attribution fixes #71 --- src/lib/npf.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/lib/npf.js b/src/lib/npf.js index 97644e7..43b78dd 100644 --- a/src/lib/npf.js +++ b/src/lib/npf.js @@ -200,13 +200,19 @@ const blockRenderers = { Object.assign(document.createElement('summary'), { textContent: title }), Object.assign(document.createElement('p'), { textContent: text }) )); + }, + + default ({ type }) { + console.warn(`Unknown block type: "${type}"`); + return document.createElement('div'); } }; -const renderBlock = block => blockRenderers[block.type](block).tap(element => { - element.dataset.block = block.type; - if (block.subtype) element.dataset.subtype = block.subtype; -}); +const renderBlock = block => + (blockRenderers[block.type] || blockRenderers.default)(block).tap(element => { + element.dataset.block = block.type; + if (block.subtype) element.dataset.subtype = block.subtype; + }); const applyFormatting = ({ text, formatting = [] }) => { if (!formatting.length) { @@ -326,12 +332,18 @@ const attributionRenderers = { } return a; + }, + + default ({ type }) { + console.warn(`Unknown attribution type: "${type}"`); + return document.createElement('span'); } }; -const renderAttribution = attribution => attributionRenderers[attribution.type](attribution).tap(element => { - element.dataset.attribution = attribution.type; -}); +const renderAttribution = attribution => + (attributionRenderers[attribution.type] || attributionRenderers.default)(attribution).tap(element => { + element.dataset.attribution = attribution.type; + }); const ascendBy = (...funcs) => (negativeFirstItem, postiveFirstItem) => { for (const func of funcs) {