From 3c7ef5a1850319aadbfb77a0e3a8771c9f428424 Mon Sep 17 00:00:00 2001 From: Emma Date: Fri, 15 Mar 2024 10:02:46 -0600 Subject: [PATCH] ttcMD: allow for typing metadata --- lib/tcmd/TokenIdentifiers.tsx | 46 +++++++++++++++++------------------ types.d.ts | 12 ++++----- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/tcmd/TokenIdentifiers.tsx b/lib/tcmd/TokenIdentifiers.tsx index c2c5a37..9cecf42 100644 --- a/lib/tcmd/TokenIdentifiers.tsx +++ b/lib/tcmd/TokenIdentifiers.tsx @@ -11,27 +11,27 @@ type SearchFunction = (s: string, start: number, end: number) => { lastIndex: number; }; -type TokenIdentifier = { +type TokenIdentifier = { rx: RegExp; - parse: (s: string) => Token; + parse: (s: string) => Token; search?: SearchFunction; }; type TokenIdentifierMap = Map< string, - TokenIdentifier + TokenIdentifier >; export const TokenRenderers = new Map< string, - TokenRenderer + TokenRenderer >(); -type IdentifierRegistration = ( +type IdentifierRegistration = >( type: string, match: RegExp, - parseFunction: (s: string, rx: RegExp) => IdentifiedToken, - renderFunction: TokenRenderer, + parseFunction: (s: string, rx: RegExp) => IdentifiedToken, + renderFunction: TokenRenderer, openTagRx?: RegExp, closeTagRx?: RegExp, ) => void; @@ -42,28 +42,28 @@ export function buildIdentifierMap(): [ ] { const TokenIdentifiers = new Map< string, - TokenIdentifier + TokenIdentifier >(); - function registerIdentifier( + function registerIdentifier( type: string, match: RegExp, - parseFunction: (s: string, rx: RegExp) => IdentifiedToken, - renderFunction: TokenRenderer, + parseFunction: (s: string, rx: RegExp) => IdentifiedToken, + renderFunction: TokenRenderer, ): void; - function registerIdentifier( + function registerIdentifier( type: string, match: RegExp, - parseFunction: (s: string, rx: RegExp) => IdentifiedToken, - renderFunction: TokenRenderer, + parseFunction: (s: string, rx: RegExp) => IdentifiedToken, + renderFunction: TokenRenderer, openTagRx: RegExp, closeTagRx: RegExp, ): void; - function registerIdentifier( + function registerIdentifier>( type: string, match: RegExp, - parseFunction: (s: string, rx: RegExp) => IdentifiedToken, - renderFunction: TokenRenderer, + parseFunction: (s: string, rx: RegExp) => IdentifiedToken, + renderFunction: TokenRenderer, openTagRx?: RegExp, closeTagRx?: RegExp, ) { @@ -76,7 +76,7 @@ export function buildIdentifierMap(): [ type, }; - return { ...token, ...identifiedToken }; + return { ...token, ...identifiedToken } as Token; }, search: (openTagRx && closeTagRx) ? (s, start, end) => { @@ -99,7 +99,7 @@ export function buildIdentifierMap(): [ export const buildOnlyDefaultElements = () => { const [TokenIdentifiers, registerIdentifier] = buildIdentifierMap(); - TokenRenderers.set("text", (t) => { + TokenRenderers.set("text", (t: Token) => { debugger; return ( @@ -114,7 +114,7 @@ export const buildOnlyDefaultElements = () => { const rendersChildrenOnly = true; // grid - registerIdentifier( + registerIdentifier<{ columns: string }>( "grid", /(? { @@ -140,7 +140,7 @@ export const buildOnlyDefaultElements = () => { } as React.CSSProperties} className="grid grid-cols-dynamic gap-x-8 gap-y-6 mb-6" > - {children?.map((c, i) => ( + {children?.map((c) => (
{c.render(c)}
@@ -240,7 +240,7 @@ export const buildOnlyDefaultElements = () => { key={c.uuid} data-depth={metadata.initialDepth} > - {c.children?.map((c) => ( + {c.children?.map((c: Token) => ( {c.render(c)} ))} @@ -463,7 +463,7 @@ export const buildOnlyDefaultElements = () => { content={children?.map((c) => ( {c.render(c)} )) || - metadata.content} + token.content} preferredAlign="centered" preferredEdge="bottom" className="cursor-pointer mx-2" diff --git a/types.d.ts b/types.d.ts index 9e1c21c..4a7c9fe 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,5 +1,5 @@ -type IdentifiedToken = { - metadata: Record; +type IdentifiedToken = { + metadata: M; children?: Token[]; uuid: string; raw: string; @@ -8,21 +8,21 @@ type IdentifiedToken = { rendersContentOnly?: boolean; }; -type TokenRenderer = (t: Token) => ReactNode; +type TokenRenderer = (t: Token) => ReactNode; type TokenAttributes = { type: string; render: TokenRenderer; }; -type Token = IdentifiedToken & TokenAttributes; +type Token> = IdentifiedToken & TokenAttributes; -type TokenMarker = { +type TokenMarker> = { start: number; end: number; type: string; parent?: TokenMarker; - token: Token; + token: Token; }; type FrontMatter = Record;