shadowfacts.net/lib/metadata.ts

39 lines
783 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;
layout?: string;
oldPermalink?: string;
}
export interface PostMetadata extends Metadata {
title: string;
slug: string;
category: string;
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
}