From 188a301e45685be5b233a55b158a9ec100d4052a Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 15 Jun 2025 14:02:21 -0300 Subject: replacer: error postprocessing media tags w/o src --- src/replacer.js | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/replacer.js b/src/replacer.js index 0698eced..927ea9ef 100644 --- a/src/replacer.js +++ b/src/replacer.js @@ -8,7 +8,7 @@ import * as marked from 'marked'; import * as html from '#html'; -import {escapeRegex, typeAppearance} from '#sugar'; +import {empty, escapeRegex, typeAppearance} from '#sugar'; import {matchMarkdownLinks} from '#wiki-data'; export const replacerSpec = { @@ -526,6 +526,7 @@ export function postprocessComments(inputNodes) { function postprocessHTMLTags(inputNodes, tagName, callback) { const outputNodes = []; + const errors = []; const lastNode = inputNodes.at(-1); @@ -593,10 +594,16 @@ function postprocessHTMLTags(inputNodes, tagName, callback) { return false; })(); - outputNodes.push( - callback(attributes, { - inline, - })); + try { + outputNodes.push( + callback(attributes, { + inline, + })); + } catch (caughtError) { + errors.push(new Error( + `Failed to process ${match[0]}`, + {cause: caughtError})); + } // No longer at the start of a line after the tag - there will at // least be text with only '\n' before the next of this tag that's @@ -619,6 +626,12 @@ function postprocessHTMLTags(inputNodes, tagName, callback) { outputNodes.push(node); } + if (!empty(errors)) { + throw new AggregateError( + errors, + `Errors postprocessing <${tagName}> tags`); + } + return outputNodes; } @@ -628,6 +641,11 @@ export function postprocessImages(inputNodes) { const node = {type: 'image'}; node.src = attributes.get('src'); + + if (!node.src) { + throw new Error(' missing src attribute'); + } + node.inline = attributes.get('inline') ?? inline; if (attributes.get('link')) node.link = attributes.get('link'); @@ -653,6 +671,10 @@ export function postprocessVideos(inputNodes) { node.src = attributes.get('src'); + if (!node.src) { + throw new Error('