1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +00:00

Utilities: Perform most markdown-check link checking with URLs

This should be much more robust against weirdly-formatted links that are
still valid URLs, additionally, future URL checkers can immediately
take advantage of the already-existing URL object.

Note that not all markdown links are valid URLs or paths, and that that
is intentional (e.g. only fragments, relative links etc.). We don't just
fail when something is not a URL.
This commit is contained in:
kleines Filmröllchen 2022-02-25 13:44:52 +01:00 committed by Idan Horowitz
parent 9902e71f99
commit 7399520e9a

View file

@ -18,6 +18,7 @@
#include <AK/OwnPtr.h>
#include <AK/RecursionDecision.h>
#include <AK/StdLibExtras.h>
#include <AK/URL.h>
#include <AK/Vector.h>
#include <LibCore/File.h>
#include <LibMarkdown/Document.h>
@ -166,25 +167,27 @@ RecursionDecision MarkdownLinkage::visit(Markdown::Text::LinkNode const& link_no
// Nothing to do here.
return RecursionDecision::Recurse;
}
if (href.starts_with("https://") || href.starts_with("http://")) {
outln("Not checking external link {}", href);
return RecursionDecision::Recurse;
}
if (href.starts_with("help://")) {
// TODO: Check that the man page actually exists. (That check would also fail because we are currently referring to some nonexistent man pages.)
outln("Not checking man page link {}", href);
return RecursionDecision::Recurse;
}
if (href.starts_with("file://")) {
// TODO: Resolve relative to $SERENITY_SOURCE_DIR/Base/
// Currently, this affects only one link, so it's not worth the effort.
outln("Not checking local link {}", href);
return RecursionDecision::Recurse;
}
if (href.starts_with("/res/icons/")) {
// TODO: Resolve relative to $SERENITY_SOURCE_DIR/Base/
outln("Not checking icon link {}", href);
return RecursionDecision::Recurse;
auto url = URL::create_with_url_or_path(href);
if (url.is_valid()) {
if (url.scheme() == "https" || url.scheme() == "http") {
outln("Not checking external link {}", href);
return RecursionDecision::Recurse;
}
if (url.scheme() == "help") {
// TODO: Check that the man page actually exists. (That check would also fail because we are currently referring to some nonexistent man pages.)
outln("Not checking man page link {}", href);
return RecursionDecision::Recurse;
}
if (url.scheme() == "file") {
// TODO: Resolve relative to $SERENITY_SOURCE_DIR/Base/, though we might refer to build-only files like binaries.
if (url.path().starts_with("/res/icons/")) {
outln("Not checking icon link {}", href);
return RecursionDecision::Recurse;
}
outln("Not checking local link {}", href);
return RecursionDecision::Recurse;
}
}
String label = StringCollector::from(*link_node.text);