Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

parseEnrichedMarkdown.test-support.ts

34 lines1.1 KB

1import * as NodeModule from "node:module";
2import * as NodePath from "node:path";
3import * as NodeURL from "node:url";
4​
5export interface EnrichedMarkdownAstNode {
6 type: string;
7 content?: string;
8 attributes?: Record<string, string>;
9 children?: EnrichedMarkdownAstNode[];
11​
12const require = NodeModule.createRequire(import.meta.url);
13// Upstream v1.2+ restricts package exports to the public entry; tests resolve the web WASM parser from package root.
14const parserModuleUrl = NodeURL.pathToFileURL(
15 NodePath.join(
16 NodePath.dirname(require.resolve("react-native-enriched-markdown/package.json")),
17 "lib/module/web/parseMarkdown.js",
18 ),
19).href;
20​
21let parserModulePromise: Promise<{
22 parseMarkdown: (
23 markdown: string,
24 flags?: Record<string, boolean>,
25 ) => Promise<EnrichedMarkdownAstNode>;
26}> | null = null;
27​
28export async function parseEnrichedMarkdown(
29 markdown: string,
30 flags?: Record<string, boolean>,
31): Promise<EnrichedMarkdownAstNode> {
32 parserModulePromise ??= import(parserModuleUrl);
33 return (await parserModulePromise).parseMarkdown(markdown, flags);