-
-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathmermaid.ts
125 lines (94 loc) · 2.9 KB
/
mermaid.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
import elkLayouts from '@mermaid-js/layout-elk';
import zenuml from '@mermaid-js/mermaid-zenuml';
import type { MermaidConfig, RenderResult } from 'mermaid';
import mermaid from 'mermaid';
mermaid.registerLayoutLoaders(elkLayouts);
const init = mermaid.registerExternalDiagrams([zenuml]);
//-----------------------------------------
class ExtensionData {
name: string;
url: string;
}
class alias {
parent: string;
}
class Aliases {
[x: string] : alias;
}
class Icon {
body: string;
width: number;
height: number;
}
class IconNameData {
[x: string] : Icon;
}
class Module {
lastModified: number;
height: number;
width: number;
prefix: string;
icons: IconNameData;
aliases: Aliases;
}
type WrapFunction = () => Module;
class MermaidRegisterObject {
name: string;
loader: WrapFunction;
}
async function loader_function(url: string): Promise<Module> {
const response = await fetch(url); // Tipo: Response
const module = await response.json() as Module; // El tipo de `json()` es `any`, pero lo asignamos a `Module`
// Asegurándonos de que el JSON tenga la forma correcta de `Module`
return module; // Aquí estamos afirmando que el JSON tiene la forma de `Module`
}
async function UrlsToRegisterObject(extension_value: ExtensionData): Promise<MermaidRegisterObject> {
const name = extension_value.name
const url = extension_value.url
const module = await loader_function(url);
function wrap(): Module {return module;}
return {
name: name,
loader: wrap,
}
} //Comment for commit, you can ignore this
function checkIfExtensionIsPresent(){
//Just to have true false value instead of null document
return document.querySelector('#extension-data') ? true : false;
}
// Tipar la función loadInputs correctamente
async function loadInputs(): Promise<MermaidRegisterObject[] | null> {
//waitSync(1000); //Just to try to see if it loads the data
if (!checkIfExtensionIsPresent()){return null;}
const dataElement = document.querySelector('#extension-data');
const extension_data = JSON.parse(dataElement.textContent) as ExtensionData[];
const data: MermaidRegisterObject[] = [];
for (const item of extension_data) {
const mermaidRegister = await UrlsToRegisterObject(item);
data.push(mermaidRegister);
}
//
return data;
}
// La función mermaidRegisterProcess
async function mermaidRegisterProcess() {
const inputs = await loadInputs();
if (inputs) {
mermaid.registerIconPacks(inputs);
}
}
//-------
export const render = async (
config: MermaidConfig,
code: string,
id: string
): Promise<RenderResult> => {
await init;
// Should be able to call this multiple times without any issues.
mermaid.initialize(config);
await mermaidRegisterProcess();
return await mermaid.render(id, code);
};
export const parse = async (code: string): Promise<unknown> => {
return await mermaid.parse(code);
};