ttcMD: fixes typedef file needing to be imported

This commit is contained in:
2024-03-15 08:40:55 -06:00
parent 1cc514d83a
commit 1b0d39f7a3
4 changed files with 21 additions and 83 deletions

View File

@@ -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;
}

View File

@@ -1,6 +1,5 @@
"use client";
import { FrontMatter, Token, TokenMarker } from "@/types";
import { zipArrays } from "../zip";
import { buildOnlyDefaultElements, TokenRenderers } from "./TokenIdentifiers";