From 90b6af261d44f267a439ed0d7a4c8b6219727df1 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 3 May 2024 23:17:28 +0100 Subject: [PATCH] bugfix(#31): only compile embeds if link exists --- index.d.ts | 1 + src/interlinker.js | 2 +- src/wikilink-parser.js | 4 +++- tests/eleventy.test.js | 14 ++++++++++++++ .../fixtures/sample-with-simple-embed/broken-2.md | 6 ++++++ tests/fixtures/sample-with-simple-embed/broken.md | 6 ++++++ tests/fixtures/sample-with-simple-embed/stubs.md | 6 ++++++ 7 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/sample-with-simple-embed/broken-2.md create mode 100644 tests/fixtures/sample-with-simple-embed/broken.md create mode 100644 tests/fixtures/sample-with-simple-embed/stubs.md diff --git a/index.d.ts b/index.d.ts index 46281d3..9baa6a0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 diff --git a/src/interlinker.js b/src/interlinker.js index c2a44b2..0111e2e 100644 --- a/src/interlinker.js +++ b/src/interlinker.js @@ -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)); } diff --git a/src/wikilink-parser.js b/src/wikilink-parser.js index 3ceed47..af27b07 100644 --- a/src/wikilink-parser.js +++ b/src/wikilink-parser.js @@ -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 @@ -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. diff --git a/tests/eleventy.test.js b/tests/eleventy.test.js index 699c728..7210722 100644 --- a/tests/eleventy.test.js +++ b/tests/eleventy.test.js @@ -123,3 +123,17 @@ test("Sample page (files with hash in title)", async t => { `

This link should be to a fragment identifier.

Hello world.

` ); }); + +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), + `

[UNABLE TO LOCATE EMBED]

` + ); +}); diff --git a/tests/fixtures/sample-with-simple-embed/broken-2.md b/tests/fixtures/sample-with-simple-embed/broken-2.md new file mode 100644 index 0000000..945dbca --- /dev/null +++ b/tests/fixtures/sample-with-simple-embed/broken-2.md @@ -0,0 +1,6 @@ +--- +title: Broken 2 +layout: default.liquid +--- + +![[this is broken]] diff --git a/tests/fixtures/sample-with-simple-embed/broken.md b/tests/fixtures/sample-with-simple-embed/broken.md new file mode 100644 index 0000000..e9a0f22 --- /dev/null +++ b/tests/fixtures/sample-with-simple-embed/broken.md @@ -0,0 +1,6 @@ +--- +title: Broken +layout: default.liquid +--- + +![[this is broken]] diff --git a/tests/fixtures/sample-with-simple-embed/stubs.md b/tests/fixtures/sample-with-simple-embed/stubs.md new file mode 100644 index 0000000..1ff6067 --- /dev/null +++ b/tests/fixtures/sample-with-simple-embed/stubs.md @@ -0,0 +1,6 @@ +--- +title: Stubs +layout: default.liquid +--- + +Stubby Stubs.