prev-cli 0.1.6 → 0.1.8

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/dist/cli.js CHANGED
@@ -387,7 +387,10 @@ async function createViteConfig(options) {
387
387
  "@prev/theme": path4.join(srcRoot2, "theme"),
388
388
  react: path4.join(cliNodeModules, "react"),
389
389
  "react-dom": path4.join(cliNodeModules, "react-dom"),
390
- "react-router-dom": path4.join(cliNodeModules, "react-router-dom")
390
+ "react-router-dom": path4.join(cliNodeModules, "react-router-dom"),
391
+ mermaid: path4.join(cliNodeModules, "mermaid"),
392
+ dayjs: path4.join(cliNodeModules, "dayjs"),
393
+ "@terrastruct/d2": path4.join(cliNodeModules, "@terrastruct/d2")
391
394
  }
392
395
  },
393
396
  optimizeDeps: {
@@ -398,7 +401,10 @@ async function createViteConfig(options) {
398
401
  "react-dom/client",
399
402
  "react-router-dom",
400
403
  "react/jsx-runtime",
401
- "react/jsx-dev-runtime"
404
+ "react/jsx-dev-runtime",
405
+ "mermaid",
406
+ "dayjs",
407
+ "@terrastruct/d2"
402
408
  ],
403
409
  exclude: [
404
410
  "clsx",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prev-cli",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Transform MDX directories into beautiful documentation websites",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -37,31 +37,47 @@ async function renderD2Diagrams() {
37
37
  const codeBlocks = document.querySelectorAll('code.language-d2, code.hljs.language-d2')
38
38
  if (codeBlocks.length === 0) return
39
39
 
40
- const { D2 } = await import('@terrastruct/d2')
41
- const d2 = new D2()
40
+ try {
41
+ const d2Module = await import('@terrastruct/d2')
42
+ const { D2 } = d2Module
43
+ const d2 = new D2()
42
44
 
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'
45
+ for (const block of codeBlocks) {
46
+ const pre = block.parentElement as HTMLElement
47
+ if (!pre || pre.dataset.rendered) continue
48
+ pre.dataset.rendered = 'true'
47
49
 
48
- const code = block.textContent || ''
49
- const container = document.createElement('div')
50
- container.className = 'd2-diagram'
50
+ const code = block.textContent || ''
51
+ const container = document.createElement('div')
52
+ container.className = 'd2-diagram'
51
53
 
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)
54
+ try {
55
+ const result = await d2.compile(code)
56
+ const svg = await d2.render(result.diagram, result.renderOptions)
57
+ container.innerHTML = svg
58
+ // Hide original instead of replacing (avoids React DOM conflicts)
59
+ pre.style.display = 'none'
60
+ pre.insertAdjacentElement('afterend', container)
61
+ } catch (e) {
62
+ console.error('D2 render error:', e)
63
+ }
61
64
  }
65
+ } catch (e) {
66
+ console.error('D2 library load error:', e)
62
67
  }
63
68
  }
64
69
 
70
+ // Clean up rendered diagrams (for navigation)
71
+ function cleanupDiagrams() {
72
+ // Remove all rendered diagram containers
73
+ document.querySelectorAll('.mermaid-diagram, .d2-diagram').forEach(el => el.remove())
74
+ // Reset rendered state on pre elements and restore visibility
75
+ document.querySelectorAll('pre[data-rendered="true"]').forEach(el => {
76
+ delete (el as HTMLElement).dataset.rendered;
77
+ (el as HTMLElement).style.display = ''
78
+ })
79
+ }
80
+
65
81
  // Render all diagrams
66
82
  async function renderDiagrams() {
67
83
  await Promise.all([
@@ -74,9 +90,14 @@ export function Layout() {
74
90
  const location = useLocation()
75
91
 
76
92
  useEffect(() => {
93
+ // Clean up old diagrams before rendering new ones
94
+ cleanupDiagrams()
77
95
  // Render diagrams after content loads
78
96
  const timer = setTimeout(renderDiagrams, 100)
79
- return () => clearTimeout(timer)
97
+ return () => {
98
+ clearTimeout(timer)
99
+ cleanupDiagrams()
100
+ }
80
101
  }, [location.pathname])
81
102
 
82
103
  return (