mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
feat: improve mermaid detection on paste (#8287)
This commit is contained in:
parent
62228e0bbb
commit
2427e622b0
2 changed files with 19 additions and 3 deletions
15
packages/excalidraw/mermaid.test.ts
Normal file
15
packages/excalidraw/mermaid.test.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { isMaybeMermaidDefinition } from "./mermaid";
|
||||||
|
|
||||||
|
describe("isMaybeMermaidDefinition", () => {
|
||||||
|
it("should return true for a valid mermaid definition", () => {
|
||||||
|
expect(isMaybeMermaidDefinition("flowchart")).toBe(true);
|
||||||
|
expect(isMaybeMermaidDefinition("flowchart LR")).toBe(true);
|
||||||
|
expect(isMaybeMermaidDefinition("flowchart LR\nola")).toBe(true);
|
||||||
|
expect(isMaybeMermaidDefinition("%%{}%%flowchart")).toBe(true);
|
||||||
|
expect(isMaybeMermaidDefinition("%%{}%% flowchart")).toBe(true);
|
||||||
|
|
||||||
|
expect(isMaybeMermaidDefinition("graphs")).toBe(false);
|
||||||
|
expect(isMaybeMermaidDefinition("this flowchart")).toBe(false);
|
||||||
|
expect(isMaybeMermaidDefinition("this\nflowchart")).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
|
@ -2,6 +2,7 @@
|
||||||
export const isMaybeMermaidDefinition = (text: string) => {
|
export const isMaybeMermaidDefinition = (text: string) => {
|
||||||
const chartTypes = [
|
const chartTypes = [
|
||||||
"flowchart",
|
"flowchart",
|
||||||
|
"graph",
|
||||||
"sequenceDiagram",
|
"sequenceDiagram",
|
||||||
"classDiagram",
|
"classDiagram",
|
||||||
"stateDiagram",
|
"stateDiagram",
|
||||||
|
@ -23,9 +24,9 @@ export const isMaybeMermaidDefinition = (text: string) => {
|
||||||
];
|
];
|
||||||
|
|
||||||
const re = new RegExp(
|
const re = new RegExp(
|
||||||
`^(?:%%{.*?}%%[\\s\\n]*)?\\b${chartTypes
|
`^(?:%%{.*?}%%[\\s\\n]*)?\\b(?:${chartTypes
|
||||||
.map((x) => `${x}(-beta)?`)
|
.map((x) => `\\s*${x}(-beta)?`)
|
||||||
.join("|")}\\b`,
|
.join("|")})\\b`,
|
||||||
);
|
);
|
||||||
|
|
||||||
return re.test(text.trim());
|
return re.test(text.trim());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue