Forked from JohnAtl/insert_filename_as_header.rb
Last active Apr 12, 2020
Updated filename script to work with or without Zettelkasten ID
This file contains 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. | |
# 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 | |
title = filename.chomp('.md') | |
File.open('temp.txt', 'w') do |temp| | |
File.foreach(filename) do |line| | |
line_cnt += 1 #increment line count | |
if line_cnt == 1 #if we're evaluating first line | |
if line !~ /^# #{title}[ ]*$/ #if first line does NOT match title | |
temp.puts("# " + title + "\n\n") #then put it there | |
end | |
end | |
temp.puts(line) | |
end | |
end | |
File.rename(filename, filename + '.bak') | |
File.rename('temp.txt', filename) | |
end |
This file contains 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 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