binyamin / eleventy-plugin-backlinks

An Eleventy plugin for mind gardens. Collect and display backlinks from your notes
https://www.npmjs.com/package/eleventy-plugin-backlinks
MIT License
18 stars 3 forks source link

Plugin no longer works with 11ty `3.0.0-beta` #10

Open jordan-thirus opened 2 months ago

jordan-thirus commented 2 months ago

I wanted to try upgrading to 3.0.0 which required forcing the install of this plugin to see if it was possible. This plugin fails with the error following error

[11ty] Problem writing Eleventy templates:
[11ty] Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly. Change your code to use the async `read()` method on the template instead!
[11ty] 
[11ty] Original error stack trace: Error: Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly. Change your code to use the async `read()` method on the template instead!
[11ty]     at get frontMatter [as frontMatter] (file:///home/user/github/projects/site/node_modules/@11ty/eleventy/src/TemplateContent.js:154:10)
[11ty]     at /home/user/github/projects/site/node_modules/eleventy-plugin-backlinks/lib/get-backlinks.js:18:52
[11ty]     at Object.backlinks (/home/user/github/projects/site/node_modules/eleventy-plugin-backlinks/index.js:41:38)
...

I believe what needs to happen is that the link in get-backlinks.js needs to use (await otherNote.template.read()) instead of accessing the frontmatter directly.

jordan-thirus commented 2 months ago

I used patch-package to patch eleventy-plugin-backlinks@0.2.1 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/eleventy-plugin-backlinks/lib/get-backlinks.js b/node_modules/eleventy-plugin-backlinks/lib/get-backlinks.js
index 87b0c18..3a9a814 100644
--- a/node_modules/eleventy-plugin-backlinks/lib/get-backlinks.js
+++ b/node_modules/eleventy-plugin-backlinks/lib/get-backlinks.js
@@ -6,7 +6,7 @@ function caselessCompare(a, b) {
 const wikilinkRegExp = /\[\[\s*([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s*\]\]/g

 module.exports = function (options) {
-    return ({ collections, page }) => {
+    return async ({ collections, page }) => {
         const { notes } = collections;

         const fileStem = page.filePathStem.replace(`/${options.folder}/`, '');
@@ -15,7 +15,7 @@ module.exports = function (options) {

         // Search the other notes for backlinks
         for (const otherNote of notes) {
-            const noteContent = otherNote.template.frontMatter.content;
+            const noteContent = (await otherNote.template.read()).content;

             // Get all links from otherNote
             const outboundLinks = (noteContent.match(wikilinkRegExp) || [])

This issue body was partially generated by patch-package.