Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I'm in the process of turning my vimwiki diary into a set of zettelkasten following my Simple Markdown Zettelkasten format… here's a script to migrate them.
require 'date'
SRC = "./"
DST = "./transform"
ORGTAG = /:([a-zA-Z\-_]+):/
def render(data)
%{
title: #{data[:title]}
id: #{data[:id]}
tags: #{data[:tags]}
date: #{data[:date]}
#{data[:body]}
}
end
def id(datetime)
datetime.strftime("%Y%m%d%H%M%S")
end
def clean_title(string)
string
.downcase
.gsub(/[^a-zA-Z_]/, "_")
.gsub(/_{2,}/, '_')
end
Dir.foreach(SRC) do |filename|
if filename.match?(/.*\.md/)
puts "Filename: #{filename}"
data = Hash.new
content = File.read(filename)
# EXTRACT date from filename and put in metadata
datestring =
filename
.scan(/(\d+)/)
.flatten
.map{ |n| n.to_i }
date = DateTime.new(*datestring)
# ADD id to metadata based on date
data[:id] = id(date)
data[:date] = date.strftime("%a, %e %b %Y %T")
# EXTRACT tags from /:\w+:/ format and transform to hashtags
tags =
content
.scan(ORGTAG)
.flatten
.map{ |t| '#' + t.gsub(':', '').gsub('-', '_').downcase }
.uniq
content.gsub!(ORGTAG, '\1')
# ADD tag for #journal
tags.push('#journal')
data[:tags] = tags.join(', ')
# DETERMINE title from h1 (clean, remove tags)
title = content.match(/^# .*/).to_s.gsub("# ", "")
data[:title] = title
data[:body] = content
# WRITE filename with new filename to a new folder
puts "FILENAME: #{data[:id]}-#{clean_title(title)}.md"
puts render(data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment