From 2acd60899d7bb64a70067e296be9cfba47fb6164 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Sat, 1 Jun 2024 19:24:48 -0500 Subject: [PATCH] Automatically add garden pages --- .gitignore | 2 + .gitmodules | 3 + Garden | 1 + build_garden.js | 140 +++++++ logseq-export/CHANGELOG.md | 9 + logseq-export/LICENSE | 9 + logseq-export/README.md | 128 ++++++ logseq-export/logseq-export | Bin 0 -> 3567616 bytes logseq-export/logseq-export.exe | Bin 0 -> 3817984 bytes package.json | 8 +- yarn.lock | 713 +++++++++++++++++++++++++------- 11 files changed, 851 insertions(+), 162 deletions(-) create mode 160000 Garden create mode 100644 build_garden.js create mode 100644 logseq-export/CHANGELOG.md create mode 100644 logseq-export/LICENSE create mode 100644 logseq-export/README.md create mode 100644 logseq-export/logseq-export create mode 100644 logseq-export/logseq-export.exe diff --git a/.gitignore b/.gitignore index b4e0becd..cf7abae3 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules site/.vitepress/dist site/.vitepress/cache +site/garden/ +garden-output/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index c73e648b..e93f64a9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -32,3 +32,6 @@ path = site/public/planar url = https://github.com/thepaperpilot/planar-pioneers branch = gh-pages +[submodule "Garden.git"] + path = Garden + url = git@code.incremental.social:thepaperpilot/Garden.git diff --git a/Garden b/Garden new file mode 160000 index 00000000..87c5ec9f --- /dev/null +++ b/Garden @@ -0,0 +1 @@ +Subproject commit 87c5ec9f00068af2767b9d42760e78314ca666e3 diff --git a/build_garden.js b/build_garden.js new file mode 100644 index 00000000..5388731a --- /dev/null +++ b/build_garden.js @@ -0,0 +1,140 @@ +const fs = require("fs"); +const path = require("path"); + +function walk(dir, cb) { + const list = fs.readdirSync(dir); + return Promise.all(list.map(file => { + const resolvedFile = path.resolve(dir, file); + const stat = fs.statSync(resolvedFile); + if (stat.isDirectory()) { + return walk(resolvedFile, cb); + } else { + return new Promise((resolve) => cb(dir, resolvedFile, resolve)); + } + })); +} + +function toSlug(string) { + return string.toLowerCase().replaceAll(' ', '-'); +} + +(async () => { + const blockRefs = {}; + await walk("./garden-output/logseq-pages", (dir, file, resolve) => { + const filePath = path.resolve(dir, file); + const data = fs.readFileSync(filePath).toString(); + for (const match of data.matchAll(/- (.*)\n\s*id:: (.*)/gm)) { + const text = match[1]; + const id = match[2]; + blockRefs[id] = `[${text}](/garden/${path.basename(file, ".md")}/index.md#${id})`; + } + resolve(); + }); + + const pageLinks = {}; + const taggedBy = {}; + const tagged = {}; + const referencedBy = {}; + // Walk through the pages to make sure we get the canonical name page (pre-slug) + // The logseq-export README made it sound like even the title property is transformed sometimes + await walk("./Garden/pages", (dir, file, resolve) => { + const filePath = path.resolve(dir, file); + const data = fs.readFileSync(filePath).toString(); + if (!data.match(/public::/g)) { + resolve(); + return; + } + + const name = path.basename(file, ".md").replaceAll('___', '/'); + const slug = toSlug(name); + const link = `/garden/${slug}/index.md`; + pageLinks[name] = link; + + for (match of data.matchAll(/alias:: (.*)/g)) { + match[1].split(", ").forEach(page => (pageLinks[page] = link)); + } + + for (match of data.matchAll(/tags:: (.*)/g)) { + match[1].split(", ").forEach(page => { + const pageSlug = toSlug(page); + taggedBy[pageSlug] = [...(taggedBy[pageSlug] ?? []), name]; + tagged[slug] = [...(tagged[slug] ?? []), page]; + }); + } + + for (match of data.matchAll(/\[\[([^\[\]]*)\]\]/g)) { + const pageSlug = toSlug(match[1]); + referencedBy[pageSlug] = [...(referencedBy[pageSlug] ?? []), name]; + } + + resolve(); + }); + Object.keys(referencedBy).forEach(page => { + referencedBy[page] = Array.from(new Set(referencedBy[page])); + }); + + await walk("./garden-output/logseq-pages", (dir, file, resolve) => { + const filePath = path.resolve(dir, file); + let data = fs.readFileSync(filePath).toString(); + + // Replace youtube embeds + data = data.replaceAll( + /{{video https:\/\/(?:www\.)?youtube\.com\/watch\?v=(.*)}}/g, + '