fixed backlink

This commit is contained in:
Utsob Roy 2023-02-01 16:14:50 +06:00
parent 24d7bdce5b
commit 3bb6041df0
3 changed files with 44 additions and 37 deletions

View File

@ -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;

View File

@ -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];

View File

@ -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) => {