diff --git a/include/Void/Text/Markdown.hpp b/include/Void/Text/Markdown.hpp index c2676e7..39ad28e 100644 --- a/include/Void/Text/Markdown.hpp +++ b/include/Void/Text/Markdown.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,14 @@ namespace Void::Text // https, mailto, or a relative URL); unsafe schemes such as javascript: and // data: are dropped, keeping only the label text. // +// An optional MarkdownOptions::resolver can rewrite a destination (e.g. a +// relative wiki link to a bounded server route) after the allow-list check and +// before escaping. It is called once per emitted link/image destination, in +// document order; its result is escaped like any other destination. Unsafe +// destinations are dropped regardless of the resolver, which is never called +// for them. If the resolver throws, the original validated destination is +// used; an overlong result is reported as MarkdownError::Code::output_limit. +// // All work is bounded by MarkdownLimits: input size, output size, block count // and nesting depth. @@ -53,10 +62,15 @@ struct MarkdownError [[nodiscard]] constexpr bool operator==(const MarkdownError&) const noexcept = default; }; +// Rewrites a validated link/image destination. `image` is true for images. +// The returned string is escaped before being emitted. +using MarkdownResolver = std::function; + struct MarkdownOptions { MarkdownLimits limits{}; bool allow_images = true; + MarkdownResolver resolver{}; }; namespace Detail @@ -713,6 +727,23 @@ inline void render_inline(std::string_view s, return false; } +// Apply the optional resolver to an already-validated destination. A throwing +// resolver falls back to the original destination; the writer bounds the size +// of whatever comes back. +[[nodiscard]] inline std::string resolve_destination(const MarkdownOptions& opt, + std::string_view destination, + bool image) +{ + if (!opt.resolver) { + return std::string{destination}; + } + try { + return opt.resolver(destination, image); + } catch (...) { + return std::string{destination}; + } +} + [[nodiscard]] inline std::size_t try_link(std::string_view s, std::size_t open, MarkdownWriter& w, @@ -738,19 +769,20 @@ inline void render_inline(std::string_view s, render_inline(label, w, opt, depth + 1); return paren + 1; } + if (image && !opt.allow_images) { + render_inline(label, w, opt, depth + 1); + return paren + 1; + } + const std::string resolved = resolve_destination(opt, dest, image); if (image) { - if (opt.allow_images) { - w.append("\"");"); - } else { - render_inline(label, w, opt, depth + 1); - } + w.append("\"");"); } else { w.append(" #include +#include #include #include +#include namespace { @@ -21,6 +23,13 @@ std::string render(std::string_view input) return r ? *r : std::string{}; } +std::string render_with(std::string_view input, MarkdownOptions options) +{ + const auto r = markdown_to_html(input, options); + Void::Testing::expect(r.has_value()); + return r ? *r : std::string{}; +} + MarkdownError render_error(std::string_view input, MarkdownOptions options) { const auto r = markdown_to_html(input, options); @@ -275,6 +284,90 @@ Void::Testing::Test markdown_code_block_content_is_escaped() std::string{"
</code><script>x</script>\n
\n"}); } +// ── Destination resolver ─────────────────────────────────────────────────── + +Void::Testing::Test markdown_resolver_called_once_per_destination_in_order() +{ + std::vector seen; + MarkdownOptions options; + options.resolver = [&seen](std::string_view dest, bool image) { + seen.push_back((image ? std::string{"img:"} : std::string{"link:"}) + + std::string{dest}); + return "https://wiki.local" + std::string{dest}; + }; + const auto r = markdown_to_html("[a](/one) ![i](/two.png) [b](/three)", options); + Void::Testing::expect(r.has_value()); + Void::Testing::expect_eq( + *r, + std::string{"

a " + "\"i\" " + "b

\n"}); + Void::Testing::expect_eq(seen.size(), std::size_t{3}); + Void::Testing::expect_eq(seen[0], std::string{"link:/one"}); + Void::Testing::expect_eq(seen[1], std::string{"img:/two.png"}); + Void::Testing::expect_eq(seen[2], std::string{"link:/three"}); +} + +Void::Testing::Test markdown_resolver_output_is_escaped() +{ + MarkdownOptions options; + options.resolver = [](std::string_view, bool) { + return std::string{"\">"}; + }; + const std::string out = render_with("[x](/safe)", options); + Void::Testing::expect(out.find("