From 575391dae8d4994b6ddf0a794cbed8ae609e104f Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 18 Apr 2019 08:16:55 +0000 Subject: [PATCH] fix(embed): provide fallback with esi:remove when esi include fails Until now, a failed ESI include would fail silently, without a possible fallback. This change falls back to the markdown rendered as HTML in case the ESI does not work, giving at least an approximation of the content created. fixes #267 --- src/utils/embed-handler.js | 11 +++++++++-- test/testEmbedHandler.js | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/utils/embed-handler.js b/src/utils/embed-handler.js index a8926597d..1faa0a1db 100644 --- a/src/utils/embed-handler.js +++ b/src/utils/embed-handler.js @@ -16,13 +16,20 @@ const URI = require('uri-js'); function embed({ EMBED_SERVICE }) { - return function handler(h, node) { + return function handler(h, node, _, handlechild) { const { url } = node; const props = { // prepend the embed service for absolute URLs src: (URI.parse(url).reference === 'absolute' ? EMBED_SERVICE : '') + url, }; - const retval = h(node, 'esi:include', props); + const retval = [h(node, 'esi:include', props)]; + + if (node.children && node.children.length) { + const rem = h(node, 'esi:remove', {}); + node.children.forEach(childnode => handlechild(h, childnode, node, rem)); + retval.push(rem); + } + return retval; }; } diff --git a/test/testEmbedHandler.js b/test/testEmbedHandler.js index 60293c6c4..43fadd7a4 100644 --- a/test/testEmbedHandler.js +++ b/test/testEmbedHandler.js @@ -122,6 +122,7 @@ https://www.youtube.com/watch?v=KOxbO0EI4MA assert.equal(result.response.body, `

Hello World Here comes an embed.

+

Easy!

`); }); @@ -152,6 +153,7 @@ Here comes an embed. assert.equal(result.response.body, `

Hello World Here comes an embed.

+

Easy!

`); }); });