2 Commits

Author SHA1 Message Date
90f8c11288 0.1.5 2023-05-06 14:07:18 -07:00
4ceff93214 updates to frontmatter handling 2023-05-06 14:07:15 -07:00
3 changed files with 19 additions and 11 deletions

4
package-lock.json generated
View File

@@ -29,7 +29,7 @@
"@types/qrcode": "^1.5.0", "@types/qrcode": "^1.5.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
}, },
"version": "0.1.4" "version": "0.1.5"
}, },
"node_modules/@tootallnate/once": { "node_modules/@tootallnate/once": {
"version": "2.0.0", "version": "2.0.0",
@@ -3483,5 +3483,5 @@
} }
} }
}, },
"version": "0.1.4" "version": "0.1.5"
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@doc-utils/markdown2html", "name": "@doc-utils/markdown2html",
"version": "0.1.4", "version": "0.1.5",
"publishConfig": { "publishConfig": {
"registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/" "registry": "https://gitea.home.jbrumond.me/api/packages/doc-utils/npm/"
}, },

View File

@@ -1,18 +1,26 @@
import * as yaml from 'yaml'; import * as yaml from 'yaml';
export function process_frontmatter(document: string, parse = true) { export function process_frontmatter(raw_document: string, parse = true) {
if (! document.startsWith('---\n')) { if (! raw_document.startsWith('---\n')) {
return { return {
frontmatter: null, frontmatter: null,
markdown: document, document: raw_document,
}; };
} }
const endIndex = document.slice(3).indexOf('\n---\n') + 3; const end_index = raw_document.slice(3).indexOf('\n---\n') + 3;
const frontmatterYaml = document.slice(3, endIndex);
const markdown = document.slice(endIndex + 4);
const frontmatter = parse ? frontmatterYaml : yaml.parse(frontmatterYaml);
return { frontmatter, markdown }; if (end_index === 2) {
return {
frontmatter: null,
document: raw_document,
};
}
const formatter_yaml = raw_document.slice(3, end_index);
const document = raw_document.slice(end_index + 4);
const frontmatter = parse ? formatter_yaml : yaml.parse(formatter_yaml);
return { frontmatter, document };
} }