Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Inkdrop backup file to TextBundle for importing to Bear
#!/usr/bin/env ruby
# Convert Inkdrop backup files (.json) to TextBundle v2 (.textbundle).for importing to Bear
# Usage: inkdrop2textbundle note:*.json
require "json"
require "tmpdir"
require "fileutils"
require "base64"
def inkdrop_to_textbundle(src)
src = File.expand_path(src)
src_dir = File.dirname(src)
# read note
inkdrop = JSON.parse(File.read(src))
raise "Unknown doctype: #{inkdrop["doctype"].inspect}" unless inkdrop["doctype"] == "markdown"
text = inkdrop["body"]
Dir.mktmpdir do |tmpdir|
# create $(tmpdir)/foo.textbundle
textbundle = File.join(tmpdir, File.basename(src).gsub(/\.json$/, ".textbundle"))
Dir.mkdir(textbundle)
Dir.chdir(textbundle) do
Dir.mkdir("assets")
# copy asset, replace path
text.gsub!(%r{\(inkdrop://(file:.+?)\)}){
id = $1
file = JSON.parse(File.read("#{src_dir}/#{id}.json"))
extension = case file["contentType"]
when "image/jpeg"
".jpeg"
when "image/png"
".png"
else
raise "Unknown image type: #{mime.inspect}"
end
data = Base64.decode64(file["_attachments"]["index"]["data"])
file_name = "assets/#{id}#{extension}"
File.write(file_name, data)
"(#{file_name})"
}
tags = inkdrop["tags"].map{|id|
tag = JSON.parse(File.read("#{src_dir}/#{id}.json"))
tag["name"]
}
tag_line = tags.empty? ? nil : (tags.map{|t| "##{t}" }.join(" ") + "\n")
text = [
"# " + inkdrop["title"] + "\n",
tag_line,
text,
].compact.join("\n")
File.write("text.markdown", text)
File.write("info.json", {
"transient" => true,
"type" => "net.daringfireball.markdown",
"creatorIdentifier" => "net.shinyfrog.bear",
"version" => 2,
}.to_json)
end
dest = "#{src_dir}/#{File.basename(textbundle)}"
FileUtils.cp_r(textbundle, dest)
FileUtils.touch(dest, mtime: Time.at(inkdrop["updatedAt"] / 1000.0).utc)
end
end
ARGV.each{|f| inkdrop_to_textbundle(f) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment