diff --git a/components/ttcmd/index.tsx b/components/ttcmd/index.tsx
index 252101e..4e97bfe 100644
--- a/components/ttcmd/index.tsx
+++ b/components/ttcmd/index.tsx
@@ -16,6 +16,7 @@ import React, {
import { sanitize } from "isomorphic-dompurify";
import { MDSkeletonLoader } from "../loader";
+import { Token } from "@/types";
export const TTCMD: FC<
{ body: string; escapeTOC?: (tokens: Token[]) => boolean }
@@ -64,7 +65,9 @@ export const TTCMD: FC<
);
};
-export const TTCMDRenderer: FC<{ tokens: Token[] }> = ({ tokens }) => {
+export const TTCMDRenderer: FC<{ tokens: Token[] }> = (
+ { tokens },
+) => {
const tada = useMemo(
() => (
<>
@@ -90,9 +93,7 @@ export const TTCMDRenderer: FC<{ tokens: Token[] }> = ({ tokens }) => {
function renderer(tokens: Token[]) {
const usedIds: string[] = [];
- return tokens.map((t) => (
-
{render(t, usedIds)}
- ));
+ return tokens.map((t) => {t.render(t)}
);
}
function render(token: Token, usedIds: string[]) {
@@ -175,7 +176,7 @@ function render(token: Token, usedIds: string[]) {
}
case "inline-code":
return (
-
+
{token.content}
);
diff --git a/lib/tcmd/TokenIdentifiers.ts b/lib/tcmd/TokenIdentifiers.ts
deleted file mode 100644
index c4b025a..0000000
--- a/lib/tcmd/TokenIdentifiers.ts
+++ /dev/null
@@ -1,411 +0,0 @@
-import { parse } from "path";
-
-type TokenIdentifier = {
- rx: RegExp;
- parse: (s: string) => Token;
- search?: (s: string, start: number, end: number) => {
- start: number;
- end: number;
- text: string;
- lastIndex: number;
- };
-};
-
-export const TokenIdentifiers = new Map<
- string,
- TokenIdentifier
->();
-
-// TokenIdentifiers.set("p", {
-// rx: /\n{2,}((?:.|\n)*?)\n{2,}/g,
-// parse(s) {
-// const [_, content] = s.match(new RegExp(this.rx, ""))!;
-
-// return {
-// // content,
-// content,
-// raw: s,
-// metadata: {},
-// type: "p",
-// uuid: crypto.randomUUID(),
-// };
-// },
-// });
-const rendersContentOnly = true;
-const rendersChildrenOnly = true;
-TokenIdentifiers.set("grid", {
- search(s, start, end) {
- const rx = /(?>/g,
- parse(s) {
- const [_, title, content] = s.match(new RegExp(this.rx, ""))!;
-
- return {
- // content,
- content,
- raw: s,
- metadata: { title },
- type: "popover",
- uuid: crypto.randomUUID(),
- rendersContentOnly,
- };
- },
-});
-TokenIdentifiers.set("accordion", {
- rx: /\[accordion(\s.*?)?]\n+((?:.|\n)*?)\n+\[\/accordion\]/g,
- parse(s) {
- const [_, title, content] = s.match(new RegExp(this.rx, ""))!;
-
- return {
- // content,
- content,
- raw: s,
- metadata: { title },
- type: "accordion",
- uuid: crypto.randomUUID(),
- };
- },
-});
-TokenIdentifiers.set("p", {
- // rx: /(?<=\n)\n?([\s\S]*?)\n(?=\n)/g,
- rx: /(?<=\n\n)([\s\S]*?)(?=\n\n)/g,
- parse(s) {
- // const [_, content] = s.match(new RegExp(this.rx, ""))!;
-
- return {
- // content,
- content: s,
- raw: s,
- metadata: {},
- type: "p",
- uuid: crypto.randomUUID(),
- };
- },
-});
-
-TokenIdentifiers.set("hr", {
- rx: /^-{3,}$/gm,
- parse(s) {
- return {
- content: s,
- raw: s,
- metadata: {},
- type: "hr",
- uuid: crypto.randomUUID(),
- rendersContentOnly,
- };
- },
-});
-
-TokenIdentifiers.set("comment", {
- rx: //g,
- parse(s) {
- return {
- content: "",
- metadata: { comment: s },
- raw: "",
- type: "comment",
- uuid: crypto.randomUUID(),
- rendersContentOnly,
- };
- },
-});
-
-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,
- closedRegex: RegExp,
-): number | null {
- let openings = 0;
- let closings = 0;
-
- openRegex = new RegExp(openRegex, "g");
- closedRegex = new RegExp(closedRegex, "g");
-
- let lastOpeningSuccessIndex = 0;
- let lastClosingSuccessIndex = 0;
-
- do {
- const openingMatch = openRegex.exec(str);
- const closingMatch = closedRegex.exec(str);
-
- if ((openingMatch && !closingMatch)) {
- throw Error("Things have gone horribly wrong");
- }
-
- // if ((!openingMatch && closingMatch) || (!openingMatch && !closingMatch)) break;
-
- if (
- openingMatch && closingMatch && openingMatch.index < closingMatch.index
- ) {
- openings++;
- lastOpeningSuccessIndex = openingMatch.index + openingMatch[0].length;
- closedRegex.lastIndex = lastClosingSuccessIndex;
- } else if (
- (!openingMatch && closingMatch) ||
- (openingMatch && closingMatch && openingMatch.index > closingMatch.index)
- ) {
- closings++;
- lastClosingSuccessIndex = closingMatch.index + closingMatch[0].length;
- openRegex.lastIndex = lastOpeningSuccessIndex;
- } else {
- return closingMatch?.index ?? null;
- }
- } while (openings > closings);
-
- return closedRegex.lastIndex;
-}
-
-interface SearchResult {
- start: number;
- end: number;
- text: string;
- lastIndex: number;
-}
-
-function search(
- s: string,
- start: number,
- end: number,
- openRx: RegExp,
- closeRx: RegExp,
-): SearchResult {
- const oldEnd = end;
-
- const newEnd = findMatchingClosedParenthesis(
- s,
- // s.substring(0, end - start),
- openRx,
- closeRx,
- );
-
- if (newEnd === null) throw Error("There was an issue finding a closing tag");
-
- end = newEnd + start;
-
- return {
- start,
- end,
- text: s.substring(0, newEnd),
- lastIndex: oldEnd === end ? end : start + s.match(openRx)![0].length,
- };
-}
diff --git a/lib/tcmd/TokenIdentifiers.tsx b/lib/tcmd/TokenIdentifiers.tsx
index 339b325..6e82568 100644
--- a/lib/tcmd/TokenIdentifiers.tsx
+++ b/lib/tcmd/TokenIdentifiers.tsx
@@ -4,6 +4,11 @@ import {
TokenAttributes,
TokenRenderer,
} from "@/types";
+import { sanitize } from "isomorphic-dompurify";
+import Link from "next/link";
+import { Fragment } from "react";
+import { Poppable } from "../poppables/components/poppable";
+import { Accordion, AccordionContent } from "../accordion";
type SearchFunction = (s: string, start: number, end: number) => {
start: number;
@@ -23,9 +28,9 @@ type TokenIdentifierMap = Map<
TokenIdentifier
>;
-export const TokenIdentifiers = new Map<
+export const TokenRenderers = new Map<
string,
- TokenIdentifier
+ TokenRenderer
>();
type IdentifierRegistration = (
@@ -91,6 +96,7 @@ export function buildIdentifierMap(): [
}
: undefined,
});
+ TokenRenderers.set(type, renderFunction);
}
return [TokenIdentifiers, registerIdentifier];
@@ -99,6 +105,15 @@ export function buildIdentifierMap(): [
export const buildOnlyDefaultElements = () => {
const [TokenIdentifiers, registerIdentifier] = buildIdentifierMap();
+ TokenRenderers.set("text", (t) => {
+ debugger;
+ return (
+
+ {t.content.replaceAll("\\n", "\n")}
+
+ );
+ });
+
const rendersContentOnly = true;
const rendersChildrenOnly = true;
@@ -120,8 +135,22 @@ export const buildOnlyDefaultElements = () => {
rendersChildrenOnly,
};
},
- (t) => {
- return <>{t.raw}>;
+ (token) => {
+ const { content, children, metadata, uuid } = token;
+ return (
+
+ {children?.map((c, i) => (
+
+ {c.render(c)}
+
+ ))}
+
+ );
},
/(? {
(s) => {
const rx = /\[{2}(!?)\s*?\n+([\s\S]*)\n+\]{2}/;
const match = s.match(rx);
- if (!match) debugger;
- const [_, isBlock, content] = match ||
- ["", "", s];
+ const [_, isBlock, content] = match || ["", "", s];
return {
content: content.trim(),
@@ -148,8 +175,20 @@ export const buildOnlyDefaultElements = () => {
rendersChildrenOnly,
};
},
- (t) => {
- return <>{t.raw}>;
+ (token) => {
+ const { children, metadata, uuid } = token;
+ return (
+
+ {children?.map((e) => (
+
+ {e.render(e)}
+
+ ))}
+
+ );
},
/\[\[/g,
/\]\]/g,
@@ -165,8 +204,12 @@ export const buildOnlyDefaultElements = () => {
uuid: crypto.randomUUID(),
rendersContentOnly,
};
- }, (t) => {
- return <>{t.raw}>;
+ }, (token) => {
+ return (
+
+ {token.content}
+
+ );
});
// list
@@ -187,8 +230,29 @@ export const buildOnlyDefaultElements = () => {
rendersChildrenOnly,
};
},
- (t) => {
- return <>{t.raw}>;
+ (token) => {
+ const { children, metadata, uuid } = token;
+ return (
+ <>
+
+ {children?.map((c) => {
+ return (
+ -
+ {c.children?.map((c) => (
+ {c.render(c)}
+ ))}
+
+ );
+ })}
+
+ >
+ );
},
);
@@ -209,8 +273,20 @@ export const buildOnlyDefaultElements = () => {
uuid: crypto.randomUUID(),
};
},
- (t) => {
- return <>{t.raw}>;
+ (token) => {
+ const { children, metadata, uuid } = token;
+ return (
+
+ {children?.map((c) => (
+
+ (c.render(c))
+
+ ))}
+
+ );
},
);
@@ -226,8 +302,21 @@ export const buildOnlyDefaultElements = () => {
uuid: crypto.randomUUID(),
rendersContentOnly,
};
- }, (t) => {
- return <>{t.raw}>;
+ }, (token) => {
+ return (
+
+ {token.content}
+
+ );
});
// image
@@ -244,8 +333,23 @@ export const buildOnlyDefaultElements = () => {
uuid: crypto.randomUUID(),
rendersContentOnly,
};
- }, (t) => {
- return <>{t.raw}>;
+ }, (token) => {
+ const { metadata } = token;
+ metadata.src = metadata.src as string;
+ if (metadata.src.startsWith("