Skip to content

Instantly share code, notes, and snippets.

@sei0o
Created January 13, 2023 17:14
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 sei0o/4fe7771038ffffa4e8a9826719213b50 to your computer and use it in GitHub Desktop.
Save sei0o/4fe7771038ffffa4e8a9826719213b50 to your computer and use it in GitHub Desktop.
YAML frontmatterを持つMarkdownファイルに、適当なslugを与えるスクリプト
# $ ruby slugadd.rb path/to/dir
dirname = ARGV[0] || '.'
notes = Dir.children dirname
notes.each do |path|
abspath = dirname + "/" + path
# subdirectories and non-Markdown files are ignored
next if File.directory? abspath
next unless abspath.end_with? ".md"
lines = IO.readlines abspath
in_frontmatter = false
pos = 0
lines.map(&:chomp).each_with_index do |line, i|
if in_frontmatter
if line == "---"
us = (Time.now.to_f * 1000000).floor
lines.insert(i, "slug: \"#{us}\"\n")
break
end
k, v = line.split ": "
break if k == "slug"
else
if line == "---"
in_frontmatter = true
end
end
end
IO.write abspath, lines.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment