2023-10-30 13:32:14 +02:00
|
|
|
import * as yaml from 'js-yaml';
|
|
|
|
|
2021-12-17 19:37:01 +02:00
|
|
|
const moment = require('moment');
|
|
|
|
|
2023-10-30 13:32:14 +02:00
|
|
|
export interface FrontMatter {
|
2021-12-17 19:37:01 +02:00
|
|
|
created?: Date;
|
2021-12-19 19:24:00 +02:00
|
|
|
updated?: Date;
|
2021-12-17 19:37:01 +02:00
|
|
|
source_url?: string;
|
2022-04-07 16:15:48 +02:00
|
|
|
forum_url?: string;
|
2022-06-03 13:31:42 +02:00
|
|
|
tweet?: string;
|
2023-10-30 13:32:14 +02:00
|
|
|
|
|
|
|
// Docusaurus
|
|
|
|
sidebar_label?: string;
|
|
|
|
sidebar_position?: number;
|
|
|
|
date?: string;
|
2023-10-30 14:57:56 +02:00
|
|
|
title?: string;
|
|
|
|
description?: string;
|
|
|
|
image?: string;
|
2021-12-17 19:37:01 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:32:14 +02:00
|
|
|
export interface MarkdownAndFrontMatter {
|
|
|
|
doc: string;
|
|
|
|
header: FrontMatter;
|
|
|
|
}
|
2021-12-17 19:37:01 +02:00
|
|
|
|
|
|
|
export const stripOffFrontMatter = (md: string): MarkdownAndFrontMatter => {
|
2023-10-30 13:32:14 +02:00
|
|
|
if (md.indexOf('---') !== 0) return { doc: md, header: {} };
|
2021-12-17 19:37:01 +02:00
|
|
|
|
2023-02-05 13:32:28 +02:00
|
|
|
let state = 'start';
|
2021-12-17 19:37:01 +02:00
|
|
|
const lines = md.split('\n');
|
|
|
|
const docLines: string[] = [];
|
2023-10-30 13:32:14 +02:00
|
|
|
const headerLines: string[] = [];
|
2021-12-17 19:37:01 +02:00
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
if (state === 'start') {
|
|
|
|
if (line !== '---') throw new Error('Expected front matter block to start with "---"');
|
|
|
|
state = 'in';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state === 'in') {
|
|
|
|
if (line === '---') {
|
|
|
|
state = 'out';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const propLine = line.trim();
|
2023-10-30 13:32:14 +02:00
|
|
|
headerLines.push(propLine);
|
2021-12-17 19:37:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state === 'out') {
|
|
|
|
if (line.trim()) state = 'doc';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state === 'doc') {
|
|
|
|
docLines.push(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state !== 'doc') throw new Error('Front matter block was not closed with "---"');
|
|
|
|
|
2023-10-30 13:32:14 +02:00
|
|
|
const header: Record<string, any> = yaml.load(headerLines.join('\n'), { schema: yaml.FAILSAFE_SCHEMA });
|
2021-12-17 19:37:01 +02:00
|
|
|
|
2023-10-30 13:32:14 +02:00
|
|
|
if (header.created) header.created = moment(header.created).toDate();
|
|
|
|
if (header.updated) header.updated = moment(header.updated).toDate();
|
|
|
|
if ('sidebar_position' in header) header.sidebar_position = Number(header.sidebar_position);
|
2021-12-17 19:37:01 +02:00
|
|
|
|
2023-10-30 13:32:14 +02:00
|
|
|
return { header, doc: docLines.join('\n') };
|
2021-12-17 19:37:01 +02:00
|
|
|
};
|
2022-04-07 16:15:48 +02:00
|
|
|
|
|
|
|
// ---
|
|
|
|
// created: 2021-07-05T09:42:47.000+00:00
|
|
|
|
// source_url: https://www.patreon.com/posts/any-ideas-for-53317699
|
|
|
|
// ---
|
|
|
|
|
|
|
|
const formatFrontMatterValue = (key: string, value: any) => {
|
|
|
|
if (['created', 'updated'].includes(key)) {
|
|
|
|
return moment((value as Date)).toISOString();
|
|
|
|
} else {
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const compileWithFrontMatter = (md: MarkdownAndFrontMatter): string => {
|
|
|
|
const output: string[] = [];
|
|
|
|
|
2023-10-30 13:32:14 +02:00
|
|
|
if (Object.keys(md.header).length) {
|
|
|
|
const header = { ...md.header };
|
|
|
|
for (const [key, value] of Object.entries(header)) {
|
|
|
|
(header as any)[key] = formatFrontMatterValue(key, value);
|
|
|
|
}
|
|
|
|
const headerString = yaml.dump(header, { noCompatMode: true, schema: yaml.FAILSAFE_SCHEMA, lineWidth: 100000 });
|
2022-04-07 16:15:48 +02:00
|
|
|
output.push('---');
|
2023-10-30 13:32:14 +02:00
|
|
|
output.push(headerString.trim());
|
2022-04-07 16:15:48 +02:00
|
|
|
output.push('---');
|
|
|
|
output.push('');
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push(md.doc);
|
|
|
|
|
|
|
|
return output.join('\n');
|
|
|
|
};
|