Compare commits

..

2 Commits

Author SHA1 Message Date
b7df327cd5 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
2026-07-31 01:42:39 -04:00
aa652bd7e0 Added helpers
All checks were successful
Build / Publish Docs (push) Successful in 1m47s
Build / Build NPM Project (push) Successful in 35s
Build / Tag Version (push) Successful in 8s
2026-07-29 17:51:24 -04:00
2 changed files with 26 additions and 6 deletions

View File

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

View File

@@ -26,6 +26,9 @@ export function decodeHtml(html: string) {
/** /**
* Parse markdown headers * Parse markdown headers
*
* **NOTE: frontmatter parsing only works 2 layers deep**
*
* @param {string} content * @param {string} content
* @returns {{meta: any, content: string} | {meta: {}, content: string}} * @returns {{meta: any, content: string} | {meta: {}, content: string}}
*/ */
@@ -34,12 +37,29 @@ export function parseMarkdown(content: string) {
if (!match) return {meta: {}, content}; if (!match) return {meta: {}, content};
const meta: any = {}; 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(':'); const colonIdx = line.indexOf(':');
if (colonIdx === -1) continue; 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(); 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()}; return {meta, content: match[2].trim()};
} }