comment + refactor renumber

This commit is contained in:
David Eisinger
2023-10-31 11:24:50 -04:00
parent 91530fefc4
commit d70ca94988

View File

@@ -6,33 +6,40 @@ files = Array(ARGV.shift || Dir.glob("./**/*.md"))
files.each do |file|
content = File.read(file)
# First, scan for all references ([#]: at beginning of line)
# and look for duplicates
refs = content.scan(/^\[\d+\]:/)
dups = refs.combination(2).filter_map { |i, j| i if i == j }
unless refs == refs.uniq
warn "Error in #{file}: duplicate refs detected"
# If duplicate refs detected, this won't work, so bail out
if dups.any?
warn "Duplicate refs detected in #{file} (#{dups * ", "})"
exit 1
end
idx = 0
code_block = false
refs = {}
cache = {}
updated = content.gsub(/\[\d+\]|```|`/) do |token|
# Find all numbered links (in text + references) + code delimeters
# (for fenced code blocks, "`" suffices because "```" is an odd number)
updated = content.gsub(/\[\d+\]|`/) do |token|
case token
when "```", "`"
when "`"
# We don't want to do substitution inside code blocks
# (where `[#]` sometimes occurs) but we do want to toggle
# whether or not we're in a code block
code_block = !code_block
token
else
if code_block
token
else
refs[token] ||= "[$$#{idx += 1}]"
end
# If we're in a code block, just return the token
# Otherwise, check if the reference has already been assigned
# - If so, return the cached value
# - If not, bump the index, cache the value, and return it
code_block ? token : cache[token] ||= "[#{idx += 1}]"
end
end
updated.gsub!("$$", "")
if fix
File.write(file, updated)
elsif content != updated