Skip to content

Commit

Permalink
bugfix(#31): only compile embeds if link exists
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed May 3, 2024
1 parent 41b7e68 commit 90b6af2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type WikilinkMeta = {
link: string
slug: string
isEmbed: boolean
exists: boolean

// href and path are loaded from the linked page
href?: string
Expand Down
2 changes: 1 addition & 1 deletion src/interlinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ module.exports = class Interlinker {

// If this is an embed and the embed template hasn't been compiled, add this to the queue
// @TODO compiledEmbeds should be keyed by the wikilink text as i'll be allowing setting embed values via namespace, or other method e.g ![[ident||template]]
if (link.isEmbed && this.compiledEmbeds.has(link.slug) === false) {
if (link.isEmbed && link.exists && this.compiledEmbeds.has(link.slug) === false) {
compilePromises.push(this.compileTemplate(page));
}

Expand Down
4 changes: 3 additions & 1 deletion src/wikilink-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ module.exports = class WikilinkParser {
anchor,
link,
slug,
isEmbed
isEmbed,
exists: false,
}

// Lookup page data from 11ty's collection to obtain url and title if currently null
Expand All @@ -83,6 +84,7 @@ module.exports = class WikilinkParser {
if (meta.title === null && page.data.title) meta.title = page.data.title;
meta.href = page.url;
meta.path = page.inputPath;
meta.exists = true;
} else {
// If this wikilink goes to a page that doesn't exist, add to deadLinks list and
// update href for stub post.
Expand Down
14 changes: 14 additions & 0 deletions tests/eleventy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,17 @@ test("Sample page (files with hash in title)", async t => {
`<div><p>This link should be to <a href="/page/hello/#some-heading">a fragment identifier</a>.</p><p><p>Hello world.</p></p></div><div></div>`
);
});

test("Sample with simple embed (broken embed)", async t => {
let elev = new Eleventy(fixturePath('sample-with-simple-embed'), fixturePath('sample-with-simple-embed/_site'), {
configPath: fixturePath('sample-with-simple-embed/eleventy.config.js'),
});

let results = await elev.toJSON();

// Bad Wikilink Embed shows default text
t.is(
normalize(findResultByUrl(results, '/broken/').content),
`<div><p>[UNABLE TO LOCATE EMBED]</p></div><div></div>`
);
});
6 changes: 6 additions & 0 deletions tests/fixtures/sample-with-simple-embed/broken-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Broken 2
layout: default.liquid
---

![[this is broken]]
6 changes: 6 additions & 0 deletions tests/fixtures/sample-with-simple-embed/broken.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Broken
layout: default.liquid
---

![[this is broken]]
6 changes: 6 additions & 0 deletions tests/fixtures/sample-with-simple-embed/stubs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Stubs
layout: default.liquid
---

Stubby Stubs.

0 comments on commit 90b6af2

Please sign in to comment.