Skip to content

Commit 150e901

Browse files
committed
mermaid
1 parent a635e14 commit 150e901

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

docs/domain-examples.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,78 @@ The following modeling examples illustrate how research data can be represented
1111
## Musicology
1212
![Musicology Example](assets/musicology-example.jpg)
1313

14+
```mermaid
15+
16+
stateDiagram
17+
18+
direction BT
19+
20+
#style definitions
21+
classDef clazz fill:lightgrey,color:white
22+
classDef individual font-size:small
23+
24+
#styling of classes
25+
26+
class bfo_occurrent clazz
27+
class bfo_entity clazz
28+
class bfo_continuant clazz
29+
30+
#relations
31+
32+
bfo_occurrent --> bfo_entity: subClassOf
33+
# bfo_continuant --> bfo_entity: subClassOf
34+
# bfo_process --> bfo_occurrent: subClassOf
35+
# bfo_temporal_region --> bfo_occurrent: subClassOf
36+
# bfo_one_dimensional --> bfo_temporal_region: subClassOf
37+
# bfo_two_dimensional --> bfo_temporal_region: subClassOf
38+
39+
```
40+
41+
```mermaid
42+
stateDiagram
43+
direction BT
44+
classDef clazz fill:lightgrey,color:white;
45+
46+
state "cto:CTO_0001005 (source item)" as CTO_SourceItem
47+
class CTO_SourceItem clazz
48+
state "nfdicore:NFDI_0000003 (organization)" as NFDI_organization
49+
class NFDI_organization clazz
50+
state "schema:DataFeed" as SCHEMA_DataFeed
51+
class SCHEMA_DataFeed clazz
52+
state "cto:CTO_0001024 (incipit)" as CTO_Incipit
53+
class CTO_Incipit clazz
54+
state "RISM Online" as org_rism
55+
state "E5313" as feed_e5313
56+
state "RISM Resource 201001959" as si_201001959
57+
state "201001959 incipit 1.1.1" as inc_1_1_1
58+
59+
state "A''C/'B''D'BA/2G4BA/G-B''D/G4-2-/" as PATTERN_CLASS
60+
class PATTERN_CLASS
61+
state "C/" as TIMESIG
62+
class TIMESIG
63+
64+
65+
org_rism --> NFDI_organization:a
66+
si_201001959 --> CTO_SourceItem:a
67+
inc_1_1_1 --> CTO_Incipit:a
68+
69+
70+
feed_e5313 --> SCHEMA_DataFeed:a
71+
si_201001959 --> inc_1_1_1:CTO_has incipit
72+
si_201001959 --> org_rism: NFDI_publishedBy
73+
si_201001959 --> feed_e5313:CTO_isReferencedIn
74+
inc_1_1_1 --> PATTERN_CLASS:CTO_hasPattern
75+
inc_1_1_1 --> key_G:CTO_hasKey
76+
inc_1_1_1 --> clef_C1:CTO_hasClef
77+
inc_1_1_1 --> keysig_xF:CTO_hasKeySignature
78+
inc_1_1_1 --> TIMESIG:CTO_hasIncipitTimeSignature
79+
80+
class RISM_201001959
81+
class RISM_inc_1_1_1
82+
class RISM-online
83+
class E5313
84+
class G
85+
class C1
86+
state "xF" as keysig_xF
87+
88+
```

docs/extra-loader.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const uml = async className => {
2+
3+
// Custom element to encapsulate Mermaid content.
4+
class MermaidDiv extends HTMLElement {
5+
6+
/**
7+
* Creates a special Mermaid div shadow DOM.
8+
* Works around issues of shared IDs.
9+
* @return {void}
10+
*/
11+
constructor() {
12+
super()
13+
14+
// Create the Shadow DOM and attach style
15+
const shadow = this.attachShadow({mode: "open"})
16+
const style = document.createElement("style")
17+
style.textContent = `
18+
:host {
19+
display: block;
20+
line-height: initial;
21+
font-size: 16px;
22+
}
23+
div.diagram {
24+
margin: 0;
25+
overflow: visible;
26+
}`
27+
shadow.appendChild(style)
28+
}
29+
}
30+
31+
if (typeof customElements.get("diagram-div") === "undefined") {
32+
customElements.define("diagram-div", MermaidDiv)
33+
}
34+
35+
const getFromCode = parent => {
36+
// Handles <pre><code> text extraction.
37+
let text = ""
38+
for (let j = 0; j < parent.childNodes.length; j++) {
39+
const subEl = parent.childNodes[j]
40+
if (subEl.tagName.toLowerCase() === "code") {
41+
for (let k = 0; k < subEl.childNodes.length; k++) {
42+
const child = subEl.childNodes[k]
43+
const whitespace = /^\s*$/
44+
if (child.nodeName === "#text" && !(whitespace.test(child.nodeValue))) {
45+
text = child.nodeValue
46+
break
47+
}
48+
}
49+
}
50+
}
51+
return text
52+
}
53+
54+
// Provide a default config in case one is not specified
55+
const defaultConfig = {
56+
startOnLoad: false,
57+
theme: "default",
58+
flowchart: {
59+
htmlLabels: false
60+
},
61+
er: {
62+
useMaxWidth: false
63+
},
64+
sequence: {
65+
useMaxWidth: false,
66+
noteFontWeight: "14px",
67+
actorFontSize: "14px",
68+
messageFontSize: "16px"
69+
}
70+
}
71+
72+
// Load up the config
73+
mermaid.mermaidAPI.globalReset()
74+
const config = (typeof mermaidConfig === "undefined") ? defaultConfig : mermaidConfig
75+
mermaid.initialize(config)
76+
77+
// Find all of our Mermaid sources and render them.
78+
const blocks = document.querySelectorAll(`pre.${className}, diagram-div`)
79+
const surrogate = document.querySelector("html body")
80+
for (let i = 0; i < blocks.length; i++) {
81+
const block = blocks[i]
82+
const parentEl = (block.tagName.toLowerCase() === "diagram-div") ?
83+
block.shadowRoot.querySelector(`pre.${className}`) :
84+
block
85+
86+
// Create a temporary element with the typeset and size we desire.
87+
// Insert it at the end of our parent to render the SVG.
88+
const temp = document.createElement("div")
89+
temp.style.visibility = "hidden"
90+
temp.style.display = "display"
91+
temp.style.padding = "0"
92+
temp.style.margin = "0"
93+
temp.style.lineHeight = "initial"
94+
temp.style.fontSize = "16px"
95+
surrogate.appendChild(temp)
96+
97+
try {
98+
const res = await mermaid.render(`_diagram_${i}`, getFromCode(parentEl), temp)
99+
const content = res.svg
100+
const fn = res.bindFunctions
101+
const el = document.createElement("div")
102+
el.className = className
103+
el.innerHTML = content
104+
if (fn) {
105+
fn(el)
106+
}
107+
108+
// Insert the render where we want it and remove the original text source.
109+
// Mermaid will clean up the temporary element.
110+
const shadow = document.createElement("diagram-div")
111+
shadow.shadowRoot.appendChild(el)
112+
block.parentNode.insertBefore(shadow, block)
113+
parentEl.style.display = "none"
114+
shadow.shadowRoot.appendChild(parentEl)
115+
if (parentEl !== block) {
116+
block.parentNode.removeChild(block)
117+
}
118+
} catch (err) {} // eslint-disable-line no-empty
119+
120+
if (surrogate.contains(temp)) {
121+
surrogate.removeChild(temp)
122+
}
123+
}
124+
}
125+
126+
// This should be run on document load
127+
document.addEventListener("DOMContentLoaded", () => {uml("mermaid")})

mkdocs.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ nav:
2121
# - Misc: datasources.md
2222
# - DDB: ddb.md
2323
# - Factgrid: factgrid.md
24+
25+
markdown_extensions:
26+
- pymdownx.superfences:
27+
preserve_tabs: true
28+
custom_fences:
29+
# Mermaid diagrams
30+
- name: mermaid
31+
class: mermaid
32+
format: !!python/name:pymdownx.superfences.fence_code_format
33+
34+
extra_javascript:
35+
# - optionalConfig.js
36+
- https://cdn.jsdelivr.net/npm/mermaid@11.12.0/dist/mermaid.min.js
37+
- extra-loader.js

0 commit comments

Comments
 (0)