serenevoid / kiwi.nvim

A stripped down VimWiki for Neovim
https://serenevoid.github.io/blog/my-note-taking-plugin
GNU General Public License v3.0
178 stars 9 forks source link

Porting from VimWiki to Kiwi #6

Closed YHRen closed 7 months ago

YHRen commented 11 months ago

Hi, I noticed the VimWiki (markdown) has hyperlink defined as [topic](topic), whereas kiwi uses the actual file path [topic](./topic.md). Both contain markdown notes are in a flat folder.

I wrote a python script to convert the VimWiki notes to the Kiwi notes. Perhaps it can help others.

[update, 10/04/2023] fix a bug: replace space in filename with underscore

import regex as re
import sys
from pathlib import Path

# usage:
#   python vimwiki2kiwi.py <path_of_vimwiki_note_folder> <path_of_kiwi_note_folder>
#   e.g. python vimwiki2kiwi.py ./notes ./output
input_folder = Path(sys.argv[1])
output_folder = Path(sys.argv[2])
p = re.compile("(\[.*\]\()(.*)(\))")

def func(obj):
    m = obj.group(2).replace(" ", "_")
    m = "./" + m + ".md"
    return obj.group(1) + m + obj.group(3)

if not input_folder.exists():
    print(f"input folder {input_folder} does not exist.")
    exit(1)

if not output_folder.exists():
    print(f"output folder {output_folder} does not exist.")
    print("creating one")
    output_folder.mkdir(parents=True, exist_ok=True)

for file in input_folder.glob("./*.md"):
    print(f"converting {file}")
    outfile = output_folder / file.name.replace(" ", "_")
    outfile.touch()
    with open(file, "r") as ifp, open(outfile, "w") as ofp:
        for line in ifp:
            newline = re.sub(p, func, line)
            print(newline, file=ofp, end="")
serenevoid commented 11 months ago

This would be very useful for the people who would like to try out kiwi.nvim with their Vimwiki repo. Thank you for sharing this. I'll probably move this script to the wiki section of the repository in a few days just for organizing.