Skip to content

Instantly share code, notes, and snippets.

@labocho
Created July 19, 2018 07:21
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 labocho/ca6c0c32121d773c9938cb4cfa2167b7 to your computer and use it in GitHub Desktop.
Save labocho/ca6c0c32121d773c9938cb4cfa2167b7 to your computer and use it in GitHub Desktop.
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