summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-09-28 20:51:46 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-09-28 20:51:46 +0200
commitd43c7d55f49de8b5fd52f55bb88bf3b972c33484 (patch)
tree3d9de14c7adc0c1a03b767d18729ed4008dc3c06 /Libraries
parent83f643d43cf9e0b659d71c9cb990c4ff17194eaf (diff)
downloadserenity-d43c7d55f49de8b5fd52f55bb88bf3b972c33484.zip
LibMarkdown: Support escaping of special characters
This allows you to escape function\_names\_like\_this() :^)
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibMarkdown/MDText.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/Libraries/LibMarkdown/MDText.cpp b/Libraries/LibMarkdown/MDText.cpp
index 9be080df9b..060184112f 100644
--- a/Libraries/LibMarkdown/MDText.cpp
+++ b/Libraries/LibMarkdown/MDText.cpp
@@ -1,6 +1,20 @@
#include <AK/StringBuilder.h>
#include <LibMarkdown/MDText.h>
+static String unescape(const StringView& text)
+{
+ StringBuilder builder;
+ for (int i = 0; i < text.length(); ++i) {
+ if (text[i] == '\\' && i != text.length() - 1) {
+ builder.append(text[i + 1]);
+ i++;
+ continue;
+ }
+ builder.append(text[i]);
+ }
+ return builder.build();
+}
+
String MDText::render_to_html() const
{
StringBuilder builder;
@@ -97,6 +111,12 @@ bool MDText::parse(const StringView& str)
for (int offset = 0; offset < str.length(); offset++) {
char ch = str[offset];
+ bool is_escape = ch == '\\';
+ if (is_escape && offset != str.length() - 1) {
+ offset++;
+ continue;
+ }
+
bool is_special_character = false;
is_special_character |= ch == '`';
if (!current_style.code)
@@ -128,7 +148,7 @@ bool MDText::parse(const StringView& str)
if (current_span_start < str.length()) {
Span span {
- str.substring_view(current_span_start, str.length() - current_span_start),
+ unescape(str.substring_view(current_span_start, str.length() - current_span_start)),
current_style
};
m_spans.append(move(span));