mirror of
https://github.com/tcsenpai/obsidiangarden_netlify.git
synced 2025-07-22 18:10:17 +00:00
fixed backlink
This commit is contained in:
parent
24d7bdce5b
commit
3bb6041df0
@ -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);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
let backlinks = [];
|
||||
let uniqueLinks = new Set();
|
||||
let counter = 1;
|
||||
|
||||
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;
|
||||
}
|
||||
for (const otherNote of notes) {
|
||||
const noteContent = otherNote.template.frontMatter.content;
|
||||
const backLinks = extractLinks(noteContent);
|
||||
|
||||
function forestData(data) {
|
||||
const canvasTrees = data.collections.note.map((n) => {
|
||||
return [n.data.maturity || 1, n.url, n.data.title || n.fileSlug];
|
||||
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,
|
||||
});
|
||||
return getPositions(canvasTrees);
|
||||
uniqueLinks.add(otherNote.url);
|
||||
}
|
||||
}
|
||||
return backlinks;
|
||||
}
|
||||
|
||||
exports.wikiLinkRegex = wikiLinkRegex;
|
||||
exports.internalLinkRegex = internalLinkRegex;
|
||||
exports.extractLinks = extractLinks;
|
||||
exports.forestData = forestData;
|
||||
exports.getBacklinks = getBacklinks;
|
||||
|
@ -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];
|
||||
|
@ -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) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user