-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown.ts
More file actions
80 lines (72 loc) · 2.46 KB
/
markdown.ts
File metadata and controls
80 lines (72 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { PathLike } from 'node:fs';
import { readdir, readFile } from 'node:fs/promises';
import { config } from '../env.js';
/**
* Resolves a path that works in both dev (src/) and prod (dist/) environments
*
* @param devPath - Path relative to the calling file in dev (e.g., './subjects/')
* @param prodPath - Path relative to dist/index.js in prod (e.g., './commands/tips/subjects/')
* @param baseUrl - import.meta.url from the calling file
* @returns URL that works in both environments
*/
export const resolveAssetPath = (devPath: string, prodPath: string, baseUrl: string): URL => {
const isProduction = config.ENV === 'production';
const path = isProduction ? prodPath : devPath;
return new URL(path, baseUrl);
};
/**
* A simple markdown parser that extracts frontmatter and content
*
* @param string - The markdown string to parse
* @returns
*/
export const parseMarkdown = async <T extends Record<string, unknown>>(
string: string
): Promise<{
frontmatter: Partial<T>;
content: string;
}> => {
const frontmatterRegex = /^---\n([\s\S]+?)\n---/;
const match = string.match(frontmatterRegex);
let frontmatter: Partial<T> = {};
let content = string;
if (match) {
const yaml = match[1];
frontmatter = Object.fromEntries(
yaml.split('\n').map((line) => {
const [key, ...rest] = line.split(':');
return [key.trim(), rest.join(':').trim()];
})
) as Partial<T>;
content = string.slice(match[0].length).trim();
}
return { frontmatter, content };
};
/** Load markdown files from a directory and parse them
*
* @param path - The path to the directory containing markdown files
* @param name - Optional specific file name to load (must include .md extension)
* @returns An array of parsed markdown files with frontmatter and content
*/
export const loadMarkdownOptions = async <T extends Record<string, unknown>>(
path: PathLike,
name?: string
): Promise<
Array<{
frontmatter: Partial<T>;
content: string;
}>
> => {
const files = await readdir(path);
const markdownFiles = files.filter(
(file) => file.endsWith('.md') && (name ? file === name : true)
);
const results: Array<{ frontmatter: Partial<T>; content: string }> = [];
for (const file of markdownFiles) {
const filePath = new URL(`${path}/${file}`, import.meta.url);
const fileContent = await readFile(filePath, 'utf-8');
const result = await parseMarkdown<T>(fileContent);
results.push(result);
}
return results;
};