使用Go语言实现xmind文件转换为markdown

 更新时间:2025年06月06日 08:51:22   作者:叹一曲当时只道是寻常  
这篇文章主要来和大家一起深入探讨如何用Go语言构建一个强大的命令行工具,实现XMind到Markdown的无损转换,感兴趣的小伙伴可以跟随小编一起学习一下

解锁思维导图新姿势

将XMind转为结构化Markdown

你是否曾遇到过这些场景?

  • 精心设计的XMind思维导图需要分享给只支持Markdown的协作者
  • 想将思维导图发布到支持Markdown的博客平台
  • 需要版本化管理思维导图内容

今天我们将深入探讨如何用Go语言构建一个强大的命令行工具,实现XMind到Markdown的无损转换。

一、认识Xmind结构

和docx等格式一样,xmind本质上来说是一个压缩包,将节点信息压缩在文件内。

├── Thumbnails/      # 缩略图
├── content.json     # 核心内容
├── content.xml     
├── manifest.json
├── metadata.json    # 元数据
├── Revisions/       # 修订历史
└── resources/       # 附件资源

其中最关键的是content.json文件,它用JSON格式存储了完整的思维导图数据结构。我们的转换工具需要精准解析这个文件。

二、核心转换流程详解

1.解压XMind文件(ZIP处理)

r, err := zip.OpenReader(inputPath)
defer r.Close()

for _, f := range r.File {
    if f.Name == "content.json" {
        // 读取文件内容
    }
}

这里使用标准库archive/zip读取压缩包,精准定位核心JSON文件。异常处理是关键点:

  • 检查是否为有效ZIP文件
  • 确保content.json存在
  • 处理文件读取错误

2.解析JSON数据结构

我们定义了精准映射JSON的Go结构体:

// XMindContent represents the structure of content.json
type XMindContent []struct {
    ID        string `json:"id"`
    Class     string `json:"class"`
    Title     string `json:"title"`
    RootTopic struct {
        ID             string `json:"id"`
        Class          string `json:"class"`
        Title          string `json:"title"`
        Href           string `json:"href"`
        StructureClass string `json:"structureClass"`
        Children       struct {
            Attached []Topic `json:"attached"`
        } `json:"children"`
    } `json:"rootTopic"`
}

type Topic struct {
    Title    string `json:"title"`
    ID       string `json:"id"`
    Href     string `json:"href"`
    Position struct {
        X float64 `json:"x"`
        Y float64 `json:"y"`
    } `json:"position"`
    Children struct {
        Attached []Topic `json:"attached"`
    } `json:"children"`
    Branch  string `json:"branch"`
    Markers []struct {
        MarkerID string `json:"markerId"`
    } `json:"markers"`
    Summaries []struct {
        Range   string `json:"range"`
        TopicID string `json:"topicId"`
    } `json:"summaries"`
    Image struct {
        Src   string `json:"src"`
        Align string `json:"align"`
    } `json:"image"`
    AttributedTitle []struct {
        Text string `json:"text"`
    } `json:"attributedTitle"`
}

核心:

  • 嵌套结构匹配XMind的树形数据
  • Attached字段处理多分支结构
  • 支持标记(markers)和超链接(href)解析

3:递归转换树形结构

func printTopic(topic Topic, level int, output *os.File) {
    // 动态计算缩进
    fmt.Fprintf(output, "%s- ", strings.Repeat("  ", level))
    
    // 处理超链接
    if topic.Href != "" {
        fmt.Fprintf(output, "[%s](%s)", topic.Title, topic.Href)
    } else {
        fmt.Fprint(output, topic.Title)
    }
    
    // 添加标记图标
    if len(topic.Markers) > 0 {
        fmt.Fprint(output, " [")
        for i, m := range topic.Markers {
            if i > 0 { fmt.Print(", ") }
            fmt.Fprint(output, m.MarkerID)
        }
        fmt.Print("]")
    }
    fmt.Println()
    
    // 递归处理子节点
    for _, child := range topic.Children.Attached {
        printTopic(child, level+1, output)
    }
}

递归策略:

  • 每个节点根据层级生成对应缩进
  • 动态处理超链接和标记
  • 深度优先遍历确保结构正确性

4:Markdown层级生成逻辑

采用清晰的标题层级映射:

# 思维导图名称        // H1
## 中心主题          // H2
### 主要分支         // H3
- 子主题1           // 无序列表
  - 子子主题         // 缩进列表

这种结构完美保留了:

  • 原始信息的层次关系
  • 超链接资源
  • 优先级标记(旗帜/星标等)

三、完整代码

/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"archive/zip"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/spf13/cobra"
)

// XMindContent represents the structure of content.json
type XMindContent []struct {
	ID        string `json:"id"`
	Class     string `json:"class"`
	Title     string `json:"title"`
	RootTopic struct {
		ID             string `json:"id"`
		Class          string `json:"class"`
		Title          string `json:"title"`
		Href           string `json:"href"`
		StructureClass string `json:"structureClass"`
		Children       struct {
			Attached []Topic `json:"attached"`
		} `json:"children"`
	} `json:"rootTopic"`
}

type Topic struct {
	Title    string `json:"title"`
	ID       string `json:"id"`
	Href     string `json:"href"`
	Position struct {
		X float64 `json:"x"`
		Y float64 `json:"y"`
	} `json:"position"`
	Children struct {
		Attached []Topic `json:"attached"`
	} `json:"children"`
	Branch  string `json:"branch"`
	Markers []struct {
		MarkerID string `json:"markerId"`
	} `json:"markers"`
	Summaries []struct {
		Range   string `json:"range"`
		TopicID string `json:"topicId"`
	} `json:"summaries"`
}

func generateMarkdown(sheets XMindContent, outputPath string) error {
	// Create output file
	outputFile, err := os.Create(outputPath)
	if err != nil {
		return fmt.Errorf("failed to create output file: %v", err)
	}
	defer outputFile.Close()

	// Generate Markdown for each sheet
	for _, sheet := range sheets {
		// Sheet title as H1
		fmt.Fprintf(outputFile, "# %s\n\n", sheet.Title)

		// Root topic title as H2
		fmt.Fprintf(outputFile, "## %s\n", sheet.RootTopic.Title)
		if sheet.RootTopic.Href != "" {
			fmt.Fprintf(outputFile, "[%s](%s)\n", sheet.RootTopic.Title, sheet.RootTopic.Href)
		}
		fmt.Fprintln(outputFile)

		// First level topics as H3
		for _, topic := range sheet.RootTopic.Children.Attached {
			fmt.Fprintf(outputFile, "### %s\n", topic.Title)
			if topic.Href != "" {
				fmt.Fprintf(outputFile, "[%s](%s)\n", topic.Title, topic.Href)
			}

			// Print markers if present
			if len(topic.Markers) > 0 {
				fmt.Fprint(outputFile, "Markers: ")
				for i, marker := range topic.Markers {
					if i > 0 {
						fmt.Fprint(outputFile, ", ")
					}
					fmt.Fprint(outputFile, marker.MarkerID)
				}
				fmt.Fprintln(outputFile)
			}

			// Deeper levels as lists
			for _, child := range topic.Children.Attached {
				printTopic(child, 0, outputFile)
			}
			fmt.Fprintln(outputFile) // Add extra space between topics
		}
	}

	return nil
}

func printTopic(topic Topic, level int, output *os.File) {
	// Print topic title with indentation
	fmt.Fprintf(output, "%s- ", getIndent(level))

	// Handle title with or without href
	if topic.Href != "" {
		fmt.Fprintf(output, "[%s](%s)", topic.Title, topic.Href)
	} else {
		fmt.Fprint(output, topic.Title)
	}

	// Show markers if present
	if len(topic.Markers) > 0 {
		fmt.Fprint(output, " [")
		for i, marker := range topic.Markers {
			if i > 0 {
				fmt.Fprint(output, ", ")
			}
			fmt.Fprint(output, marker.MarkerID)
		}
		fmt.Fprint(output, "]")
	}
	fmt.Fprintln(output)

	// Recursively print subtopics
	for _, child := range topic.Children.Attached {
		printTopic(child, level+1, output)
	}
}

func getIndent(level int) string {
	indent := ""
	for i := 0; i < level; i++ {
		indent += "  "
	}
	return indent
}

func Convert(inputPath, outputPath string) error {
	// 1. Unzip XMind file
	r, err := zip.OpenReader(inputPath)
	if err != nil {
		return fmt.Errorf("failed to open XMind file: %v", err)
	}
	defer r.Close()

	// 2. Read content.json
	var content []byte
	for _, f := range r.File {
		if f.Name == "content.json" {
			rc, err := f.Open()
			if err != nil {
				return fmt.Errorf("failed to open content.json: %v", err)
			}
			defer rc.Close()
			content, err = ioutil.ReadAll(rc)
			if err != nil {
				return fmt.Errorf("failed to read content.json: %v", err)
			}
			break
		}
	}
	if content == nil {
		return fmt.Errorf("content.json not found in XMind file")
	}

	// 3. Parse content.json
	var xmindContent XMindContent
	err = json.Unmarshal(content, &xmindContent)
	if err != nil {
		return fmt.Errorf("failed to parse JSON: %v", err)
	}

	// 4. Generate Markdown
	return generateMarkdown(xmindContent, outputPath)
}

// xmind2mdCmd represents the xmind2md command
var xmind2mdCmd = &cobra.Command{
	Use:   "xmind2md",
	Short: "Convert XMind to Markdown",
	Long:  `Transform XMind mind maps to Markdown format`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			fmt.Println("Please provide an input XMind file")
			return
		}
		inputFile := args[0]
		outputFile, _ := cmd.Flags().GetString("output")
		if outputFile == "" {
			// 去除.xmind后缀
			if len(inputFile) > 6 && inputFile[len(inputFile)-6:] == ".xmind" {
				outputFile = inputFile[:len(inputFile)-6] + ".md"
			} else {
				outputFile = inputFile + ".md"
			}

		}
		fmt.Printf("Converting %s to %s\n", inputFile, outputFile)
		err := Convert(inputFile, outputFile)
		if err != nil {
			fmt.Printf("Error: %v\n", err)
		} else {
			fmt.Printf("Successfully converted to %s\n", outputFile)
		}
	},
}

func init() {
	xmind2mdCmd.Flags().StringP("output", "o", "", "output file")
	rootCmd.AddCommand(xmind2mdCmd)
}

到此这篇关于使用Go语言实现xmind文件转换为markdown的文章就介绍到这了,更多相关Go xmind转markdown内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Go语言如何使用标准库sort对切片进行排序

    详解Go语言如何使用标准库sort对切片进行排序

    Sort 标准库提供了对基本数据类型的切片和自定义类型的切片进行排序的函数。今天主要分享的内容是使用 Go 标准库 sort 对切片进行排序,感兴趣的可以了解一下
    2022-12-12
  • go1.8之安装配置具体步骤

    go1.8之安装配置具体步骤

    下面小编就为大家带来一篇go1.8之安装配置具体步骤。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Go 实现 WebSockets之创建 WebSockets

    Go 实现 WebSockets之创建 WebSockets

    这篇文章主要介绍了Go 实现 WebSockets之创建 WebSockets,文章主要探索 WebSockets,并简要介绍了它们的工作原理,并仔细研究了全双工通信,想了解更多相关内容的小伙伴可以参考一下
    2022-04-04
  • golang定时任务cron项目实操指南

    golang定时任务cron项目实操指南

    Go实现的cron 表达式的基本语法跟linux 中的 crontab基本是类似的,下面这篇文章主要给大家介绍了关于golang定时任务cron项目实操的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • golang 实现json类型不确定时的转换

    golang 实现json类型不确定时的转换

    这篇文章主要介绍了golang 实现json类型不确定时的转换操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Go模块布局管理文档翻译理解

    Go模块布局管理文档翻译理解

    这篇文章主要为大家介绍了Go模块布局管理文档翻译理解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Go语言sync.Map详解及使用场景

    Go语言sync.Map详解及使用场景

    Go语言中的sync.Map是一个高效的并发安全映射结构,适用于高并发读多写少的场景,它通过读写分离、无锁读取路径、写入时的锁保护等机制,提高了读取性能并减少了锁竞争,sync.Map不需要手动管理锁,降低了编程复杂性,适合需要简单并发访问的场合
    2024-10-10
  • Golang的第一个程序-Hello World

    Golang的第一个程序-Hello World

    这篇文章主要介绍了第一个Go程序-Hello World,在编写第一个go程序之前,我们要将系统的环境变量配好,下面来看具体的编一过程吧,需要的小伙伴可以参考一下
    2022-01-01
  • Golang 模块引入及表格读写业务快速实现示例

    Golang 模块引入及表格读写业务快速实现示例

    这篇文章主要为大家介绍了Golang模块引入及表格读写业务的快速实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • go设置多个GOPATH的方式

    go设置多个GOPATH的方式

    这篇文章主要介绍了go设置多个GOPATH的方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05

最新评论