5 changed files with 179 additions and 27 deletions
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/** heuristically checks whether the text may be a mermaid diagram definition */ |
||||
export const isMaybeMermaidDefinition = (text: string) => { |
||||
const chartTypes = [ |
||||
"flowchart", |
||||
"sequenceDiagram", |
||||
"classDiagram", |
||||
"stateDiagram", |
||||
"stateDiagram-v2", |
||||
"erDiagram", |
||||
"journey", |
||||
"gantt", |
||||
"pie", |
||||
"quadrantChart", |
||||
"requirementDiagram", |
||||
"gitGraph", |
||||
"C4Context", |
||||
"mindmap", |
||||
"timeline", |
||||
"zenuml", |
||||
"sankey", |
||||
"xychart", |
||||
"block", |
||||
]; |
||||
|
||||
const re = new RegExp( |
||||
`^(?:%%{.*?}%%[\\s\\n]*)?\\b${chartTypes |
||||
.map((x) => `${x}(-beta)?`) |
||||
.join("|")}\\b`,
|
||||
); |
||||
|
||||
return re.test(text.trim()); |
||||
}; |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
import { vi } from "vitest"; |
||||
import * as MermaidToExcalidraw from "@excalidraw/mermaid-to-excalidraw"; |
||||
import type { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw"; |
||||
import React from "react"; |
||||
|
||||
export const mockMermaidToExcalidraw = (opts: { |
||||
parseMermaidToExcalidraw: typeof parseMermaidToExcalidraw; |
||||
mockRef?: boolean; |
||||
}) => { |
||||
vi.mock("@excalidraw/mermaid-to-excalidraw", async (importActual) => { |
||||
const module = (await importActual()) as any; |
||||
|
||||
return { |
||||
__esModule: true, |
||||
...module, |
||||
}; |
||||
}); |
||||
const parseMermaidToExcalidrawSpy = vi.spyOn( |
||||
MermaidToExcalidraw, |
||||
"parseMermaidToExcalidraw", |
||||
); |
||||
|
||||
parseMermaidToExcalidrawSpy.mockImplementation(opts.parseMermaidToExcalidraw); |
||||
|
||||
if (opts.mockRef) { |
||||
vi.spyOn(React, "useRef").mockReturnValue({ |
||||
current: { |
||||
parseMermaidToExcalidraw: parseMermaidToExcalidrawSpy, |
||||
}, |
||||
}); |
||||
} |
||||
}; |
||||
Loading…
Reference in new issue