1.4 KB59 lines
Blame
1import type { MermaidConfig } from 'mermaid';
2
3const warning = (s: string) => {
4 // Todo remove debug code
5 // eslint-disable-next-line no-console
6 console.error('Log function was called before initialization', s);
7};
8
9export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
10
11export const LEVELS: Record<LogLevel, number> = {
12 trace: 0,
13 debug: 1,
14 info: 2,
15 warn: 3,
16 error: 4,
17 fatal: 5,
18};
19
20export const log: Record<keyof typeof LEVELS, typeof console.log> = {
21 trace: warning,
22 debug: warning,
23 info: warning,
24 warn: warning,
25 error: warning,
26 fatal: warning,
27};
28
29export let setLogLevel: (level: keyof typeof LEVELS | number) => void;
30export let getConfig: () => MermaidConfig;
31export let sanitizeText: (str: string) => string;
32// eslint-disable @typescript-eslint/no-explicit-any
33export let setupGraphViewbox: (
34 graph: any,
35 svgElem: any,
36 padding: any,
37 useMaxWidth: boolean
38) => void;
39
40export const injectUtils = (
41 _log: Record<keyof typeof LEVELS, typeof console.log>,
42 _setLogLevel: any,
43 _getConfig: any,
44 _sanitizeText: any,
45 _setupGraphViewbox: any
46) => {
47 _log.info('Mermaid utils injected');
48 log.trace = _log.trace;
49 log.debug = _log.debug;
50 log.info = _log.info;
51 log.warn = _log.warn;
52 log.error = _log.error;
53 log.fatal = _log.fatal;
54 setLogLevel = _setLogLevel;
55 getConfig = _getConfig;
56 sanitizeText = _sanitizeText;
57 setupGraphViewbox = _setupGraphViewbox;
58};
59