Skip to content

Instantly share code, notes, and snippets.

@dmyates
Created June 14, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmyates/3df6cbc9c9191c258f5d0d33c281b371 to your computer and use it in GitHub Desktop.
Save dmyates/3df6cbc9c9191c258f5d0d33c281b371 to your computer and use it in GitHub Desktop.
# 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