Skip to content

Instantly share code, notes, and snippets.

@dmyates
Created Jun 14, 2016
Embed
What would you like to do?
# Convert all files in dir from Ghost-compatible markdown to Hugo-compatible markdown
# (changes footnotes and headings)
#
# Usage: ruby md2md.rb /path/to/posts
# #Heading --> # Heading
def headings(str)
str.gsub(/(#+)(?=[^ #])/i,'\1 ')
end
# this[^n] is[^n] footnoted[^n] --> this[^m] is[^n] footnoted[^o]
def footnote_refs(str)
thing = ('m'..'z').to_a.cycle.each
while str =~ /\[\^n\](?=[^:])/
str.sub!(/\[\^n\](?=[^:])/,"[^#{thing.next}]")
end
str
end
# [^n]: Blah blah\n[^n]: Further blah\n[^n]: More blah --> [^m]: Blah blah\n[^n]: Further blah\n[^o]: More blah
def footnotes(str)
thing = ('m'..'z').to_a.cycle.each
while str =~ /\[\^n\]:/
str.sub!(/\[\^n\]:/,"[^#{thing.next}]:")
end
str
end
# Do this to every file.md in-place (living dangerously)
Dir.glob("#{ARGV[0]}/**/*.md") do |file|
f = open(file).read
f = headings(f)
f = footnote_refs(f)
f = footnotes(f)
File.open(file, 'w') do |fl|
fl.write f
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment