@graph-artifact/core 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,11 +12,34 @@ const invisibleHandle = {
|
|
|
12
12
|
background: 'transparent',
|
|
13
13
|
pointerEvents: 'none',
|
|
14
14
|
};
|
|
15
|
+
function decodeHtmlEntities(src) {
|
|
16
|
+
// Keep this intentionally small; labels should be plain text.
|
|
17
|
+
return src
|
|
18
|
+
.replace(/ /gi, ' ')
|
|
19
|
+
.replace(/&/gi, '&')
|
|
20
|
+
.replace(/</gi, '<')
|
|
21
|
+
.replace(/>/gi, '>')
|
|
22
|
+
.replace(/"/gi, '"')
|
|
23
|
+
.replace(/'/g, "'");
|
|
24
|
+
}
|
|
25
|
+
function htmlSubsetToMarkdown(src) {
|
|
26
|
+
// Mermaid labels often contain a tiny HTML subset: <br/>, <b>, <i>, etc.
|
|
27
|
+
// Convert the common cases to markdown so react-markdown renders them,
|
|
28
|
+
// and strip any remaining tags (avoid raw HTML rendering).
|
|
29
|
+
let s = src;
|
|
30
|
+
s = s.replace(/<\s*br\s*\/?\s*>/gi, '\n');
|
|
31
|
+
s = s.replace(/<\s*\/?\s*(strong|b)\s*>/gi, '**');
|
|
32
|
+
s = s.replace(/<\s*\/?\s*(em|i)\s*>/gi, '*');
|
|
33
|
+
s = s.replace(/<\s*\/?\s*code\s*>/gi, '`');
|
|
34
|
+
// Strip any remaining tags defensively
|
|
35
|
+
s = s.replace(/<[^>]*>/g, '');
|
|
36
|
+
return decodeHtmlEntities(s);
|
|
37
|
+
}
|
|
15
38
|
function normalizeLabelMarkdown(label) {
|
|
16
39
|
// Mermaid treats "\n" inside labels as a hard line break.
|
|
17
40
|
// react-markdown doesn't always preserve raw newlines, so force
|
|
18
41
|
// CommonMark hard-breaks by adding two trailing spaces.
|
|
19
|
-
return label
|
|
42
|
+
return htmlSubsetToMarkdown(label)
|
|
20
43
|
.replace(/\\n/g, '\n')
|
|
21
44
|
.trim()
|
|
22
45
|
.replace(/\n/g, ' \n');
|
|
@@ -35,14 +35,39 @@ function stripMarkdown(input) {
|
|
|
35
35
|
.replace(/[_~#>|-]/g, '')
|
|
36
36
|
.trim();
|
|
37
37
|
}
|
|
38
|
+
function decodeHtmlEntities(src) {
|
|
39
|
+
return src
|
|
40
|
+
.replace(/ /gi, ' ')
|
|
41
|
+
.replace(/&/gi, '&')
|
|
42
|
+
.replace(/</gi, '<')
|
|
43
|
+
.replace(/>/gi, '>')
|
|
44
|
+
.replace(/"/gi, '"')
|
|
45
|
+
.replace(/'/g, "'");
|
|
46
|
+
}
|
|
47
|
+
function normalizeLabelText(label) {
|
|
48
|
+
// Sizing must mirror render-time transformations. In practice Mermaid labels
|
|
49
|
+
// often contain a small HTML subset (<br/>, <b>, <i>) which we render as
|
|
50
|
+
// markdown in the node components. Convert the key cases here so line/width
|
|
51
|
+
// estimation is correct.
|
|
52
|
+
let s = label;
|
|
53
|
+
s = s.replace(/<\s*br\s*\/?\s*>/gi, '\n');
|
|
54
|
+
// Strip common formatting tags (they don't affect width materially)
|
|
55
|
+
s = s.replace(/<\s*\/?\s*(strong|b|em|i|code)\s*>/gi, '');
|
|
56
|
+
// Strip any remaining tags defensively
|
|
57
|
+
s = s.replace(/<[^>]*>/g, '');
|
|
58
|
+
s = decodeHtmlEntities(s);
|
|
59
|
+
// Mermaid uses escaped "\n" inside labels
|
|
60
|
+
s = s.replace(/\\n/g, '\n');
|
|
61
|
+
return s;
|
|
62
|
+
}
|
|
38
63
|
function labelMetrics(label) {
|
|
39
|
-
const normalized = label
|
|
64
|
+
const normalized = normalizeLabelText(label);
|
|
40
65
|
const lines = normalized.split('\n');
|
|
41
66
|
const maxChars = Math.max(1, ...lines.map((line) => stripMarkdown(line).length || 1));
|
|
42
67
|
return { maxChars, lines: Math.max(1, lines.length) };
|
|
43
68
|
}
|
|
44
69
|
function estimatedWrappedLineCount(label, contentCharsPerLine) {
|
|
45
|
-
const normalized = label
|
|
70
|
+
const normalized = normalizeLabelText(label);
|
|
46
71
|
const lines = normalized.split('\n');
|
|
47
72
|
let total = 0;
|
|
48
73
|
const charsPerLine = Math.max(8, contentCharsPerLine);
|