");
+ append_escaped(w, code);
+ w.append("");
+ i = close + run;
+ } else {
+ append_escaped(w, s.substr(i, run));
+ i += run;
+ }
+ continue;
+ }
+
+ if (c == '*' || c == '_') {
+ if (try_emphasis(s, i, w, opt, depth)) {
+ continue;
+ }
+ append_escaped(w, s.substr(i, 1));
+ ++i;
+ continue;
+ }
+
+ if (c == '[') {
+ const std::size_t end = try_link(s, i, w, opt, depth, false);
+ if (end != npos) {
+ i = end;
+ continue;
+ }
+ append_escaped(w, s.substr(i, 1));
+ ++i;
+ continue;
+ }
+
+ if (c == '!' && i + 1 < s.size() && s[i + 1] == '[') {
+ const std::size_t end = try_link(s, i + 1, w, opt, depth, true);
+ if (end != npos) {
+ i = end;
+ continue;
+ }
+ append_escaped(w, s.substr(i, 1));
+ ++i;
+ continue;
+ }
+
+ if (c == '<') {
+ const std::size_t close = s.find('>', i + 1);
+ if (close != npos) {
+ const std::string_view inner = s.substr(i + 1, close - i - 1);
+ if (is_autolink(inner)) {
+ w.append("");
+ append_escaped(w, inner);
+ w.append("");
+ i = close + 1;
+ continue;
+ }
+ }
+ append_escaped(w, s.substr(i, 1));
+ ++i;
+ continue;
+ }
+
+ if (c == '\n') {
+ if (w.out.size() >= 2 && w.out[w.out.size() - 1] == ' ' &&
+ w.out[w.out.size() - 2] == ' ') {
+ w.out.resize(w.out.size() - 2);
+ w.append("");
+ append_escaped(w, code);
+ w.append("\n\n");
+ continue;
+ }
+
+ const int level = atx_heading(line);
+ if (level != 0) {
+ consume_line(rest);
+ const std::string_view content = heading_content(line, level);
+ const char digit = static_cast\n"); + render_blocks(inner, w, opt, depth + 1, blocks, false); + w.append("\n"); + continue; + } + + bool ordered = false; + int start = 1; + std::size_t offset = 0; + if (parse_list_marker(line, ordered, start, offset)) { + w.append(ordered ? "
"); + render_inline(para, w, opt, depth + 1); + w.append("
\n"); + } + } +} +} // namespace Detail + +/// Parse a bounded CommonMark subset from `input` and render it to safe HTML. +/// Raw HTML is escaped, never passed through; unsafe link destinations are +/// dropped. Returns a `MarkdownError` when a configured limit is exceeded. +[[nodiscard]] inline std::expected####### seven
\n"}); +} + +Void::Testing::Test markdown_atx_closing_sequence() +{ + Void::Testing::expect_eq(render("## title ##"), std::string{"one\ntwo
\nthree
\n"}); +} + +Void::Testing::Test markdown_empty_input() +{ + Void::Testing::expect_eq(render(""), std::string{}); + Void::Testing::expect_eq(render("\n\n \n"), std::string{}); +} + +// ── Inline constructs ─────────────────────────────────────────────────────── + +Void::Testing::Test markdown_emphasis_and_strong() +{ + Void::Testing::expect_eq(render("*em*"), std::string{"em
\n"}); + Void::Testing::expect_eq(render("_em_"), std::string{"em
\n"}); + Void::Testing::expect_eq(render("**strong**"), + std::string{"strong
\n"}); + Void::Testing::expect_eq(render("__strong__"), + std::string{"strong
\n"}); + Void::Testing::expect_eq(render("**bold *and em***"), + std::string{"bold and em
\n"}); +} + +Void::Testing::Test markdown_underscore_intraword_is_literal() +{ + Void::Testing::expect_eq(render("snake_case_name"), + std::string{"snake_case_name
\n"}); +} + +Void::Testing::Test markdown_unmatched_delimiters_are_literal() +{ + Void::Testing::expect_eq(render("*unclosed"), std::string{"*unclosed
\n"}); + Void::Testing::expect_eq(render("a * b"), std::string{"a * b
\n"}); +} + +Void::Testing::Test markdown_code_spans() +{ + Void::Testing::expect_eq(render("`a < b`"), std::string{"a < b
a ` b
code
*not em*
\n"}); + Void::Testing::expect_eq(render("\\[not a link\\]"), + std::string{"[not a link]
\n"}); +} + +Void::Testing::Test markdown_hard_breaks() +{ + Void::Testing::expect_eq(render("one \ntwo"), + std::string{"one
\ntwo
one
\ntwo

alt text
\n"}); +} + +Void::Testing::Test markdown_autolink() +{ + Void::Testing::expect_eq( + render("int x = 1;\n\n"});
+ Void::Testing::expect_eq(
+ render("```cpp\nif (a < b) {}\n```"),
+ std::string{"if (a < b) {}\n\n"});
+ // Tilde fences work too.
+ Void::Testing::expect_eq(render("~~~\nx\n~~~"),
+ std::string{"x\n\n"});
+}
+
+Void::Testing::Test markdown_unclosed_fence_renders_to_end()
+{
+ Void::Testing::expect_eq(render("```\nabc"),
+ std::string{"abc\n\n"});
+}
+
+Void::Testing::Test markdown_unordered_list()
+{
+ Void::Testing::expect_eq(render("- one\n- two"),
+ std::string{"\n\n"}); + Void::Testing::expect_eq(render("> a\n> b"), + std::string{"quoted
\n
\n\n"}); +} + +// ── Hostile input ────────────────────────────────────────────────────────── + +Void::Testing::Test markdown_raw_html_is_escaped() +{ + Void::Testing::expect_eq(render(""), + std::string{"a\nb
\n
<script>alert(1)</script>
\n"}); + Void::Testing::expect_eq( + render("<img src=x onerror=alert(1)>
\n"}); +} + +Void::Testing::Test markdown_javascript_links_dropped() +{ + Void::Testing::expect_eq(render("[x](javascript:alert(1))"), + std::string{"x
\n"}); + Void::Testing::expect_eq(render("[x](JaVaScRiPt:alert(1))"), + std::string{"x
\n"}); + Void::Testing::expect_eq(render("[x](data:text/html;base64,AAAA)"), + std::string{"x
\n"}); + Void::Testing::expect_eq(render("[x](vbscript:msgbox(1))"), + std::string{"x
\n"}); +} + +Void::Testing::Test markdown_entity_smuggled_scheme_dropped() +{ + Void::Testing::expect_eq(render("[x](javascript:alert(1))"), + std::string{"x
\n"}); + Void::Testing::expect_eq(render("[x](javascript:alert(1))"), + std::string{"x
\n"}); + Void::Testing::expect_eq(render("[x](javascript:alert(1))"), + std::string{"x
\n"}); +} + +Void::Testing::Test markdown_attribute_injection_is_escaped() +{ + const std::string out = render("[x](https://e.com/\" onmouseover=\"alert(1))"); + Void::Testing::expect(out.find("onmouseover=\"") == std::string::npos); + Void::Testing::expect(out.find(""") != std::string::npos); +} + +Void::Testing::Test markdown_code_block_content_is_escaped() +{ + Void::Testing::expect_eq(render("```\n\n```"), + std::string{"</code><script>x</script>\n\n"});
+}
+
+// ── Bounds ─────────────────────────────────────────────────────────────────
+
+Void::Testing::Test markdown_input_limit()
+{
+ MarkdownOptions options;
+ options.limits.max_input = 4;
+ const auto err = render_error("12345", options);
+ Void::Testing::expect(err.code == MarkdownError::Code::input_limit);
+}
+
+Void::Testing::Test markdown_output_limit()
+{
+ MarkdownOptions options;
+ options.limits.max_output = 8;
+ const auto err = render_error("# a long heading", options);
+ Void::Testing::expect(err.code == MarkdownError::Code::output_limit);
+}
+
+Void::Testing::Test markdown_block_limit()
+{
+ MarkdownOptions options;
+ options.limits.max_blocks = 1;
+ const auto err = render_error("one\n\ntwo", options);
+ Void::Testing::expect(err.code == MarkdownError::Code::block_limit);
+}
+
+Void::Testing::Test markdown_depth_limit()
+{
+ MarkdownOptions options;
+ options.limits.max_depth = 1;
+ const auto err = render_error("**a *b* c**", options);
+ Void::Testing::expect(err.code == MarkdownError::Code::depth_limit);
+}