-
Notifications
You must be signed in to change notification settings - Fork 121
refactor(web): use parsedown as markdown renderer #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,19 +92,12 @@ private function receivePostData() { | |
| $this->post_data['is_hidden'] = isset($_POST["{$this->name}_is_hidden"]) ? 1 : 0; | ||
|
|
||
| $purifier = HTML::pruifier(); | ||
| $parsedown = HTML::parsedown(); | ||
|
|
||
| $this->post_data['title'] = HTML::escape($this->post_data['title']); | ||
|
|
||
| if ($this->type == 'blog') { | ||
| $content_md = $_POST[$this->name . '_content_md']; | ||
| try { | ||
| $v8 = new V8Js('POST'); | ||
| $v8->content_md = $this->post_data['content_md']; | ||
| $v8->executeString(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/js/marked.js'), 'marked.js'); | ||
| $this->post_data['content'] = $v8->executeString('marked(POST.content_md)'); | ||
| } catch (V8JsException $e) { | ||
| die(json_encode(array('content_md' => '未知错误'))); | ||
| } | ||
| $this->post_data['content'] = $parsedown->text($this->post_data['content_md']); | ||
|
|
||
| if (preg_match('/^.*<!--.*readmore.*-->.*$/m', $this->post_data['content'], $matches, PREG_OFFSET_CAPTURE)) { | ||
| $content_less = substr($this->post_data['content'], 0, $matches[0][1]); | ||
|
|
@@ -118,45 +111,20 @@ private function receivePostData() { | |
| if ($content_array === false || !is_array($content_array)) { | ||
| die(json_encode(array('content_md' => '不合法的 YAML 格式'))); | ||
| } | ||
|
|
||
| try { | ||
| $v8 = new V8Js('PHP'); | ||
| $v8->executeString(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/js/marked.js'), 'marked.js'); | ||
| $v8->executeString(<<<EOD | ||
| marked.setOptions({ | ||
| getLangClass: function(lang) { | ||
| lang = lang.toLowerCase(); | ||
| switch (lang) { | ||
| case 'c': return 'c'; | ||
| case 'c++': return 'cpp'; | ||
| case 'pascal': return 'pascal'; | ||
| default: return lang; | ||
| } | ||
| }, | ||
| getElementClass: function(tok) { | ||
| switch (tok.type) { | ||
| case 'list_item_start': | ||
| return 'fragment'; | ||
| case 'loose_item_start': | ||
| return 'fragment'; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| }) | ||
| EOD | ||
| ); | ||
| } catch (V8JsException $e) { | ||
| die(json_encode(array('content_md' => '未知错误'))); | ||
| } | ||
|
|
||
| $marked = function($md) use ($v8, $purifier) { | ||
| try { | ||
| $v8->md = $md; | ||
| return $purifier->purify($v8->executeString('marked(PHP.md)')); | ||
| } catch (V8JsException $e) { | ||
| die(json_encode(array('content_md' => '未知错误'))); | ||
|
|
||
| $marked = function($md) use ($parsedown, $purifier) { | ||
| $dom = new DOMDocument; | ||
| $dom->loadHTML(mb_convert_encoding($parsedown->text($md), 'HTML-ENTITIES', 'UTF-8')); | ||
| $elements = $dom->getElementsByTagName('li'); | ||
|
|
||
| foreach ($elements as $element) { | ||
| $element->setAttribute( | ||
| 'class', | ||
| $element->getAttribute('class') . ' fragment' | ||
| ); | ||
| } | ||
|
|
||
| return $purifier->purify($dom->saveHTML()); | ||
| }; | ||
|
Comment on lines
+115
to
128
|
||
|
|
||
| $config = array(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||
| <?php | ||||||
|
|
||||||
| require_once __DIR__ . '../vendor/parsedown/ParsedownMath.php'; | ||||||
|
||||||
| require_once __DIR__ . '../vendor/parsedown/ParsedownMath.php'; | |
| require_once __DIR__ . '/../vendor/parsedown/ParsedownMath.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will create a leading space in the class attribute if the element has no existing class, resulting in a class value like ' fragment'. Consider checking if the existing class is empty or trimming the result.