-
Notifications
You must be signed in to change notification settings - Fork 31
/
MermaidRendererPlugin.ts
133 lines (118 loc) · 3.29 KB
/
MermaidRendererPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { filter, traverse } from "@atlaskit/adf-utils/traverse";
import { UploadedImageData } from "../Attachments";
import { JSONDocNode } from "@atlaskit/editor-json-transformer";
import { ADFProcessingPlugin, PublisherFunctions } from "./types";
import { ADFEntity } from "@atlaskit/adf-utils/types";
import SparkMD5 from "spark-md5";
export function getMermaidFileName(mermaidContent: string | undefined) {
const mermaidText = mermaidContent ?? "flowchart LR\nid1[Missing Chart]";
const pathMd5 = SparkMD5.hash(mermaidText);
const uploadFilename = `RenderedMermaidChart-${pathMd5}.png`;
return { uploadFilename, mermaidText };
}
export interface ChartData {
name: string;
data: string;
}
export interface MermaidRenderer {
captureMermaidCharts(charts: ChartData[]): Promise<Map<string, Buffer>>;
}
export class MermaidRendererPlugin
implements
ADFProcessingPlugin<
ChartData[],
Record<string, UploadedImageData | null>
>
{
constructor(private mermaidRenderer: MermaidRenderer) {}
extract(adf: JSONDocNode): ChartData[] {
const mermaidNodes = filter(
adf,
(node) =>
node.type == "codeBlock" &&
(node.attrs || {})?.["language"] === "mermaid"
);
const mermaidNodesToUpload = new Set(
mermaidNodes.map((node) => {
const mermaidDetails = getMermaidFileName(
node?.content?.at(0)?.text
);
return {
name: mermaidDetails.uploadFilename,
data: mermaidDetails.mermaidText,
} as ChartData;
})
);
return Array.from(mermaidNodesToUpload);
}
async transform(
mermaidNodesToUpload: ChartData[],
supportFunctions: PublisherFunctions
): Promise<Record<string, UploadedImageData | null>> {
let imageMap: Record<string, UploadedImageData | null> = {};
if (mermaidNodesToUpload.length === 0) {
return imageMap;
}
const mermaidChartsAsImages =
await this.mermaidRenderer.captureMermaidCharts([
...mermaidNodesToUpload,
]);
for (const mermaidImage of mermaidChartsAsImages) {
const uploadedContent = await supportFunctions.uploadBuffer(
mermaidImage[0],
mermaidImage[1]
);
imageMap = {
...imageMap,
[mermaidImage[0]]: uploadedContent,
};
}
return imageMap;
}
load(
adf: JSONDocNode,
imageMap: Record<string, UploadedImageData | null>
): JSONDocNode {
let afterAdf = adf as ADFEntity;
afterAdf =
traverse(afterAdf, {
codeBlock: (node, _parent) => {
if (node?.attrs?.["language"] === "mermaid") {
const mermaidContent = node?.content?.at(0)?.text;
if (!mermaidContent) {
return;
}
const mermaidFilename =
getMermaidFileName(mermaidContent);
if (!imageMap[mermaidFilename.uploadFilename]) {
return;
}
const mappedImage =
imageMap[mermaidFilename.uploadFilename];
if (mappedImage) {
node.type = "mediaSingle";
node.attrs["layout"] = "center";
if (node.content) {
node.content = [
{
type: "media",
attrs: {
type: "file",
collection: mappedImage.collection,
id: mappedImage.id,
width: mappedImage.width,
height: mappedImage.height,
},
},
];
}
delete node.attrs["language"];
return node;
}
}
return;
},
}) || afterAdf;
return afterAdf as JSONDocNode;
}
}