Hello everyone, my name is Xiaoming.
Recently, many bloggers have demanded that they need to quickly extract the directory in the Markdown document and convert it into an xmind mind map. It is said that the official provides a method to directly generate an xmind mind map with python, but some people report that the generated file cannot be opened. So based on this situation, I will develop an efficiency aid to help you quickly convert Markdown to xmind.
Introduction to the use of the software
First open the program, the interface is as follows:
We can first select the folder where the loaded Markdown document is located:
Then click the Load button again:
All md files in that directory are loaded into the list. We can also tick the recursive checkbox to load:
At this point the included subfiles are all loaded into the list.
How to get the directory corresponding to the specified Markdown file?
Just click on the corresponding list item:
At this point, the Markdown directory has been copied to the cut version, and we can paste it into the xmind software. After opening the xmind software, delete the redundant nodes, select the central node, and then paste:
It can be seen that the result has become a perfect mind map. At this time, we only need to modify the name of the central node and save it.
Of course, some Markdown documents are special, such as:
We want to remove the first-level title and raise the titles of other levels by one level. You can modify the minimum title level to 2 and then click on the article:
At this point, go to xmind and paste it to get the desired effect.
Of course, for the files in the list, we can also export directory files in batches, click Batch Export. After clicking batch export, first select a save location, and then start exporting:
If you click and cancel the selection of the save folder, the execution of the export task will be terminated.
develop code
First develop a function to extract the Markdown directory:
import cchardet
def load_md(filename, min_level=1):
with open(filename, "rb") as f:
md_bytes = f.read()
encoding = cchardet.detect(md_bytes)['encoding']
if encoding is None:
encoding = "u8"
md = md_bytes.decode(encoding)
code_area = False
result = []
for line in md.splitlines():
if line.startswith("```"):
code_area = not code_area
if code_area or not line.startswith("#") or line.find(" ") == -1:
continue
num_sign, title = line.split(maxsplit=1)
if not title:
continue
tab_num = len(num_sign) - min_level
if tab_num < 0:
continue
result.append("\t" * tab_num + title)
return result
Here I use cchardet for encoding recognition, readers who have not used it need to install:
pip install cchardet
The principle of the above code is to read the Markdown text by line, and the text that starts with # and contains spaces is considered to be a line with a title, and excludes the case where # is in the code area.
For other codes, interested children's shoes can study by themselves. Please download the complete code and packaged tools from codeChina.
Tools and open source code download address: