29 lines
No EOL
1.2 KiB
TypeScript
29 lines
No EOL
1.2 KiB
TypeScript
import { PageInfo } from "./page.ts";
|
|
import fm, { type FrontMatterResult } from 'front-matter';
|
|
import {existsSync} from "https://deno.land/std/fs/mod.ts";
|
|
|
|
export async function AnalyzeMarkdown(file: string | URL): Promise<PageInfo> {
|
|
if (!existsSync(file))
|
|
throw new Error("File not found: " + file)
|
|
|
|
const contents = new TextDecoder().decode(Deno.readFileSync(file))
|
|
const stat = await Deno.stat(file);
|
|
|
|
const fm_result: FrontMatterResult<any> = fm(contents);
|
|
|
|
const info: PageInfo = {
|
|
created: fm_result.attributes.created ?
|
|
fm_result.attributes.created : new Date(),
|
|
edited: stat.mtime ?
|
|
stat.mtime : new Date(),
|
|
author: fm_result.attributes.author ? fm_result.attributes.author : null,
|
|
title: fm_result.attributes.title ? fm_result.attributes.title : null,
|
|
description: fm_result.attributes.description ? fm_result.attributes.description : null,
|
|
tags: fm_result.attributes.tags ? fm_result.attributes.tags : null,
|
|
categories: fm_result.attributes.categories ? fm_result.attributes.categories : null,
|
|
url: file as URL,
|
|
wordCount: fm_result.body.trim().split(/\s+/).length
|
|
};
|
|
|
|
return info;
|
|
} |