ttcMD: adds frontmatter extraction and token,

fixes inline-code... again
This commit is contained in:
2024-03-14 09:26:15 -06:00
parent 606a90b050
commit 0d839fbf37
5 changed files with 96 additions and 10 deletions

View File

@@ -1,3 +1,5 @@
import { parse } from "path";
type TokenIdentifier = {
rx: RegExp;
parse: (s: string) => Token;
@@ -63,8 +65,13 @@ TokenIdentifiers.set("card", {
return search(s, start, end, rx, crx);
},
parse(s) {
const rx = /\[{2}(!?)\n+([\s\S]*)\n+\]{2}/;
const [_, isBlock, content] = s.match(rx) || ["", "Unable to parse card"];
const rx = /\[{2}(!?)\s*?\n+([\s\S]*)\n+\]{2}/;
const match = s.match(rx);
if (!match) debugger;
const [_, isBlock, content] = match ||
["", "", s];
// if (!_) debugger;
return {
content: content.trim(),
@@ -74,6 +81,7 @@ TokenIdentifiers.set("card", {
},
type: "card",
uuid: crypto.randomUUID(),
rendersChildrenOnly,
};
},
});
@@ -187,7 +195,7 @@ TokenIdentifiers.set("anchor", {
},
});
TokenIdentifiers.set("inline-code", {
rx: /\s?`(.{3,}?|[a-z0-9]*?)`[^`a-z0-9\n]/gi,
rx: /(?<=\s|^)`(.*?)`(?=[\s,.!?)]|$)/gi,
parse(s) {
return {
// content: inline,
@@ -307,6 +315,21 @@ TokenIdentifiers.set("comment", {
},
});
TokenIdentifiers.set("frontmatter", {
rx: /^---([\s\S]*?)---/g,
parse(s) {
return {
content: "",
metadata: {
frontmatterString: s.match(this.rx)?.at(0) || "",
},
raw: "",
type: "frontmatter",
uuid: "frontmatter",
};
},
});
function findMatchingClosedParenthesis(
str: string,
openRegex: RegExp,

View File

@@ -155,9 +155,20 @@ const contentToChildren = (token: Token) => {
content: c.replaceAll("\n", ""),
metadata: {},
raw: c,
type: "text",
type: token.rendersChildrenOnly ? "p" : "text",
uuid: crypto.randomUUID(),
rendersContentOnly: true,
children: token.rendersChildrenOnly && c.replaceAll("\n", "")
? [
{
content: c.replaceAll("\n", ""),
metadata: {},
raw: c,
type: "text",
uuid: crypto.randomUUID(),
},
]
: undefined,
})),
token.children || [],
).filter((c) => c.children?.length || (c.rendersContentOnly && c.content));
@@ -239,3 +250,26 @@ function processChunks(chunks: Token[][]) {
// Filter out chunks that were merged into others
return chunks.filter((c) => !mergedChunks.find((c2) => c === c2));
}
/**
* @description Extracts the frontmatter of a markdown document and returns it as an object and with the body with the frontmatter removed from it
*
* @returns a tuple containing the body and the frontmatter object
*/
export function extractFrontMatter(body: string): [FrontMatter, string] {
const rx = /^---([\s\S]*?)---/;
const [_, frontmatterString] = body.match(rx) || ["", ""];
body = body.replace(rx, "");
const frontMatter: FrontMatter = {};
for (const line of frontmatterString.split("\n")) {
if (!line) continue;
const [key, value] = line.split(": ");
frontMatter[key] = value;
}
return [frontMatter, body];
}