Skip to content

Instantly share code, notes, and snippets.

@trashhalo
Last active June 20, 2022 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trashhalo/f1c51f782a87728a4d831809380f3ea9 to your computer and use it in GitHub Desktop.
Save trashhalo/f1c51f782a87728a4d831809380f3ea9 to your computer and use it in GitHub Desktop.
# Script to migrate from obsidian readwise to logseq readwise
# 1. Create a new logseq vault and sync your readwise content into it. Script assumes you are in this new folder.
# 2. Merge obsidian readwise content into new vault using this ruby file
# 3. Delete Readwise folder from obsidian vault
# 4. copy the pages files into your obsidian pages folder
# map of url => new filename
logseq_url_map = Dir["pages/*"].map do |page|
[File.read(page)[/^\s*url:: (.*)$/, 1], page]
end.to_h
# map of full title => new filename
logseq_full_title_map = Dir["pages/*"].map do |page|
[File.read(page)[/^\s*full-title:: (.*)$/, 1], page]
end.to_h
# map of new file name => title
logseq_new_title_map = Dir["pages/*"].map do |page|
[page, File.read(page)[/^\s*title:: (.*)$/, 1]]
end.to_h
# iterate over old obsidian readwise files
Dir.glob("../Notes2.0/Readwise/**/*.md").each do |page|
content = File.read(page)
url = content[/^\s*URL:: (.*)$/, 1]
full_title = content[/^\s*full title:: (.*)$/, 1]
title = content[/^\s*title:: (.*)$/, 1]
filename = nil
# did we find a url? try to match by url
if !url.nil?
filename = logseq_url_map[url]
end
# url didnt work? did we find a title? try that
if filename.nil? && !full_title.nil?
filename = logseq_full_title_map[full_title]
end
if filename.nil?
print "warn #{page} no match\n"
next
end
# replace title with new title format
content[title] = logseq_new_title_map[filename]
# write obsidian readwise content onto new filename
File.write(filename, content)
en
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment