Improved markdown parser
All checks were successful
Build / Publish Docs (push) Successful in 1m35s
Build / Build NPM Project (push) Successful in 1m45s
Build / Tag Version (push) Successful in 10s

This commit is contained in:
2026-07-31 01:42:39 -04:00
parent aa652bd7e0
commit b7df327cd5
2 changed files with 25 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.30.3",
"version": "0.30.4",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@@ -26,20 +26,40 @@ export function decodeHtml(html: string) {
/**
* Parse markdown headers
*
* **NOTE: frontmatter parsing only works 2 layers deep**
*
* @param {string} content
* @returns {{meta: any, content: string} | {meta: {}, content: string}}
*/
export function parseMarkdown(content: string) {
const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
if(!match) return {meta: {}, content};
if (!match) return {meta: {}, content};
const meta: any = {};
for (const line of match[1].split('\n')) {
let currentParent: string | null = null;
for (const rawLine of match[1].split('\n')) {
if (!rawLine.trim()) continue;
const indented = /^\s+/.test(rawLine);
const line = rawLine.trim();
const colonIdx = line.indexOf(':');
if (colonIdx === -1) continue;
const key = line.slice(0, colonIdx).trim();
const key = line.slice(0, colonIdx).trim();
const value = line.slice(colonIdx + 1).trim();
try { meta[key] = JSON.parse(value); } catch { meta[key] = value; }
let parsed: any = value;
try { parsed = JSON.parse(value); } catch {}
if (!indented) {
currentParent = value === '' ? key : null;
if (value === '') meta[key] = {};
else meta[key] = parsed;
} else if (currentParent) {
meta[currentParent][key] = parsed;
}
}
return {meta, content: match[2].trim()};
}