@@ -34,7 +34,7 @@ def __init__(self, template_path: str | Path | None = None):
3434 Initialize the converter.
3535
3636 Args:
37- template_path: Path to HTML template (optional)
37+ template_path: Path to HTML template (optional, defaults to templates/markmap.html )
3838 """
3939 self .base_dir = Path (__file__ ).parent
4040
@@ -46,109 +46,13 @@ def __init__(self, template_path: str | Path | None = None):
4646 else :
4747 template_path = self .base_dir / "templates" / "markmap.html"
4848
49- if template_path .exists ():
50- self .template = Template (template_path .read_text (encoding = "utf-8" ))
51- else :
52- # Use default template
53- self .template = Template (self ._default_template ())
54-
55- def _default_template (self ) -> str :
56- """Return default HTML template."""
57- return """<!DOCTYPE html>
58- <html lang="en">
59- <head>
60- <meta charset="UTF-8">
61- <meta name="viewport" content="width=device-width, initial-scale=1.0">
62- <title>{{ title }} - NeetCode Mind Maps</title>
63- <style>
64- * { margin: 0; padding: 0; box-sizing: border-box; }
65- html, body { height: 100%; }
66- .markmap { width: 100%; height: 100%; }
67- .markmap > svg { width: 100%; height: 100%; }
68- #topbar {
69- position: fixed; top: 0; left: 0; right: 0; z-index: 100;
70- background: #fff; border-bottom: 1px solid #e5e7eb;
71- padding: 8px 16px; display: flex; gap: 8px;
72- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
73- font-size: 13px;
74- }
75- #topbar button {
76- padding: 4px 12px; border: 1px solid #d1d5db;
77- border-radius: 4px; background: #fff; cursor: pointer;
78- }
79- #topbar button:hover { background: #f3f4f6; }
80- .markmap { margin-top: 40px; height: calc(100% - 40px); }
81- </style>
82- <script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
83- <script src="https://cdn.jsdelivr.net/npm/markmap-view"></script>
84- <script src="https://cdn.jsdelivr.net/npm/markmap-lib"></script>
85- <script src="https://cdn.jsdelivr.net/npm/markmap-toolbar"></script>
86- <script>
87- function fitView() {
88- var svg = document.querySelector('.markmap > svg');
89- if (svg && svg.mm) svg.mm.fit();
90- }
91- function expandAll() {
92- var svg = document.querySelector('.markmap > svg');
93- if (svg && svg.mm) {
94- var root = svg.mm.state.data;
95- (function expand(n) {
96- n.payload = Object.assign({}, n.payload, { fold: 0 });
97- if (n.children) n.children.forEach(expand);
98- })(root);
99- svg.mm.setData(root); svg.mm.fit();
100- }
101- }
102- function collapseAll() {
103- var svg = document.querySelector('.markmap > svg');
104- if (svg && svg.mm) {
105- var root = svg.mm.state.data;
106- root.children && root.children.forEach(function collapse(n) {
107- if (n.children && n.children.length) {
108- n.payload = Object.assign({}, n.payload, { fold: 1 });
109- n.children.forEach(collapse);
110- }
111- });
112- svg.mm.setData(root); svg.mm.fit();
113- }
114- }
115- document.addEventListener('DOMContentLoaded', function() {
116- const { Transformer, Markmap } = window.markmap;
117- const transformer = new Transformer();
118- const markdown = `{{ markdown_content | safe }}`;
119- const { root } = transformer.transform(markdown);
120- const svg = d3.select('.markmap').append('svg');
121- const mm = Markmap.create(svg.node(), { color: (node) => node.payload?.color || '#f59e0b' }, root);
122- svg.node().mm = mm;
123- if (window.markmap && window.markmap.Toolbar) {
124- const toolbar = new window.markmap.Toolbar();
125- toolbar.attach(mm);
126- setTimeout(function() {
127- document.querySelectorAll('.mm-toolbar').forEach(function(toolbar) {
128- toolbar.querySelectorAll('.mm-toolbar-item').forEach(function(item) {
129- if ((item.title || '').toLowerCase().includes('dark')) item.remove();
130- });
131- var brand = toolbar.querySelector('.mm-toolbar-brand');
132- if (brand) {
133- brand.innerHTML = '🟡 NeetCode';
134- brand.href = '#'; brand.onclick = function(e) { e.preventDefault(); };
135- brand.style.fontSize = '12px'; brand.style.color = '#666';
136- }
137- });
138- }, 200);
139- }
140- });
141- </script>
142- </head>
143- <body>
144- <div id="topbar">
145- <button onclick="fitView()">Fit View</button>
146- <button onclick="expandAll()">Expand All</button>
147- <button onclick="collapseAll()">Collapse All</button>
148- </div>
149- <div class="markmap"></div>
150- </body>
151- </html>"""
49+ if not template_path .exists ():
50+ raise FileNotFoundError (
51+ f"Template file not found: { template_path } \n "
52+ "Please ensure templates/markmap.html exists or specify a custom template path."
53+ )
54+
55+ self .template = Template (template_path .read_text (encoding = "utf-8" ))
15256
15357 def convert (
15458 self ,
0 commit comments