Last active
February 21, 2025 12:18
-
-
Save rberaldo/2a3bd82d5ed4bc39fee7e8ff4a6242b2 to your computer and use it in GitHub Desktop.
Obsidian to org-roam migration kit – a completely half-baked attempt at moving from Obsidian to org-roam which nevertheless works (for me).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys,re,os | |
if not os.path.isdir("out/"): | |
os.mkdir("out/") | |
md_file = sys.argv[1] | |
org_file = md_file[:-3] + ".org" | |
def replace(pattern, substitution, filename): | |
f = open(filename, "r+") | |
content = f.read() | |
content = re.sub(pattern, substitution, content) | |
f.seek(0) | |
f.write(content) | |
f.truncate() | |
f.close() | |
# Treat all comments in file | |
re_comm = re.compile(r"^%%(.*?)%%", re.MULTILINE) | |
replace(re_comm, r"#!#comment: \1", md_file) | |
# Ensure space after "---" | |
re_ruler = re.compile(r"^---\n(.+)", re.MULTILINE) | |
replace(re_ruler, r"---\n\n\1", md_file) | |
# Convert from md to org | |
pandoc_command = 'pandoc -f markdown "{0}" --lua-filter=remove-header-attr.lua'\ | |
' --wrap=preserve -o out/"{1}"'.format(md_file,org_file) | |
os.system(pandoc_command) | |
# Regularize comments | |
re_comm_org = re.compile(r"^#!#comment:(.*?)$", re.MULTILINE) | |
replace(re_comm_org, r"#\1", "out/" + org_file) | |
# Convert all kinds of links | |
re_url = re.compile(r"\[\[(.*?)\]\[(.*?)\]\]") | |
re_link = re.compile(r"\[\[(.*?)\]\]") | |
re_link_description = re.compile(r"\[\[(.*?)\|(.*?)\]\]") | |
with open("out/" + org_file, "r+") as f: | |
content = f.read() | |
new_content = "" | |
matches = re.finditer(r"\[\[.*?\]\]", content) | |
pos = 0 | |
for m in matches: | |
s = m.start() | |
e = m.end() | |
m_string = m.group(0) | |
if "://" in m_string: | |
new_content = new_content + content[pos:s] + re.sub(re_url, r"[[\1][\2]]", m_string) | |
elif "|" in m_string: | |
new_content = new_content + content[pos:s] + re.sub(re_link_description, r"[[file:\1.org][\2]]", m_string) | |
else: | |
new_content = new_content + content[pos:s] + re.sub(re_link, r"[[file:\1.org][\1]]", m_string) | |
pos = e | |
new_content = new_content + content[pos:] | |
f.seek(0) | |
f.write(new_content) | |
f.truncate() | |
print("Converted " + org_file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Header (header) | |
return pandoc.Header(header.level, header.content, pandoc.Attr()) | |
end |
No, no! Good job I'd say, Python is meant for such hackery.
Hey @s-c-p, thanks for the comment. That is really kind of you. Hope it has helped you, though I have found a few bugs which I manually corrected over my transition to org-roam.
Hi @rberaldo! I took this code and ran with it to make https://github.com/jml/obsidian-to-org, which I've used to convert my Obsidian files to org-roam. I hope you don't mind.
Let me know if you have an opinion about license. I'll default to GPL 3 if I don't hear from you.
@jml really cool work! GPL 3 is good to me. Maybe I'll update this gist to set a license and link to your work.
Thanks :) I've updated the repo with the license.
…On Mon, 6 Jun 2022 at 20:03, Rafael Beraldo ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@jml <https://github.com/jml> really cool work! GPL 3 is good to me.
Maybe I'll update this gist to set a license and link to your work.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/2a3bd82d5ed4bc39fee7e8ff4a6242b2#gistcomment-4191138>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA6V2XRZTLVHWA6RNH2JPDVNZDPVANCNFSM47L4JVNA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is absolutely half-assed – I'm a linguist, not a programmer – but it still seems to have worked to migrate my largish collection of notes from Obsidian to org-roam.
Feel free to criticize the hell out of it.
I start by converting all my beginning-of-line non-markdown-standard comments – lines starting and ending by
%%
– to transitional comments which are later translated into properorg
format comments.I ensure there's
\n
between instances of---
and text sopandoc
doesn't go all crazy making new headers.Then
pandoc
does its thing.After converting the comments into their final form, some arcane index alchemy I concocted converts wiki-style links into
org
-style links.This took me more than I'd like to admit to put together, so I hope someone gets good use out of it and contributes a better version.
Have fun! All hail Eris!