43 lines
754 B
Ruby
Executable File
43 lines
754 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
fix = ARGV.shift if ARGV.first == "--fix"
|
|
files = Array(ARGV.shift || Dir.glob("./**/*.md"))
|
|
|
|
files.each do |file|
|
|
content = File.read(file)
|
|
|
|
refs = content.scan(/^\[\d+\]:/)
|
|
|
|
unless refs == refs.uniq
|
|
warn "Error in #{file}: duplicate refs detected"
|
|
exit 1
|
|
end
|
|
|
|
idx = 0
|
|
code_block = false
|
|
refs = {}
|
|
|
|
updated = content.gsub(/\[\d+\]|```|`/) do |token|
|
|
case token
|
|
when "```", "`"
|
|
code_block = !code_block
|
|
token
|
|
else
|
|
if code_block
|
|
token
|
|
else
|
|
refs[token] ||= "[$$#{idx += 1}]"
|
|
end
|
|
end
|
|
end
|
|
|
|
updated.gsub!("$$", "")
|
|
|
|
if fix
|
|
File.write(file, updated)
|
|
elsif content != updated
|
|
warn "Links in #{file} are not in order"
|
|
exit 1
|
|
end
|
|
end
|