Changes
diff --git a/.gitignore b/.gitignore
index 4c1dba5..3af09fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/build_wasm/
+!tests/md-html-suite/code-4-spaces-escaping.out
diff --git a/src/standard_library.cpp b/src/standard_library.cpp
index e043306..8d8e2a0 100644
--- a/src/standard_library.cpp
+++ b/src/standard_library.cpp
@@ -3,6 +3,43 @@
namespace macrodown
{
+namespace
+{
+
+// Escape literal code text before inserting it into an HTML element.
+std::string escapeCode(const std::string& content)
+{
+ std::string escaped;
+ escaped.reserve(content.size());
+ for(char character : content)
+ {
+ switch(character)
+ {
+ case '&':
+ escaped += "&";
+ break;
+ case '<':
+ escaped += "<";
+ break;
+ case '>':
+ escaped += ">";
+ break;
+ default:
+ escaped += character;
+ break;
+ }
+ }
+ return escaped;
+}
+
+// Render the literal contents of an inline code span.
+std::string renderCode(const std::vector<std::string>& args)
+{
+ return "<code>" + (args.empty() ? "" : escapeCode(args[0])) + "</code>";
+}
+
+} // namespace
+
void StandardLibrary::registerMacros(Evaluator& evaluator)
{
// Blocks
@@ -48,7 +85,7 @@ void StandardLibrary::registerMacros(Evaluator& evaluator)
return "";
}
std::string info = args[0];
- std::string content = args[1];
+ std::string content = escapeCode(args[1]);
if(!info.empty())
{
return "<pre><code class=\"language-" + info + "\">" + content +
@@ -60,7 +97,7 @@ void StandardLibrary::registerMacros(Evaluator& evaluator)
// Inline
evaluator.define("em", {"content"}, "<em>%content</em>");
evaluator.define("strong", {"content"}, "<strong>%content</strong>");
- evaluator.define("code", {"content"}, "<code>%content</code>");
+ evaluator.defineIntrinsic("code", renderCode);
evaluator.define("link", {"url", "text"}, "<a href=\"%url\">%text</a>");
evaluator.define("img", {"url", "alt"},
"<img src=\"%url\" alt=\"%alt\" />");
diff --git a/tests/md-html-suite/code-4-spaces-escaping.out b/tests/md-html-suite/code-4-spaces-escaping.out
new file mode 100644
index 0000000..67d3043
--- /dev/null
+++ b/tests/md-html-suite/code-4-spaces-escaping.out
@@ -0,0 +1,3 @@
+<pre><code>10 PRINT < > &
+20 GOTO 10
+</code></pre>
diff --git a/tests/test_macrodown.cpp b/tests/test_macrodown.cpp
index 321bd9c..64728ae 100644
--- a/tests/test_macrodown.cpp
+++ b/tests/test_macrodown.cpp
@@ -167,6 +167,23 @@ TEST(MacroDownTest, CommonMarkCode)
EXPECT_EQ(md.render(*md.parse(indented_input)), indented_expected);
}
+// Code contents are literal text, including HTML and entity spellings.
+TEST(MacroDownTest, CodeEscapesHtml)
+{
+ MacroDown md;
+ EXPECT_EQ(md.render(*md.parse(
+ "> the `marker-box` which is a `<div>`.")),
+ "<blockquote>\n<p>the <code>marker-box</code> which is a "
+ "<code><div></code>.</p>\n</blockquote>\n");
+ EXPECT_EQ(md.render(*md.parse("`<div> & < *text* %em{x}`")),
+ "<p><code><div> & &lt; *text* %em{x}"
+ "</code></p>\n");
+ EXPECT_EQ(md.render(*md.parse("```\n<div> & <\n```")),
+ "<pre><code><div> & &lt;\n</code></pre>\n");
+ EXPECT_EQ(md.render(*md.parse(" <div> & <")),
+ "<pre><code><div> & &lt;\n</code></pre>\n");
+}
+
// Verify that a paragraph following a list correctly closes the list block.
TEST(MacroDownTest, ListFollowedByParagraph)
{
@@ -256,5 +273,5 @@ TEST(MacroDownTest, InvalidAutolinksRemainText)
EXPECT_EQ(md.render(*md.parse(R"(\<https://example.com/>)")),
"<p><https://example.com/></p>\n");
EXPECT_EQ(md.render(*md.parse("`<https://example.com/>`")),
- "<p><code><https://example.com/></code></p>\n");
+ "<p><code><https://example.com/></code></p>\n");
}