diff --git a/web/app/models/HTML.php b/web/app/models/HTML.php index 89bc8b6..f037f09 100644 --- a/web/app/models/HTML.php +++ b/web/app/models/HTML.php @@ -193,7 +193,7 @@ class HTML { } public static function parsedown() { - return new ParsedownMath([ + return new UOJMarkdown([ 'math' => [ 'enabled' => true, 'matchSingleDollar' => true diff --git a/web/app/models/UOJBlogEditor.php b/web/app/models/UOJBlogEditor.php index 6654b03..9178c99 100644 --- a/web/app/models/UOJBlogEditor.php +++ b/web/app/models/UOJBlogEditor.php @@ -108,15 +108,7 @@ class UOJBlogEditor { if ($this->show_editor) { if ($this->type == 'blog') { - $content_md = $_POST[$this->name . '_content_md']; - $dom = new DOMDocument; - $dom->loadHTML(mb_convert_encoding($parsedown->text($this->post_data['content_md']), 'HTML-ENTITIES', 'UTF-8')); - $elements = $dom->getElementsByTagName('table'); - foreach ($elements as $element) { - $element->setAttribute('class', - $element->getAttribute('class') . ' table table-bordered'); - } - $this->post_data['content'] = $dom->saveHTML(); + $this->post_data['content'] = $parsedown->text($this->post_data['content_md']); if (preg_match('/^.*.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) { $content_less = substr($this->post_data['content'], 0, $matches[0][1]); diff --git a/web/app/models/UOJMarkdown.php b/web/app/models/UOJMarkdown.php new file mode 100644 index 0000000..b813f66 --- /dev/null +++ b/web/app/models/UOJMarkdown.php @@ -0,0 +1,78 @@ +InlineTypes['@'][] = 'UserMention'; + $this->inlineMarkerList .= '@'; + } + + // https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L340-L345 + protected function doGetAttributes($Element) { + if (isset($Element['attributes'])) { + return (array) $Element['attributes']; + } + return array(); + } + + // https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L347-L358 + protected function doGetContent($Element) { + if (isset($Element['text'])) { + return $Element['text']; + } + if (isset($Element['rawHtml'])) { + return $Element['rawHtml']; + } + if (isset($Element['handler']['argument'])) { + return implode("\n", (array) $Element['handler']['argument']); + } + return null; + } + + // https://github.com/taufik-nurrohman/parsedown-extra-plugin/blob/1653418c5a9cf5277cd28b0b23ba2d95d18e9bc4/ParsedownExtraPlugin.php#L369-L378 + protected function doSetAttributes(&$Element, $From, $Args = array()) { + $Attributes = $this->doGetAttributes($Element); + $Content = $this->doGetContent($Element); + if (is_callable($From)) { + $Args = array_merge(array($Content, $Attributes, &$Element), $Args); + $Element['attributes'] = array_replace($Attributes, (array) call_user_func_array($From, $Args)); + } else { + $Element['attributes'] = array_replace($Attributes, (array) $From); + } + } + + // Add classes to + protected function blockTableComplete($Block) { + $this->doSetAttributes($Block['element'], ['class' => 'table table-bordered']); + + return $Block; + } + + // https://gist.github.com/ShNURoK42/b5ce8baa570975db487c + protected function inlineUserMention($Excerpt) { + if (preg_match('/^@([^\s]+)/', $Excerpt['text'], $matches)) { + if (validateUsername($matches[1]) && ($user = queryUser($matches[1]))) { + return [ + 'extent' => strlen($matches[0]), + 'element' => [ + 'name' => 'a', + 'text' => '@' . $user['username'], + 'attributes' => [ + 'href' => '/user/' . $user['username'], + 'class' => 'uoj-username', + 'data-realname' => $user['realname'], + ], + ], + ]; + } + + return [ + 'extent' => strlen($matches[0]), + 'markup' => $matches[0], + ]; + } + } +}