From bd31703d8627d3c8e046d62223a08d295f0e9578 Mon Sep 17 00:00:00 2001
From: "Thomas F. K. Jorna" <thomasfkjorna@gmail.com>
Date: Mon, 16 Aug 2021 19:09:53 +0200
Subject: fix: #67, correctly skip non-node headlines

---
 pages/index.tsx | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

(limited to 'pages/index.tsx')

diff --git a/pages/index.tsx b/pages/index.tsx
index 12f9f74..21a0738 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -113,25 +113,22 @@ export function GraphPage() {
       }
       return headingNodes.map((headingNode) => {
         const smallerHeadings = nodesInFile.filter((node) => {
-          if (node.level >= headingNode.level) {
+          if (
+            node.level >= headingNode.level ||
+            node.pos >= headingNode.pos ||
+            !headingNode.olp?.includes(node.title) ||
+            node.level >= headingNode.level - headingNode.olp.reverse().indexOf(node.title)
+          ) {
             return false
           }
           return true
         })
-        const smallerPos = smallerHeadings.map((node) => {
-          if (node.pos >= headingNode.pos) {
-            return 0
-          }
-          return node.pos
-        })
 
-        const target = nodesInFile.find((node) => {
-          return node.pos === Math.max(...smallerPos)
-        })
+        const target = smallerHeadings.slice(-1)[0]
 
         return {
           source: headingNode.id,
-          target: target!.id,
+          target: target?.id || fileNode.id,
           type: 'heading',
         }
       })
@@ -173,6 +170,7 @@ export function GraphPage() {
           title: sourceId,
           level: 0,
           pos: 0,
+          olp: null,
         })
         return { ...link, type: 'bad' }
       }
@@ -185,6 +183,7 @@ export function GraphPage() {
           title: targetId,
           level: 0,
           pos: 0,
+          olp: null,
         })
         return { ...link, type: 'bad' }
       }
-- 
cgit v1.2.3

