Created
January 13, 2023 17:14
-
-
Save sei0o/4fe7771038ffffa4e8a9826719213b50 to your computer and use it in GitHub Desktop.
YAML frontmatterを持つMarkdownファイルに、適当なslugを与えるスクリプト
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
# $ 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