single computation for both sidebar page mentions and graph

This commit is contained in:
Utsob Roy 2023-02-02 10:41:46 +06:00
parent 3bb6041df0
commit 5fcee3da0b
8 changed files with 89 additions and 109 deletions

View File

@ -30,47 +30,63 @@ function extractLinks(content) {
];
}
function getBacklinks(data) {
const notes = data.collections.note;
if (!notes) {
return [];
}
const currentFileSlug = data.page.filePathStem
.replace("/notes/", "")
.split("#")[0];
const currentURL = data.page.url;
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);
function getGraph(data) {
let nodes = {};
let links = [];
let stemURLs = {};
let homeAlias = "/";
data.collections.note.forEach((v, idx) => {
let fpath = v.filePathStem.replace("/notes/", "");
let parts = fpath.split("/");
let group = "none";
if (parts.length >= 3) {
group = parts[parts.length - 2];
}
}
return backlinks;
nodes[v.url] = {
id: idx,
title: v.data.title || v.fileSlug,
url: v.url,
group,
home: v.data["dg-home"] || false,
outBound: extractLinks(v.template.frontMatter.content),
neighbors: new Set(),
backLinks: new Set(),
};
stemURLs[fpath] = v.url;
if (v.data["dg-home"]) {
homeAlias = v.url;
}
});
Object.values(nodes).forEach((node) => {
let outBound = new Set();
node.outBound.forEach((olink) => {
let link = (stemURLs[olink] || olink).split("#")[0];
outBound.add(link);
});
node.outBound = Array.from(outBound);
node.outBound.forEach((link) => {
let n = nodes[link];
if (n) {
n.neighbors.add(node.url);
n.backLinks.add(node.url);
node.neighbors.add(n.url);
links.push({ source: node.id, target: n.id });
}
});
});
Object.keys(nodes).map((k) => {
nodes[k].neighbors = Array.from(nodes[k].neighbors);
nodes[k].backLinks = Array.from(nodes[k].backLinks);
nodes[k].size = nodes[k].neighbors.length;
});
return {
homeAlias,
nodes,
links,
};
}
exports.wikiLinkRegex = wikiLinkRegex;
exports.internalLinkRegex = internalLinkRegex;
exports.extractLinks = extractLinks;
exports.getBacklinks = getBacklinks;
exports.getGraph = getGraph;

View File

@ -0,0 +1,5 @@
const { getGraph } = require("../../helpers/linkUtils");
module.exports = {
graph: (data) => getGraph(data),
}

View File

@ -102,7 +102,7 @@
}
function fetchGraphData() {
fetch('/graph').then(res => res.json()).then(data => {
fetch('/graph.json').then(res => res.json()).then(data => {
window.graphData = data;
Graph = renderGraph(filterToDepth(JSON.parse(JSON.stringify(data))), "link-graph", 330, 330);
});

View File

@ -44,25 +44,41 @@
{%endif%}
{%if settings.dgShowBacklinks === true %}
{%if settings.dgShowBacklinks === true %}
<div class="backlinks">
<div class="backlink-title" style="margin: 4px 0 !important;">Pages mentioning this page</div>
<div class="backlink-list">
{%- if backlinks.length === 0 -%}
{%- if page.url == "/" -%}
{%- if graph.nodes[graph.homeAlias].backLinks.length === 0 -%}
<div class="backlink-card">
<span class="no-backlinks-message">No other pages mentions this page</span>
</div>
{%- endif -%}
{%- for backlink in graph.nodes[graph.homeAlias].backLinks -%}
{%- if graph.nodes[backlink].url != graph.homeAlias -%}
<div class="backlink-card">
<span class="no-backlinks-message">No other pages mentions this page</span>
</div>
{%- endif -%}
{%- for backlink in backlinks -%}
<div class="backlink-card">
<i class="fa fa-link"></i> <a href="{{backlink.url}}">{{backlink.title}}</a>
<i class="fa fa-link"></i> <a href="{{graph.nodes[backlink].url}}">{{graph.nodes[backlink].title}}</a>
</div>
{%- endif -%}
{%- endfor -%}
{%- else -%}
{%- if graph.nodes[page.url].backLinks.length === 0 -%}
<div class="backlink-card">
<span class="no-backlinks-message">No other pages mentions this page</span>
</div>
{%- endif -%}
{%- for backlink in graph.nodes[page.url].backLinks -%}
{%- if graph.nodes[backlink].url != page.url -%}
<div class="backlink-card">
<i class="fa fa-link"></i> <a href="{{graph.nodes[backlink].url}}">{{graph.nodes[backlink].title}}</a>
</div>
{%- endif -%}
{%- endfor -%}
{%- endif -%}
</div>
</div>
</div>
{%endif%}
{%endif%}
</div>
</div>
</div>

View File

@ -1,53 +0,0 @@
const { extractLinks } = require("../helpers/linkUtils");
module.exports = {
eleventyComputed: {
graphData: (data) => {
let nodes = {};
let links = [];
let stemURLs = {};
data.collections.note.forEach((v, idx) => {
let fpath = v.filePathStem.replace("/notes/", "");
let parts = fpath.split("/");
let group = "none";
if (parts.length >= 3) {
group = parts[parts.length - 2];
}
nodes[v.url] = {
id: idx,
title: v.data.title || v.fileSlug,
url: v.url,
group,
home: v.data["dg-home"] || false,
outBound: extractLinks(v.template.frontMatter.content),
neighbors: new Set(),
};
stemURLs[fpath] = v.url;
});
Object.values(nodes).forEach((node) => {
let outBound = new Set();
node.outBound.forEach((olink) => {
let link = (stemURLs[olink] || olink).split("#")[0];
outBound.add(link);
});
node.outBound = Array.from(outBound);
node.outBound.forEach((link) => {
let n = nodes[link];
if (n) {
n.neighbors.add(node.url);
node.neighbors.add(n.url);
links.push({ source: node.id, target: n.id });
}
});
});
Object.keys(nodes).map((k) => {
nodes[k].neighbors = Array.from(nodes[k].neighbors);
nodes[k].size = nodes[k].neighbors.length;
});
return JSON.stringify({
nodes,
links,
});
},
},
};

View File

@ -1,5 +1,5 @@
---
permalink: /graph
permalink: /graph.json
eleventyExcludeFromCollections: true
---
{{ graphData | safe }}
{{ graph | jsonify | safe }}

View File

@ -5,12 +5,10 @@ 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,12 +1,10 @@
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) => {