Skip to content

Instantly share code, notes, and snippets.

@elliottwilliams
Created September 15, 2017 18:24
Show Gist options
  • Save elliottwilliams/c672bc822d7a527868d1c590016887f6 to your computer and use it in GitHub Desktop.
Save elliottwilliams/c672bc822d7a527868d1c590016887f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'set'
# Searches through the given markdown files to find reference-style links that
# are not defined.
abort "Usage: unlinked.rb [files...]" if ARGV.empty?
ARGV.each do |filename|
defs = Set.new
reqs = Set.new
open(filename) do |file|
contents = file.read
contents.scan(/^\[(\w+)\]:/) { |l| defs << l }
contents.scan(/\[[^\]]+\]\[([^\]]+)\]/) { |l| reqs << l }
end
diff = reqs - defs
abort "#{filename} does not define: #{diff.to_a.join(', ')}" unless diff.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment