From 3bb6041df03f59c8188228895fd303b2583c377d Mon Sep 17 00:00:00 2001 From: Utsob Roy Date: Wed, 1 Feb 2023 16:14:50 +0600 Subject: [PATCH] fixed backlink --- src/helpers/linkUtils.js | 76 +++++++++++++++++--------------- src/site/index.11tydata.js | 3 +- src/site/notes/notes.11tydata.js | 2 + 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/helpers/linkUtils.js b/src/helpers/linkUtils.js index 630dfe1..c563250 100644 --- a/src/helpers/linkUtils.js +++ b/src/helpers/linkUtils.js @@ -1,6 +1,10 @@ const wikiLinkRegex = /\[\[(.*?\|.*?)\]\]/g; const internalLinkRegex = /href="\/(.*?)"/g; +function caselessCompare(a, b) { + return a.toLowerCase() === b.toLowerCase(); +} + function extractLinks(content) { return [ ...(content.match(wikiLinkRegex) || []).map( @@ -26,47 +30,47 @@ function extractLinks(content) { ]; } -function shuffle(a) { - var j, x, i; - for (i = a.length - 1; i > 0; i--) { - j = Math.floor(Math.random() * (i + 1)); - x = a[i]; - a[i] = a[j]; - a[j] = x; +function getBacklinks(data) { + const notes = data.collections.note; + if (!notes) { + return []; } - return a; -} + const currentFileSlug = data.page.filePathStem + .replace("/notes/", "") + .split("#")[0]; + const currentURL = data.page.url; -function sliceIntoChunks(arr, chunkSize) { - const res = []; - for (let i = 0; i < arr.length; i += chunkSize) { - const chunk = arr.slice(i, i + chunkSize); - res.push(chunk); + let backlinks = []; + let uniqueLinks = new Set(); + let counter = 1; + + for (const otherNote of notes) { + const noteContent = otherNote.template.frontMatter.content; + const backLinks = extractLinks(noteContent); + + if ( + !uniqueLinks.has(otherNote.url) && + backLinks.some( + (link) => + caselessCompare(link, currentFileSlug) || + currentURL == link.split("#")[0] + ) + ) { + let preview = noteContent.slice(0, 240); + backlinks.push({ + url: otherNote.url, + title: otherNote.data.title || otherNote.data.page.fileSlug, + preview, + id: counter++, + isHome: otherNote.data["dg-home"] || false, + }); + uniqueLinks.add(otherNote.url); + } } - return res; -} - -function getPositions(trees) { - let minInRow = Math.floor(Math.sqrt(trees.length)); - let maxInRow = Math.ceil(Math.sqrt(trees.length)); - if (minInRow < maxInRow) { - trees = trees.concat( - Array(Math.pow(maxInRow, 2) - trees.length).fill([0, "", ""]) - ); - } - trees = shuffle([...trees]); - let levels = sliceIntoChunks(trees, maxInRow); - return levels; -} - -function forestData(data) { - const canvasTrees = data.collections.note.map((n) => { - return [n.data.maturity || 1, n.url, n.data.title || n.fileSlug]; - }); - return getPositions(canvasTrees); + return backlinks; } exports.wikiLinkRegex = wikiLinkRegex; exports.internalLinkRegex = internalLinkRegex; exports.extractLinks = extractLinks; -exports.forestData = forestData; +exports.getBacklinks = getBacklinks; diff --git a/src/site/index.11tydata.js b/src/site/index.11tydata.js index 2a5974f..164a9e4 100644 --- a/src/site/index.11tydata.js +++ b/src/site/index.11tydata.js @@ -5,11 +5,12 @@ const markdownIt = require("markdown-it"); const md = markdownIt({ html: true, }).use(require("../helpers/utils").namedHeadingsFilter); - +const { getBacklinks } = require("../helpers/linkUtils"); const allSettings = settings.ALL_NOTE_SETTINGS; module.exports = { eleventyComputed: { + backlinks: (data) => getBacklinks(data), settings: (data) => { const currentnote = data.collections.gardenEntry && data.collections.gardenEntry[0]; diff --git a/src/site/notes/notes.11tydata.js b/src/site/notes/notes.11tydata.js index c036a74..f3b4049 100644 --- a/src/site/notes/notes.11tydata.js +++ b/src/site/notes/notes.11tydata.js @@ -1,10 +1,12 @@ require("dotenv").config(); const settings = require("../../helpers/constants"); +const { getBacklinks } = require("../../helpers/linkUtils"); const allSettings = settings.ALL_NOTE_SETTINGS; module.exports = { eleventyComputed: { + backlinks: (data) => getBacklinks(data), settings: (data) => { const noteSettings = {}; allSettings.forEach((setting) => {