Created
May 11, 2022 23:28
-
-
Save manuel-rubio/8f3841c8f35acbc753d5e73d221177e5 to your computer and use it in GitHub Desktop.
Broken local links extension for lambdapad
This file contains hidden or 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
@doc """ | |
Check local links, if there is a broken link the compilation fails. | |
You can configure the kind of URLs to avoid to check. For example, if you | |
want to avoid check dependencies like PDF or images: | |
[links] | |
exclude_uris = [ | |
'^/pdf/', | |
'^/images/' | |
] | |
You can use it adding to lambdapad.exs the line: | |
extension "gist:manuel-rubio/broken_local_links.exs" | |
""" | |
check "broken local links" do | |
set on: :finish | |
set run: fn(config) -> | |
urls = for {url, _} <- config[:url_data], do: URI.parse(url) | |
site_root = config["blog"]["url"] | |
for link <- config[:links] do | |
link = | |
if String.ends_with?(link, "index.html") do | |
String.replace_suffix(link, "/index.html", "") | |
else | |
link | |
end | |
exclude_uris = config["links"]["exclude_uris"] || [] | |
if String.starts_with?(link, "/") and Enum.all?(exclude_uris, ¬ Regex.match?(Regex.compile!(&1), link)) do | |
url = | |
site_root | |
|> Path.join(link) | |
|> URI.parse() | |
if URI.parse(url) not in urls do | |
raise """ | |
Broken link: #{inspect(link)} | |
""" | |
end | |
end | |
end | |
config | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment