making the fics loader prettier
This commit is contained in:
parent
ebcc2bf819
commit
6a9ae7237d
@ -4,6 +4,8 @@ publishedAt: 2020-12-20T19:18:00
|
||||
notes: i wrote this in a fugue state while listening to [Waste by Oh Wonder](https://www.youtube.com/watch?v=Ar1grAdGkec)
|
||||
---
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
|
||||
|
39
src/utils/ficsLoader.ts
Normal file
39
src/utils/ficsLoader.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { type Loader, type LoaderContext } from "astro/loaders";
|
||||
import { parseFrontmatter } from '@astrojs/markdown-remark';
|
||||
import { readdirSync } from "fs";
|
||||
import { glob } from "fs/promises";
|
||||
|
||||
export function ficsLoader(options: { path: string }) {
|
||||
return {
|
||||
name: "fics",
|
||||
async load(context: LoaderContext) {
|
||||
const fics = readdirSync(options.path, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
for (const fic of fics) {
|
||||
const { content } = parseFrontmatter(fic, { frontmatter: "empty-with-spaces" });
|
||||
console.log(content);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function test(options: { path: string }) {
|
||||
const fics = readdirSync(options.path, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => `${dirent.parentPath}/${dirent.name}`);
|
||||
|
||||
for (const fic of fics) {
|
||||
const entries = await Array.fromAsync(glob([`${fic}/*.yaml`, `${fic}/*.yml`, `${fic}/*.toml`, `${fic}/*.json`]));
|
||||
console.log(entries);
|
||||
const chapterPaths = await Array.fromAsync(glob(fic + '/*.md'));
|
||||
const chapters = chapterPaths.map(chapter => chapter);
|
||||
// const files
|
||||
// console.log("" + chapters)
|
||||
// const { content } = parseFrontmatter(fic, { frontmatter: "empty-with-spaces" });
|
||||
// console.log(content);
|
||||
}
|
||||
}
|
||||
|
||||
test({ path: "./src/content/fics" });
|
@ -3,7 +3,11 @@ import { statSync } from "fs";
|
||||
export function modifiedTime() {
|
||||
return function (_tree, file) {
|
||||
const path = file.history[0];
|
||||
try {
|
||||
const result = statSync(path);
|
||||
file.data.astro.frontmatter.lastModified = result.mtime;
|
||||
} catch (error) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +1,8 @@
|
||||
import type { MarkdownInstance } from "astro";
|
||||
import type { Loader, LoaderContext } from "astro/loaders";
|
||||
import { parseFrontmatter } from '@astrojs/markdown-remark';
|
||||
import path from "path";
|
||||
import { glob } from "fs/promises";
|
||||
|
||||
async function getAllChapters(metaPath: string) {
|
||||
const entryPath = path.parse(metaPath);
|
||||
const fic = entryPath.dir;
|
||||
const entries = await Array.fromAsync(glob(fic + '/*.md'));
|
||||
const chapters = entries.map(chapter => path.relative(fic, chapter));
|
||||
return chapters.map(chapter => `${fic.split("/").at(-1)}/${path.parse(chapter).name}`);
|
||||
}
|
||||
import { glob, readFile } from "fs/promises";
|
||||
|
||||
export function ficsLoader(loader: Loader) {
|
||||
const oldLoad = loader.load;
|
||||
@ -47,8 +40,13 @@ async function resolveFics(loader: (oldContext: LoaderContext) => Promise<void>,
|
||||
if (valueWithoutDigest.data['oneshot'] === true) {
|
||||
// i've committed unspeakable atrocities here
|
||||
const search = import.meta.glob<MarkdownInstance<any>>(`../content/fics/**/*.md`, { eager: true });
|
||||
const body = Object.values(search).filter(path => path.file?.includes(chapters[0]))[0];
|
||||
const onlyChapter = chapters[0];
|
||||
const includedChapter = (path: MarkdownInstance<any>) => path.file?.includes(onlyChapter);
|
||||
const [body] = Object.values(search).filter(includedChapter);
|
||||
const html = await body.compiledContent();
|
||||
// following could be good for being way more forgiving of paths
|
||||
// const { content, frontmatter } = await readChapterFile(value.filePath as string, chapters);
|
||||
// const test = await context.renderMarkdown(content);
|
||||
context.store.set({
|
||||
...valueWithoutDigest,
|
||||
data: newData,
|
||||
@ -76,3 +74,26 @@ async function resolveFics(loader: (oldContext: LoaderContext) => Promise<void>,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function getAllChapters(metaPath: string) {
|
||||
const entryPath = path.parse(metaPath);
|
||||
const fic = entryPath.dir;
|
||||
const ficFolder = fic.split("/").at(-1);
|
||||
const entries = await Array.fromAsync(glob(fic + '/*.md'));
|
||||
const chapters = entries.map(chapter => path.relative(fic, chapter));
|
||||
return chapters.map(chapter => `${ficFolder}/${chapter}`);
|
||||
}
|
||||
|
||||
// unused for now
|
||||
async function readChapterFile(folderPath: string, chapters: string[]) {
|
||||
const folder = (folderPath)
|
||||
.split("/")
|
||||
.slice(0, -2) // we only want the stuff before fic folder
|
||||
.join("/"); // reconnect as string
|
||||
const onlyChapter = chapters[0];
|
||||
const filePath = path.resolve(folder, onlyChapter);
|
||||
const [search] = await Array.fromAsync(glob(`${filePath}`));
|
||||
const fileContent = await readFile(search, "utf8");
|
||||
const result = parseFrontmatter(fileContent);
|
||||
return result;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user