diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..b9bf6c0 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .markmap_node import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] \ No newline at end of file diff --git a/markmap_node.py b/markmap_node.py new file mode 100644 index 0000000..b1d9806 --- /dev/null +++ b/markmap_node.py @@ -0,0 +1,132 @@ +import os +import folder_paths +import json + +class MarkmapNode: + """ + 将Markdown文本转换为思维导图的节点 + """ + + def __init__(self): + self.output_dir = os.path.join(folder_paths.get_output_directory(), 'markmap') + self.static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static') + if not os.path.exists(self.output_dir): + os.makedirs(self.output_dir) + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "markdown_text": ("STRING", {"multiline": True}), + "filename": ("STRING", {"default": "mindmap.html"}) + } + } + + RETURN_TYPES = ("STRING",) + FUNCTION = "create_markmap" + CATEGORY = "markmap" + + def create_markmap(self, markdown_text, filename): + # 确保文件名以.html结尾 + if not filename.endswith('.html'): + filename += '.html' + + # 使用规范化的路径 + output_path = os.path.normpath(os.path.join(self.output_dir, filename)) + + # 读取模板文件 + template_path = os.path.join(self.static_dir, 'markmap.html') + with open(template_path, 'r', encoding='utf-8') as f: + html_template = f.read() + + # 确保markdown文本格式正确 + markdown_lines = markdown_text.split('\n') + markdown_lines = [line.strip() for line in markdown_lines if line.strip()] + # 确保第一行是标题 + if not markdown_lines[0].startswith('#'): + markdown_lines.insert(0, '# Mindmap') + + # 解析markdown为树形结构 + def parse_markdown(lines): + root = {"content": lines[0].lstrip('#').strip(), "children": [], "payload": {"tag": "h1", "lines": "0,1"}} + current_level = 1 + current_node = root + parent_stack = [] + line_number = 1 + + for line in lines[1:]: + if line.startswith('#'): + level = len(line.split()[0]) + content = line.lstrip('#').strip() + node = {"content": content, "children": [], "payload": {"tag": f"h{level}", "lines": f"{line_number},{line_number+1}"}} + + while len(parent_stack) >= level - 1: + parent_stack.pop() + + parent = parent_stack[-1] if parent_stack else root + parent["children"].append(node) + parent_stack.append(node) + current_node = node + elif line.startswith('-'): + content = line.lstrip('- ').strip() + node = {"content": content, "children": [], "payload": {"tag": "li", "lines": f"{line_number},{line_number+1}"}} + current_node["children"].append(node) + + line_number += 1 + + return root + + # 生成数据结构 + data = parse_markdown(markdown_lines) + + # 替换模板中的数据 + html_content = html_template.replace('{{data}}', json.dumps(data)) + + # 使用UTF-8编码保存文件,不带BOM + with open(output_path, 'w', encoding='utf-8', newline='\n') as f: + f.write(html_content) + + return (output_path,) + +class ReadHtmlNode: + """ + 读取HTML文件内容并输出文本的节点 + """ + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "file_path": ("STRING", {}), + } + } + + RETURN_TYPES = ("STRING",) + FUNCTION = "read_html" + CATEGORY = "markmap" + + def read_html(self, file_path): + # 确保文件存在 + if not os.path.exists(file_path): + raise FileNotFoundError(f"File not found: {file_path}") + + # 读取HTML文件内容 + with open(file_path, 'r', encoding='utf-8') as f: + html_content = f.read() + + return (html_content,) + +# 更新节点列表 +NODE_CLASS_MAPPINGS = { + "MarkmapNode": MarkmapNode, + "ReadHtmlNode": ReadHtmlNode +} + +# 更新节点显示名称 +NODE_DISPLAY_NAME_MAPPINGS = { + "MarkmapNode": "Markdown to Mindmap", + "ReadHtmlNode": "Read HTML Content" +} \ No newline at end of file diff --git a/node_map.json b/node_map.json new file mode 100644 index 0000000..9b58ccd --- /dev/null +++ b/node_map.json @@ -0,0 +1,3 @@ +{ + "MarkmapNode": "Markdown to Mindmap" +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4c90572 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +# No additional requirements + + diff --git a/static/markmap.html b/static/markmap.html new file mode 100644 index 0000000..5058152 --- /dev/null +++ b/static/markmap.html @@ -0,0 +1,31 @@ + + + + + + + Markmap + + + + + + + + + + + + \ No newline at end of file