From fa3bc3de9273d2cb437605d1b229d9a8d79331b5 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Fri, 11 Feb 2022 22:24:54 +0000
Subject: [PATCH] Merge pull request #48 from earnestma/earne/configurable-page-toc

---
 layouts/partials/graph.html |   40 ++++++++++++++++++++++++++++------------
 1 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/layouts/partials/graph.html b/layouts/partials/graph.html
index 7dcd536..10243bb 100644
--- a/layouts/partials/graph.html
+++ b/layouts/partials/graph.html
@@ -11,17 +11,35 @@
     }
 </style>
 <script>
-  const index = {{$.Site.Data.linkIndex.index}}
-  const links = {{$.Site.Data.linkIndex.links}}
-  const content = {{$.Site.Data.contentIndex}}
-  const curPage = {{ strings.TrimRight "/" .Page.RelPermalink }}
+  const curPage = {{ strings.TrimRight "/" .Page.Permalink }}.replace({{strings.TrimRight "/" .Site.BaseURL }}, "")
   const pathColors = {{$.Site.Data.graphConfig.paths}}
+  let depth = {{$.Site.Data.graphConfig.depth}}
 
   const parseIdsFromLinks = (links) => [...(new Set(links.flatMap(link => ([link.source, link.target]))))]
 
+  const neighbours = new Set()
+  const wl = [curPage || "/", "__SENTINEL"]
+  if (depth >= 0) {
+    while (depth >= 0 && wl.length > 0) {
+      // compute neighbours
+      const cur = wl.shift()
+      if (cur === "__SENTINEL") {
+        depth--
+        wl.push("__SENTINEL")
+      } else {
+        neighbours.add(cur)
+        const outgoing = index.links[cur] || []
+        const incoming = index.backlinks[cur] || []
+        wl.push(...outgoing.map(l => l.target), ...incoming.map(l => l.source))
+      }
+    }
+  } else {
+    parseIdsFromLinks(links).forEach(id => neighbours.add(id))
+  }
+
   const data = {
-    nodes: parseIdsFromLinks(links).map(id => ({id})),
-    links,
+    nodes: [...neighbours].map(id => ({id})),
+    links: links.filter(l => neighbours.has(l.source) && neighbours.has(l.target)),
   }
 
   const color = (d) => {
@@ -70,10 +88,8 @@
   const width = document.getElementById("graph-container").offsetWidth
 
   const simulation = d3.forceSimulation(data.nodes)
-    .force("charge", d3.forceManyBody().strength(-20))
-    .force("link", d3.forceLink(data.links)
-      .id(d => d.id)
-    )
+    .force("charge", d3.forceManyBody().strength(-30))
+    .force("link", d3.forceLink(data.links).id(d => d.id))
     .force("center", d3.forceCenter());
 
   const svg = d3.select('#graph-container')
@@ -127,7 +143,7 @@
     .attr("fill", color)
     .style("cursor", "pointer")
     .on("click", (_, d) => {
-      window.location.href = {{.Site.BaseURL}} + d.id.replace(" ", "-").replace("%20", "-");
+      window.location.href = {{.Site.BaseURL}} + decodeURI(d.id).replace(/\s+/g, '-')
     })
     .on("mouseover", function (_, d) {
       d3.selectAll(".node")
@@ -185,7 +201,7 @@
   const labels = graphNode.append("text")
     .attr("dx", 12)
     .attr("dy", ".35em")
-    .text((d) => content[d.id.replace("%20", "-")]?.title || "Untitled")
+    .text((d) => content[decodeURI(d.id).replace(/\s+/g, '-')]?.title || "Untitled")
     .style("opacity", 0)
     .style("pointer-events", "none")
     .call(drag(simulation));

--
Gitblit v1.10.0