Exporting a DayOne commonplace book to org-roam
I’ve kept some quotes in DayOne for years, and I frequently find myself coming back to them when I’m writing, either to actually use them or to just remember an idea. I thought it’d be handy to have them in my writing tool, so I used ChatGPT to help me write a quick exporter.
I’ll include that below, but the other thing to link to before your eyes glaze over with a hunk of Ruby is this useful post on using consult-ripgrep with org-roam for fulltext search from the org-roam Discourse. I’ve been careful to tag everything I’ve put in so far, but I won’t get to that with this batch of files, and I usually remember a keyword, anyhow. So they’re just tagged with “quotes” and “commonplace” for now.
The script just consumes the JSON that DayOne’s export function provides and coughs out formatted org-roam nodes. It uses DayOne’s uuid’s, but tacks on a few words – just in case? Maybe over time I’ll go clean up the titles but for now they’re just there to provide a hint when I search.
1#!/usr/bin/env ruby
2
3require 'json'
4require 'date'
5
6# Set these up before running
7src_json = "/path/to/file.json"
8destination_dir = "/path/to/export/dir"
9
10# Read the JSON file
11json_str = File.read(src_json)
12
13# Parse the JSON string
14data = JSON.parse(json_str)
15
16def write_json_to_org_roam_files(json_str, path)
17 data = JSON.parse(json_str)
18 entries = data['entries']
19 entries.each do |entry|
20 created_date = DateTime.parse(entry['creationDate']).strftime('%Y%m%d%H%M%S')
21 title = entry['text'].gsub(/^"/, '').gsub(/"$/, '').gsub(/[^a-zA-Z0-9 ]/, ' ').squeeze(' ').strip # replace non-alphanumeric with space, remove extra spaces
22 first_words = title.split.first(5).join(' ').gsub(/[^a-zA-Z0-9]/, '-') # replace non-alphanumeric with dash
23 uuid = first_words + '-' + entry['uuid']
24 content = JSON.parse(entry['richText'])['contents'].map { |c| c['text'] }.join("\n")
25 filename = "#{created_date}-#{first_words}.org".gsub(/^-/, '').gsub(/-$/, '').gsub(/-{2,}/,'-') # remove leading/trailing dashes and collapse multiple dashes
26 file_content = <<~ORG_NODE.gsub(/^\s*/, '')
27 :PROPERTIES:
28 :ID: #{uuid}
29 :END:
30 #+title: #{first_words}
31 #+filetags: :commonplace:quotes:
32
33 #{content}
34 ORG_NODE
35 file = File.open(File.join(path, filename), 'w')
36 file.write(file_content)
37 file.close
38 end
39end
40# Iterate over each entry
41data['entries'].each do |entry|
42 write_json_to_org_roam_files(json_str, File.expand_path(destination_dir))
43end