forked from preactjs/preact-www
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontmatter.js
More file actions
54 lines (47 loc) · 1.55 KB
/
frontmatter.js
File metadata and controls
54 lines (47 loc) · 1.55 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
import yaml from 'yaml';
/**
* Parse Markdown with YAML FrontMatter
* @param {string} content - Raw markdown content with frontmatter
* @param {string} path - File path for error reporting
* @returns {{title: string, description: string, body: string, meta: object}}
*/
export function parseFrontmatter(content, path = '') {
// Accept both LF and CRLF line endings so frontmatter is detected on Windows
const FRONT_MATTER_REG = /^\s*---\r?\n\s*([\s\S]*?)\s*\r?\n---\r?\n/i;
const matches = content.match(FRONT_MATTER_REG);
if (!matches) {
throw new Error(`Missing YAML FrontMatter in ${path}`);
}
let meta = {};
try {
meta = yaml.parse('---\n' + matches[1].replace(/^/gm, ' ') + '\n');
if (!meta.title) {
throw new Error(`Missing title in YAML FrontMatter for ${path}`);
}
} catch (e) {
e.message = `Error parsing YAML FrontMatter in ${path}:\n\n ${e.message}`;
throw e;
}
const body = content.replace(FRONT_MATTER_REG, '').trim();
return {
title: meta.title || '',
description: meta.description || '',
body,
meta
};
}
/**
* Process and clean code blocks for LLM consumption
* Removes --repl comments and markers that are used for REPL functionality
* @param {string} content - Markdown content
* @returns {string} - Cleaned content
*/
export function cleanReplComments(content) {
// Remove --repl comment lines and markers
return content
.replace(/^\/\/ --repl.*$/gm, '')
.replace(/^\/\/ --repl-before.*$/gm, '')
.replace(/^\/\/ --repl-after.*$/gm, '')
.replace(/\n\n\n+/g, '\n\n') // Clean up extra newlines
.trim();
}