wiki base, middleware
This commit is contained in:
parent
db1c07a3cf
commit
22ba0c7cf6
16 changed files with 2203 additions and 263 deletions
lib/wiki
70
lib/wiki/api.ts
Normal file
70
lib/wiki/api.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
import { default as pathlib } from "path";
|
||||
import fg from "fast-glob";
|
||||
import fs from "fs";
|
||||
import matter from "gray-matter";
|
||||
|
||||
const wikiDir = pathlib.join(process.cwd(), "InvestWiki/wiki");
|
||||
|
||||
const getAllFiles = (path: string) => {
|
||||
const files = fg.sync("**/*.md", {
|
||||
cwd: path,
|
||||
onlyFiles: true,
|
||||
absolute: false,
|
||||
});
|
||||
return files;
|
||||
};
|
||||
|
||||
function getAllWikiPaths() {
|
||||
const files = getAllFiles(wikiDir);
|
||||
// manipulate array entries, remove the .md extension and move it to the front of the string
|
||||
const paths = files.map((file: string) => {
|
||||
const path = file.replace(".md", "");
|
||||
// move the last part of the path to the front
|
||||
const pathParts = path.split("/");
|
||||
const lastPart = pathParts.pop();
|
||||
if (lastPart) {
|
||||
pathParts.unshift(lastPart);
|
||||
}
|
||||
return pathParts.join("/");
|
||||
});
|
||||
return paths;
|
||||
}
|
||||
|
||||
function getWikiPath(lang: string, path: string) {
|
||||
const files = getAllFiles(wikiDir);
|
||||
// filter the files to only include the ones in the path
|
||||
const pageFiles = files.filter(
|
||||
(file) => file.split("/").slice(0, -1).join("/") === path
|
||||
);
|
||||
|
||||
let pagePath = "";
|
||||
// do https://github.com/vercel/next.js/blob/canary/examples/blog-starter/lib/api.ts
|
||||
if (pageFiles.length !== 0) {
|
||||
// check if there is a file with the language code
|
||||
const langFile = pageFiles.find((file) => file.includes(`${lang}.md`));
|
||||
if (langFile) {
|
||||
pagePath = langFile;
|
||||
} else {
|
||||
// otherwise, use the english file if it exists, otherwise return the first file
|
||||
pagePath =
|
||||
pageFiles.find((file) => file.includes("en.md")) ?? pageFiles[0];
|
||||
}
|
||||
}
|
||||
return pagePath;
|
||||
}
|
||||
|
||||
function getWikiContent(lang: string, path: string) {
|
||||
// get the path to the file
|
||||
const relativePath = getWikiPath(lang, path);
|
||||
if (!relativePath) {
|
||||
return null;
|
||||
}
|
||||
const filePath = pathlib.join(wikiDir, relativePath);
|
||||
// read the file
|
||||
const fileContents = fs.readFileSync(filePath, "utf8");
|
||||
const { data, content } = matter(fileContents);
|
||||
|
||||
return { data, content };
|
||||
}
|
||||
|
||||
export { getWikiPath, getAllWikiPaths, getWikiContent };
|
Loading…
Add table
Add a link
Reference in a new issue