Skip to content

Commit

Permalink
Merge pull request #67 from photogabble/patch/v1.1.0/#66
Browse files Browse the repository at this point in the history
BUGFIX: Render embed templates correctly
  • Loading branch information
carbontwelve authored Nov 29, 2024
2 parents 3f81a6a + 190ac5b commit ee16917
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,37 @@ export const defaultEmbedFn = async (link, currentPage, interlinker) => {
? page.data[interlinker.opts.layoutKey]
: interlinker.opts.defaultLayout;

// TODO this should be based upon the layout extension
const language = (page.data.hasOwnProperty(interlinker.opts.layoutTemplateLangKey))
? page.data[interlinker.opts.layoutTemplateLangKey]
: interlinker.opts.defaultLayoutLang === null
? page.page.templateSyntax
: interlinker.opts.defaultLayoutLang;

// TODO: (#36) the layout below is liquid, will break if content contains invalid template tags such as passing njk file src
const tpl = layout === null
? template.content
: `{% layout "${layout}" %} {% block content %} ${template.content} {% endblock %}`;

// This is the async compile function from the RenderPlugin.js bundled with 11ty. I'm using it directly here
// to compile the embedded content.
const compiler = EleventyRenderPlugin.String;

const fn = await compiler(tpl, language, {
// Compile template.content
const contentFn = await compiler(template.content, language, {
templateConfig: interlinker.templateConfig,
extensionMap: interlinker.extensionMap
});

return fn({content: template.content, ...page.data});
const content = await contentFn({...page.data});

// If we don't have an embed layout wrapping this content, return the compiled result.
if (layout === null) return content;

// The template string is just to invoke the embed layout, the content value is the
// compiled result of template.content.
const tpl = `{% layout "${layout}" %}`

const tplFn = await compiler(tpl, language, {
templateConfig: interlinker.templateConfig,
extensionMap: interlinker.extensionMap
});

return await tplFn({content, ...page.data});
}
12 changes: 12 additions & 0 deletions tests/eleventy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,15 @@ test("Stub URL Config (can be disabled)", async t => {
`<div><p>Broken link with custom stub url [[ broken link ]].</p></div>`
);
})

test("Embedded file shortcodes get run", async t => {
let elev = new Eleventy(fixturePath('website-with-liquid-embed-shortcode'), fixturePath('website-with-liquid-embed-shortcode/_site'), {
configPath: fixturePath('website-with-liquid-embed-shortcode/eleventy.config.js'),
});

const results = await elev.toJSON();
t.is(
normalize(findResultByUrl(results, '/').content),
`<h1>Embed Below</h1><p><figure>Hello world</figure></p><h1>Embed 2 Below</h1><p><div><figure>Hello world</figure></div></p>` // TODO: (#65) remove wrapping <p> from embed
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>{{ content }}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import WikiLinksPlugin from '../../../index.js';

export default function (eleventyConfig) {
eleventyConfig.addPlugin(WikiLinksPlugin);

eleventyConfig.addPairedShortcode('sc', (content) => `<figure>${content}</figure>`);

return {
dir: {
includes: "_includes",
layouts: "_layouts",
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Embedded Page with template
embedLayout: _layouts/test-embed.liquid
---

{% sc %}
Hello world
{% endsc %}
7 changes: 7 additions & 0 deletions tests/fixtures/website-with-liquid-embed-shortcode/embed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Embedded Page
---

{% sc %}
Hello world
{% endsc %}
11 changes: 11 additions & 0 deletions tests/fixtures/website-with-liquid-embed-shortcode/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Embed Page
---

# Embed Below

![[Embedded Page]]

# Embed 2 Below

![[Embedded Page with template]]

0 comments on commit ee16917

Please sign in to comment.