Created
February 7, 2020 20:25
-
-
Save JohnAtl/6f719f0249c881a5f7ff4c465909ae42 to your computer and use it in GitHub Desktop.
Code to help with note-link-janitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Converts links in files from [[20200207151300]] A sample link | |
# to [[20200207151300 A sample link]] | |
# Original files saved as filename.bak. | |
# | |
# As always, caveat emptor. This code could harm your files, so try it on a backup. | |
# | |
sed -i.bak -E 's/\[\[([0-9]{14})\]\] (.+)/\[\[\1 \2\]\]/' *.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Converts links in files from [[20200207151300 A sample link]] to | |
# [[20200207151300]] A sample link | |
# Original files saved as filename.bak. | |
# | |
# As always, caveat emptor. This code could harm your files, so try it on a backup. | |
# | |
sed -i.bak -E 's/\[\[([0-9]{14}) (.+)\]\]/\[\[\1\]\] \2/' *.md |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment