Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Don't render embeds if link doesn't exist #35

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading