From ce8de2da9fb57e23c1e8657da95f6344eb368b39 Mon Sep 17 00:00:00 2001 From: Tuan Cao Date: Mon, 18 Apr 2022 11:48:36 +0700 Subject: [PATCH] - fix bug: missing nodes in global graph - fix bug: mismatch mapping node in index page (/) - local graph now include both backlink and forward link --- lib/transformer.js | 12 +++++--- lib/utils.js | 75 +++++++++++++++++++++++++++++----------------- pages/index.js | 4 +-- pages/note/[id].js | 6 ++-- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/lib/transformer.js b/lib/transformer.js index 91366ec..029837f 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -44,6 +44,8 @@ export const Transformer = { if (result === undefined || result.length === 0) { console.log("Cannot resolve file path " + pageName) } + + // console.log("Internal Link resolved: [" + pageName + "] ==> [" + temp[0] +"]") return (result !== undefined && result.length > 0) ? [toSlug(result)] : ["/"] }, hrefTemplate: function (permalink) { @@ -122,8 +124,6 @@ export const Transformer = { }, /* Pair provided and existing Filenames*/ getInternalLinks: function (aFilePath) { - // const filePaths = Node.getFiles(postsDirectory); - // const currentFilePath = Transformer.pairCurrentFile(aFilePath, filePaths) const fileContent = Node.readFileSync(aFilePath); const internalLinks = [] const sanitizedContent = Transformer.preprocessThreeDashes(fileContent) @@ -155,11 +155,10 @@ export const Transformer = { shortSummary: canonicalSlug } - if (canonicalSlug != null && internalLinks.find(aLink => aLink.slug === canonicalSlug) == null) { + if (canonicalSlug != null && internalLinks.indexOf(canonicalSlug) < 0) { internalLinks.push(backLink); } - return [canonicalSlug] } , @@ -172,6 +171,11 @@ export const Transformer = { .use(html) .processSync(sanitizedContent) + // console.log("Internal Links of: " + aFilePath) + // internalLinks.forEach(aLink => { + // console.log(aLink.title + " --> " + aLink.slug) + // }) + // console.log("===============Internal Links") return internalLinks; } } diff --git a/lib/utils.js b/lib/utils.js index dbf44a4..c7ff7ea 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -29,8 +29,9 @@ export function getShortSummary(slug) { export function getAllMarkdownFiles() { return Node.getFiles(Node.getMarkdownFolder()) } + export function getSinglePost(slug) { - console.log("\n\nFile is scanning: ", slug) + // List of filenames that will provide existing links to wikilink let currentFilePath = slug !== "index" ? toFilePath(slug) : Node.getMarkdownFolder() + "/index.md" @@ -39,8 +40,9 @@ export function getSinglePost(slug) { var fileContent = Node.readFileSync(currentFilePath) const currentFileFrontMatter = Transformer.getFrontMatterData(fileContent) - + // console.log("===============\n\nFile is scanning: ", slug) const [htmlContent] = Transformer.getHtmlContent(fileContent) + // console.log("==================================") //console.log("hrmlcontents and backlinks") return { id: slug, @@ -52,15 +54,15 @@ export function getSinglePost(slug) { export function toFilePath(slug) { // Construct file name from slug of /notes/abcxyz - let filePath ; + let filePath; if (slug === '/') { - filePath = Node.getMarkdownFolder() + "/index.md" - } else { + filePath = Node.getMarkdownFolder() + "/index.md" + } else { filePath = Node.getMarkdownFolder() + slug - .replaceAll('__','/') - .replaceAll('--',' ') - .replaceAll('ambersand','&') + .replaceAll('__', '/') + .replaceAll('--', ' ') + .replaceAll('ambersand', '&') + ".md"; } @@ -76,9 +78,9 @@ export function toSlug(filePath) { if (Node.isFile(filePath) && filePath.includes(Node.getMarkdownFolder())) { return filePath.replace(Node.getMarkdownFolder(), '') - .replaceAll('/','__') - .replaceAll(' ','--') - .replaceAll('&','ambersand') + .replaceAll('/', '__') + .replaceAll(' ', '--') + .replaceAll('&', 'ambersand') .replace('.md', '') } else { //TODO handle this properly @@ -88,30 +90,36 @@ export function toSlug(filePath) { } -export function constructBackLinks() { +export function constructGraphData() { const filePaths = getAllMarkdownFiles(); const edges = [] const nodes = [] - filePaths.forEach(filename => { + filePaths + .forEach(aFilePath => { // const {currentFilePath} = getFileNames(filename) - const internalLinks = Transformer.getInternalLinks(filename) + const aNode = { + slug: toSlug(aFilePath), + shortSummary: getShortSummary(toSlug(aFilePath)) + } + nodes.push(aNode) + + console.log("Constructing graph for node: " + aFilePath ) + const internalLinks = Transformer.getInternalLinks(aFilePath) internalLinks.forEach(aLink => { if (aLink.slug === null || aLink.slug.length === 0) return const anEdge = { - source: toSlug(filename), + source: toSlug(aFilePath), target: aLink.slug, } edges.push(anEdge) - if (nodes.findIndex(aNode => aNode.slug === aLink.slug) === -1) { - aLink.shortSummary = getShortSummary(aLink.slug) - // console.log(aLink.shortSummary) - nodes.push(aLink) - } + console.log("Source: " + anEdge.source) + console.log("Target: " + anEdge.target) }) + console.log("==============Constructing graph" ) } ) @@ -119,9 +127,9 @@ export function constructBackLinks() { } -export function getGraphData(currentNodeId) { +export function getLocalGraphData(currentNodeId) { - const {nodes, edges} = constructBackLinks() + const {nodes, edges} = constructGraphData() const newNodes = nodes.map(aNode => ( { @@ -140,18 +148,29 @@ export function getGraphData(currentNodeId) { })) - const existingNodeIDs = newNodes.map(aNode => aNode.data.id) + currentNodeId = currentNodeId === 'index' ? '__index' : currentNodeId if (currentNodeId != null && existingNodeIDs.includes(currentNodeId)) { - const localNodeIDs = newEdges + const outGoingNodeIds = newEdges .filter(anEdge => anEdge.data.source === currentNodeId) .map(anEdge => anEdge.data.target) - localNodeIDs.push(currentNodeId) + const incomingNodeIds = newEdges + .filter(anEdge => anEdge.data.target === currentNodeId) + .map(anEdge => anEdge.data.source) - const localNodes = newNodes.filter(aNode => localNodeIDs.includes(aNode.data.id)) - const localEdges = newEdges.filter(edge => localNodeIDs.includes(edge.data.source)).filter(edge => localNodeIDs.includes(edge.data.target)) + outGoingNodeIds.push(currentNodeId) + const localNodeIds = incomingNodeIds.concat(outGoingNodeIds.filter(item => incomingNodeIds.indexOf(item) < 0)) + if (localNodeIds.indexOf(currentNodeId) < 0) { + localNodeIds.push(currentNodeId) + } + + const localNodes = newNodes.filter(aNode => localNodeIds.includes(aNode.data.id)) + var localEdges = newEdges.filter(edge => localNodeIds.includes(edge.data.source)).filter(edge => localNodeIds.includes(edge.data.target)) + + // Filter self-reference edges + localEdges = localEdges.filter(edge => edge.data.source !== edge.data.target) return { nodes: localNodes, edges: localEdges @@ -189,7 +208,7 @@ export function convertObject(thisObject) { let routerPath = getAllSlugs().find(slug => { const fileName = Transformer.parseFileNameFromPath(toFilePath(slug)) - return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name) + return Transformer.normalizeFileName(fileName) === Transformer.normalizeFileName(thisObject.name) }) || null routerPath = routerPath ? '/note/' + routerPath : null const newObject = { diff --git a/pages/index.js b/pages/index.js index 04adb44..0bbe3c0 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,5 +1,5 @@ import Layout from "../components/Layout"; -import {getSinglePost, getDirectoryData, convertObject, getFlattenArray, getGraphData} from "../lib/utils"; +import {getSinglePost, getDirectoryData, convertObject, getFlattenArray, getLocalGraphData} from "../lib/utils"; import FolderTree from "../components/FolderTree"; import MDContainer from "../components/MDContainer"; import dynamic from 'next/dynamic' @@ -32,7 +32,7 @@ export function getStaticProps() { const tree = convertObject(getDirectoryData()); const contentData = getSinglePost("index"); const flattenNodes = getFlattenArray(tree) - const graphData = getGraphData("index"); + const graphData = getLocalGraphData("index"); return { props: { content: contentData, diff --git a/pages/note/[id].js b/pages/note/[id].js index fba15c4..38500f5 100644 --- a/pages/note/[id].js +++ b/pages/note/[id].js @@ -5,7 +5,7 @@ import { getSinglePost, convertObject, getDirectoryData, - constructBackLinks, getGraphData + constructGraphData, getLocalGraphData } from "../../lib/utils"; import FolderTree from "../../components/FolderTree"; import {getFlattenArray} from "../../lib/utils"; @@ -46,7 +46,7 @@ export async function getStaticPaths() { }; } -const {nodes, edges} = constructBackLinks() +const {nodes, edges} = constructGraphData() export function getStaticProps({params}) { const note = getSinglePost(params.id); @@ -56,7 +56,7 @@ export function getStaticProps({params}) { const listOfEdges = edges.filter(anEdge => anEdge.target === params.id) const internalLinks = listOfEdges.map(anEdge => nodes.find(aNode => aNode.slug === anEdge.source)).filter(element => element !== undefined) - const graphData = getGraphData(params.id) + const graphData = getLocalGraphData(params.id) return { props: { note,