diff options
author | kleines Filmröllchen <filmroellchen@serenityos.org> | 2022-02-25 13:44:52 +0100 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2022-02-26 20:05:06 +0200 |
commit | 7399520e9a9da8c73ab7e58a4060e8ba089474a4 (patch) | |
tree | 30be4e973db50ffe5aba1e7a79080f441b79e5b8 /Userland/Utilities/markdown-check.cpp | |
parent | 9902e71f99ad4a00ae77b356540df175983ec0b1 (diff) | |
download | serenity-7399520e9a9da8c73ab7e58a4060e8ba089474a4.zip |
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.
Diffstat (limited to 'Userland/Utilities/markdown-check.cpp')
-rw-r--r-- | Userland/Utilities/markdown-check.cpp | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/Userland/Utilities/markdown-check.cpp b/Userland/Utilities/markdown-check.cpp index a86b492d9d..50ffcf2dbf 100644 --- a/Userland/Utilities/markdown-check.cpp +++ b/Userland/Utilities/markdown-check.cpp @@ -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); |