prev-cli 0.1.4 → 0.1.5
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.
- package/package.json +2 -1
- package/src/theme/Layout.tsx +47 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prev-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Transform MDX directories into beautiful documentation websites",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@mdx-js/rollup": "^3.0.0",
|
|
44
|
+
"@terrastruct/d2": "^0.1.33",
|
|
44
45
|
"@tailwindcss/vite": "^4.0.0",
|
|
45
46
|
"@vitejs/plugin-react-swc": "^4.2.2",
|
|
46
47
|
"class-variance-authority": "^0.7.0",
|
package/src/theme/Layout.tsx
CHANGED
|
@@ -5,15 +5,16 @@ import './styles.css'
|
|
|
5
5
|
|
|
6
6
|
// Lazy-load and render mermaid diagrams
|
|
7
7
|
async function renderMermaidDiagrams() {
|
|
8
|
-
const codeBlocks = document.querySelectorAll('code.language-mermaid')
|
|
8
|
+
const codeBlocks = document.querySelectorAll('code.language-mermaid, code.hljs.language-mermaid')
|
|
9
9
|
if (codeBlocks.length === 0) return
|
|
10
10
|
|
|
11
11
|
const mermaid = await import('mermaid')
|
|
12
12
|
mermaid.default.initialize({ startOnLoad: false, theme: 'neutral' })
|
|
13
13
|
|
|
14
14
|
for (const block of codeBlocks) {
|
|
15
|
-
const pre = block.parentElement
|
|
16
|
-
if (!pre || pre.dataset.
|
|
15
|
+
const pre = block.parentElement as HTMLElement
|
|
16
|
+
if (!pre || pre.dataset.rendered) continue
|
|
17
|
+
pre.dataset.rendered = 'true'
|
|
17
18
|
|
|
18
19
|
const code = block.textContent || ''
|
|
19
20
|
const container = document.createElement('div')
|
|
@@ -22,19 +23,59 @@ async function renderMermaidDiagrams() {
|
|
|
22
23
|
try {
|
|
23
24
|
const { svg } = await mermaid.default.render(`mermaid-${Math.random().toString(36).slice(2)}`, code)
|
|
24
25
|
container.innerHTML = svg
|
|
25
|
-
|
|
26
|
+
// Hide original instead of replacing (avoids React DOM conflicts)
|
|
27
|
+
pre.style.display = 'none'
|
|
28
|
+
pre.insertAdjacentElement('afterend', container)
|
|
26
29
|
} catch (e) {
|
|
27
30
|
console.error('Mermaid render error:', e)
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
35
|
+
// Lazy-load and render D2 diagrams
|
|
36
|
+
async function renderD2Diagrams() {
|
|
37
|
+
const codeBlocks = document.querySelectorAll('code.language-d2, code.hljs.language-d2')
|
|
38
|
+
if (codeBlocks.length === 0) return
|
|
39
|
+
|
|
40
|
+
const { D2 } = await import('@terrastruct/d2')
|
|
41
|
+
const d2 = new D2()
|
|
42
|
+
|
|
43
|
+
for (const block of codeBlocks) {
|
|
44
|
+
const pre = block.parentElement as HTMLElement
|
|
45
|
+
if (!pre || pre.dataset.rendered) continue
|
|
46
|
+
pre.dataset.rendered = 'true'
|
|
47
|
+
|
|
48
|
+
const code = block.textContent || ''
|
|
49
|
+
const container = document.createElement('div')
|
|
50
|
+
container.className = 'd2-diagram'
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const result = await d2.compile(code)
|
|
54
|
+
const svg = await d2.render(result.diagram)
|
|
55
|
+
container.innerHTML = svg
|
|
56
|
+
// Hide original instead of replacing (avoids React DOM conflicts)
|
|
57
|
+
pre.style.display = 'none'
|
|
58
|
+
pre.insertAdjacentElement('afterend', container)
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.error('D2 render error:', e)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Render all diagrams
|
|
66
|
+
async function renderDiagrams() {
|
|
67
|
+
await Promise.all([
|
|
68
|
+
renderMermaidDiagrams(),
|
|
69
|
+
renderD2Diagrams()
|
|
70
|
+
])
|
|
71
|
+
}
|
|
72
|
+
|
|
32
73
|
export function Layout() {
|
|
33
74
|
const location = useLocation()
|
|
34
75
|
|
|
35
76
|
useEffect(() => {
|
|
36
|
-
// Render
|
|
37
|
-
const timer = setTimeout(
|
|
77
|
+
// Render diagrams after content loads
|
|
78
|
+
const timer = setTimeout(renderDiagrams, 100)
|
|
38
79
|
return () => clearTimeout(timer)
|
|
39
80
|
}, [location.pathname])
|
|
40
81
|
|