Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to help with note-link-janitor
#!/usr/bin/ruby
#
# Inserts the file's name as the first header in the file, if it isn't already there.
# Assumes filename begins with a 14 digit zettelkasten ID.
# Original file saved as filename.bak.
#
# As always, caveat emptor. This code could harm your files, so try it on a backup.
#
Dir.glob('*.md') do |filename|
puts "working on: #{filename}"
id_text = nil
line_cnt = 0
should_add = false
File.open('temp.txt', 'w') do |temp|
File.foreach(filename) do |line|
line_cnt += 1
if line_cnt == 1
if line !~ /# \d{14} .+/
temp.puts("# " + filename + "\n\n")
end
if line =~ /(# \d{14} .+)\.md/
line = $1
end
end
temp.puts(line)
end
end
File.rename(filename, filename + '.bak')
File.rename('temp.txt', filename)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.