shadowfacts.net/lib/metadata.ts

41 lines
823 B
TypeScript
Raw Normal View History

2019-01-04 18:14:53 +00:00
import { promises as fs } from "fs";
export interface Page {
text: string;
metadata: Metadata;
}
export interface Metadata {
permalink: string;
source?: string;
2020-07-09 18:14:42 +00:00
sourceDir?: string;
2019-01-04 18:14:53 +00:00
layout?: string;
oldPermalink?: string;
}
export interface PostMetadata extends Metadata {
title: string;
slug: string;
2020-08-29 16:09:42 +00:00
tags: string[];
2021-04-12 22:31:03 +00:00
tagSlugs: string[];
2019-01-04 18:14:53 +00:00
date: string | Date;
2019-06-29 20:01:05 +00:00
readingTime?: number;
2019-11-12 02:28:52 +00:00
wordCount?: number;
2019-01-04 18:14:53 +00:00
excerpt?: string;
2019-02-18 18:47:43 +00:00
uuid: string;
2019-01-04 18:14:53 +00:00
}
export async function get(path: string): Promise<Page> {
let text = (await fs.readFile(path)).toString();
let metadata = {
source: path
} as Metadata;
if (text.startsWith("```")) {
const parts = text.split("```");
text = parts.slice(2).join("```");
const configure = new Function("metadata", parts[1]);
configure(metadata);
}
return {text, metadata};
2019-11-12 02:28:52 +00:00
}