ttcMD: fixes typedef file needing to be imported
This commit is contained in:
parent
1cc514d83a
commit
1b0d39f7a3
@ -4,7 +4,6 @@ import { createElements } from "@/lib/tcmd";
|
||||
import React, { FC, Suspense, useEffect, useMemo, useState } from "react";
|
||||
|
||||
import { MDSkeletonLoader } from "../loader";
|
||||
import { Token } from "@/types";
|
||||
|
||||
export const TTCMD: FC<
|
||||
{ body: string; escapeTOC?: (tokens: Token[]) => boolean }
|
||||
@ -80,6 +79,5 @@ export const TTCMDRenderer: FC<{ tokens: Token[] }> = (
|
||||
};
|
||||
|
||||
function renderer(tokens: Token[]) {
|
||||
const usedIds: string[] = [];
|
||||
return tokens.map((t) => <div className="p" key={t.uuid}>{t.render(t)}</div>);
|
||||
}
|
||||
|
@ -1,9 +1,3 @@
|
||||
import {
|
||||
IdentifiedToken,
|
||||
Token,
|
||||
TokenAttributes,
|
||||
TokenRenderer,
|
||||
} from "@/types";
|
||||
import { sanitize } from "isomorphic-dompurify";
|
||||
import Link from "next/link";
|
||||
import { Fragment } from "react";
|
||||
@ -114,6 +108,8 @@ export const buildOnlyDefaultElements = () => {
|
||||
);
|
||||
});
|
||||
|
||||
const usedIds: string[] = [];
|
||||
|
||||
const rendersContentOnly = true;
|
||||
const rendersChildrenOnly = true;
|
||||
|
||||
@ -292,12 +288,14 @@ export const buildOnlyDefaultElements = () => {
|
||||
|
||||
// heading
|
||||
registerIdentifier("heading", /^#+\s(.*?)$/gm, (s, rx) => {
|
||||
const content = s.match(new RegExp(rx, ""))?.at(1) ||
|
||||
"Unable to parse heading";
|
||||
return {
|
||||
content: s.match(new RegExp(rx, ""))?.at(1) ||
|
||||
"Unable to parse heading",
|
||||
content: content,
|
||||
raw: s,
|
||||
metadata: {
|
||||
strength: s.match(/#/g)?.length.toString() || "1",
|
||||
id: generateId(content, usedIds),
|
||||
},
|
||||
uuid: crypto.randomUUID(),
|
||||
rendersContentOnly,
|
||||
@ -698,3 +696,18 @@ function search(
|
||||
lastIndex: oldEnd === end ? end : start + s.match(openRx)![0].length,
|
||||
};
|
||||
}
|
||||
|
||||
// Finds a unique id for things like headings
|
||||
function generateId(t: string, usedIds: string[]) {
|
||||
let id = t.toLowerCase().replace(/[^a-z\s]/ig, "").trim().replaceAll(
|
||||
" ",
|
||||
"-",
|
||||
);
|
||||
let idNum = 1;
|
||||
while (usedIds.includes(id)) {
|
||||
id = id.replace(/-[0-9]+$/g, "");
|
||||
id += "-" + idNum;
|
||||
idNum++;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import { FrontMatter, Token, TokenMarker } from "@/types";
|
||||
import { zipArrays } from "../zip";
|
||||
import { buildOnlyDefaultElements, TokenRenderers } from "./TokenIdentifiers";
|
||||
|
||||
|
72
types.d.ts
vendored
72
types.d.ts
vendored
@ -1,46 +1,3 @@
|
||||
import { ReactNode } from "react";
|
||||
|
||||
type InlineToken = {
|
||||
type:
|
||||
| "text"
|
||||
| "bold"
|
||||
| "anchor"
|
||||
| "image"
|
||||
| "popover"
|
||||
| "italic"
|
||||
| "inline-code";
|
||||
content: string;
|
||||
data?: any;
|
||||
uuid: string;
|
||||
};
|
||||
|
||||
type InlineTokenInsert = {
|
||||
start: number;
|
||||
end: number;
|
||||
} & InlineToken;
|
||||
|
||||
type Line = string | InlineToken[];
|
||||
|
||||
type MultilineToken = {
|
||||
type: "code" | "p";
|
||||
lines: SingleLineToken[];
|
||||
};
|
||||
|
||||
type SingleLineCfg = {
|
||||
rx: RegExp;
|
||||
create: (line: string) => SingleLineToken;
|
||||
replaceRx: RegExp;
|
||||
shouldMendNextLine?: boolean;
|
||||
};
|
||||
|
||||
type SingleLineToken = {
|
||||
type: "h1" | "h2" | "h3" | "text" | `list${number}`;
|
||||
line: Line;
|
||||
raw: string;
|
||||
mends?: boolean;
|
||||
cfg?: SingleLineCfg;
|
||||
uuid: string;
|
||||
};
|
||||
type IdentifiedToken = {
|
||||
metadata: Record<string, string>;
|
||||
children?: Token[];
|
||||
@ -69,32 +26,3 @@ type TokenMarker = {
|
||||
};
|
||||
|
||||
type FrontMatter = Record<string, string>;
|
||||
|
||||
type MultilineCfg = {
|
||||
rx: RegExp;
|
||||
closeRx?: RegExp;
|
||||
create: (tokens: Token[]) => SingleLineToken[];
|
||||
replace: (line: string) => string;
|
||||
};
|
||||
|
||||
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
||||
|
||||
type BlockToken = {
|
||||
type: "block" | "grid" | "card" | "accordion";
|
||||
metadata: any;
|
||||
children: BlockChildren[];
|
||||
parent?: string;
|
||||
closed: boolean;
|
||||
uuid: string;
|
||||
};
|
||||
|
||||
type BlockChildren = ParagraphToken | BlockToken | SingleLineToken;
|
||||
|
||||
type ParagraphToken = {
|
||||
content: SingleLineToken[];
|
||||
allowsInline: boolean;
|
||||
type: "p" | "code";
|
||||
metadata: any;
|
||||
closed: boolean;
|
||||
uuid: string;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user