From 7399520e9a9da8c73ab7e58a4060e8ba089474a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Fri, 25 Feb 2022 13:44:52 +0100 Subject: [PATCH] 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. --- Userland/Utilities/markdown-check.cpp | 41 ++++++++++++++------------- 1 file 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 #include #include +#include #include #include #include @@ -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);