diff --git a/src/Service/ActivityPub/MarkdownConverter.php b/src/Service/ActivityPub/MarkdownConverter.php index 5bd477bba..ecc01b141 100644 --- a/src/Service/ActivityPub/MarkdownConverter.php +++ b/src/Service/ActivityPub/MarkdownConverter.php @@ -23,6 +23,7 @@ public function convert(string $value): string { $converter = new HtmlConverter(['strip_tags' => true]); $converter->getEnvironment()->addConverter(new TableConverter()); + $converter->getEnvironment()->addConverter(new StrikethroughConverter()); $value = stripslashes($converter->convert($value)); preg_match_all('/\[([^]]*)\] *\(([^)]*)\)/i', $value, $matches, PREG_SET_ORDER); diff --git a/src/Service/ActivityPub/StrikethroughConverter.php b/src/Service/ActivityPub/StrikethroughConverter.php new file mode 100644 index 000000000..ca69495e7 --- /dev/null +++ b/src/Service/ActivityPub/StrikethroughConverter.php @@ -0,0 +1,48 @@ +config = $config; + } + + public function convert(ElementInterface $element): string + { + $value = $element->getValue(); + if (!trim($value)) { + return $value; + } + + $prefix = ltrim($value) !== $value ? ' ' : ''; + $suffix = rtrim($value) !== $value ? ' ' : ''; + + /* If this node is immediately preceded or followed by one of the same type don't emit + * the start or end $style, respectively. This prevents foobar from + * being converted to ~~foo~~~~bar~~ which is incorrect. We want ~~foobar~~ instead. + */ + $preStyle = \in_array($element->getPreviousSibling()?->getTagName(), $this->getSupportedTags()) ? '' : '~~'; + $postStyle = \in_array($element->getNextSibling()?->getTagName(), $this->getSupportedTags()) ? '' : '~~'; + + return $prefix.$preStyle.trim($value).$postStyle.$suffix; + } +}