Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
fix(embed): provide fallback with esi:remove when esi include fails
Browse files Browse the repository at this point in the history
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
  • Loading branch information
trieloff committed Apr 18, 2019
1 parent 0d5193a commit 575391d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/utils/embed-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand Down
2 changes: 2 additions & 0 deletions test/testEmbedHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ https://www.youtube.com/watch?v=KOxbO0EI4MA
assert.equal(result.response.body, `<p>Hello World
Here comes an embed.</p>
<esi:include src="https://example-embed-service.com/https://www.youtube.com/watch?v=KOxbO0EI4MA"></esi:include>
<esi:remove><p><a href="https://www.youtube.com/watch?v=KOxbO0EI4MA">https://www.youtube.com/watch?v=KOxbO0EI4MA</a></p></esi:remove>
<p><img src="easy.png" alt="Easy!" srcset="easy.png?width=480&amp;auto=webp 480w,easy.png?width=1384&amp;auto=webp 1384w,easy.png?width=2288&amp;auto=webp 2288w,easy.png?width=3192&amp;auto=webp 3192w,easy.png?width=4096&amp;auto=webp 4096w" sizes="100vw"></p>`);
});

Expand Down Expand Up @@ -152,6 +153,7 @@ Here comes an embed.
assert.equal(result.response.body, `<p>Hello World
Here comes an embed.</p>
<esi:include src="/test/foo.embed.html"></esi:include>
<esi:remove><p>./foo.md</p></esi:remove>
<p><img src="easy.png" alt="Easy!" srcset="easy.png?width=480&amp;auto=webp 480w,easy.png?width=1384&amp;auto=webp 1384w,easy.png?width=2288&amp;auto=webp 2288w,easy.png?width=3192&amp;auto=webp 3192w,easy.png?width=4096&amp;auto=webp 4096w" sizes="100vw"></p>`);
});
});

0 comments on commit 575391d

Please sign in to comment.