Text: add Markdown link/image destination resolver hook

MarkdownOptions::resolver rewrites a validated link/image destination
before it is escaped and emitted, once per destination in document order.
Unsafe schemes (javascript:, data:, ...) are still dropped and never reach
the resolver. A throwing resolver falls back to the original destination;
an overlong result is bounded by MarkdownLimits::max_output.

Closes #59
This commit is contained in:
faumaray committed 2026-12-04 21:48:20 +03:00
1 parent 549c7cfccb
commit e6745ac8cb
2 files changed
+135 -10

No files matched your search

+42 -10
View File
@@ -5,6 +5,7 @@
#include <cstddef>
#include <cstdint>
#include <expected>
#include <functional>
#include <string>
#include <string_view>
@@ -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<std::string(std::string_view destination, bool image)>;
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("<img src=\"");
append_escaped(w, dest);
w.append("\" alt=\"");
append_escaped(w, label);
w.append("\" />");
} else {
render_inline(label, w, opt, depth + 1);
}
w.append("<img src=\"");
append_escaped(w, resolved);
w.append("\" alt=\"");
append_escaped(w, label);
w.append("\" />");
} else {
w.append("<a href=\"");
append_escaped(w, dest);
append_escaped(w, resolved);
w.append("\"");
if (!title.empty()) {
w.append(" title=\"");