From 7e10ead90a5e04f737e77c85262e1a19454542fc Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Thu, 2 Jan 2020 17:59:08 +0100 Subject: [PATCH] Escape text that should not be interpreted as Wiki syntax --- _test/json/unformatted.json | 14 ++++++++++++++ _test/json/unformatted.txt | 1 + parser/TextNode.php | 20 +++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 _test/json/unformatted.json create mode 100644 _test/json/unformatted.txt diff --git a/_test/json/unformatted.json b/_test/json/unformatted.json new file mode 100644 index 00000000..784461d7 --- /dev/null +++ b/_test/json/unformatted.json @@ -0,0 +1,14 @@ +{ + "type": "doc", + "content": [ + { + "type": "paragraph", + "content": [ + { + "type": "text", + "text": "__foo//bar%%" + } + ] + } + ] +} diff --git a/_test/json/unformatted.txt b/_test/json/unformatted.txt new file mode 100644 index 00000000..7bbe4d7b --- /dev/null +++ b/_test/json/unformatted.txt @@ -0,0 +1 @@ +%%__%%foo%%//%%bar%% diff --git a/parser/TextNode.php b/parser/TextNode.php index b8d61715..d36452c3 100644 --- a/parser/TextNode.php +++ b/parser/TextNode.php @@ -105,7 +105,25 @@ public function getPostfixSyntax() public function getInnerSyntax() { - return $this->text; + if ($this->marks['unformatted']) { + return $this->text; + } + return preg_replace_callback( + "/\bhttps?:\/\/|__+|\/\/+|%%+|''+|\*\*+/", + function ($matches) + { + switch ($matches[0][0]) { + case 'h': + // We matched http(s):// + return $matches[0]; + case '%': + return '' . $matches[0] . ''; + default: + return '%%' . $matches[0] . '%%'; + } + }, + $this->text + ); }