From 233e5ba4ed0413f9946a85ade9ca1e55fcfb7095 Mon Sep 17 00:00:00 2001 From: Tuan Cao Date: Fri, 29 Apr 2022 15:35:10 +0700 Subject: [PATCH] Remove troublesome FrontMatter pluggin (temporarily) Cached graph-data, which result in hugely improved build performance and probably browsing experience as well --- lib/transformer.js | 2 +- lib/utils.js | 69 ++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/transformer.js b/lib/transformer.js index c180570..a689a3c 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -65,7 +65,7 @@ export const Transformer = { .use(obsidianImage) .use(highlight) .use(externalLinks, {target: "_blank", rel: ['noopener']}) - .use(frontmatter, ['yaml', 'toml']) + // .use(frontmatter, ['yaml', 'toml']) .use(wikiLinkPlugin, { permalinks: null, pageResolver: function (pageName) { diff --git a/lib/utils.js b/lib/utils.js index c812ba9..231d649 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,6 +3,8 @@ import {Transformer} from "./transformer"; import unified from "unified"; import markdown from "remark-parse"; import {toString} from 'mdast-util-to-string' +import path from "path"; +import fs from "fs"; const dirTree = require("directory-tree"); @@ -46,7 +48,7 @@ export function getSinglePost(slug) { //console.log("hrmlcontents and backlinks") return { id: slug, - ...currentFileFrontMatter, + // ...currentFileFrontMatter, data: htmlContent, } @@ -100,41 +102,48 @@ export function toSlug(filePath) { } + export function constructGraphData() { - const filePaths = getAllMarkdownFiles(); - const edges = [] - const nodes = [] + const filepath = path.join(process.cwd(), "graph-data.json"); + if (Node.isFile(filepath)) { + const data = fs.readFileSync(filepath); + return JSON.parse(String(data)) + } else { + const filePaths = getAllMarkdownFiles(); + const edges = [] + const nodes = [] + filePaths + .forEach(aFilePath => { + // const {currentFilePath} = getFileNames(filename) + const aNode = { + title: Transformer.parseFileNameFromPath(aFilePath), + slug: toSlug(aFilePath), + shortSummary: getShortSummary(toSlug(aFilePath)) + } + nodes.push(aNode) - filePaths - .forEach(aFilePath => { - // const {currentFilePath} = getFileNames(filename) - const aNode = { - title: Transformer.parseFileNameFromPath(aFilePath), - 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 => { - // console.log("Constructing graph for node: " + aFilePath ) - const internalLinks = Transformer.getInternalLinks(aFilePath) - internalLinks.forEach(aLink => { + if (aLink.slug === null || aLink.slug.length === 0) return - if (aLink.slug === null || aLink.slug.length === 0) return - - const anEdge = { - source: toSlug(aFilePath), - target: aLink.slug, + const anEdge = { + source: toSlug(aFilePath), + target: aLink.slug, + } + edges.push(anEdge) + // console.log("Source: " + anEdge.source) + // console.log("Target: " + anEdge.target) + }) + // console.log("==============Constructing graph" ) } - edges.push(anEdge) - // console.log("Source: " + anEdge.source) - // console.log("Target: " + anEdge.target) - }) - // console.log("==============Constructing graph" ) - } - ) - - return {nodes, edges}; + ) + const data = {nodes, edges}; + fs.writeFileSync(filepath, JSON.stringify(data), "utf-8"); + return data; + } }