Skip to content

Commit

Permalink
Squashed 'MarkdownParse/' changes from 764316e..3975312
Browse files Browse the repository at this point in the history
3975312 Fix typecho-fans#5 : Fix duplicate builds table of contents

git-subtree-dir: MarkdownParse
git-subtree-split: 3975312a00374ff8d4ffc8ff9e13ef871ffd002e
  • Loading branch information
chenyi committed Feb 18, 2019
1 parent 0e7393f commit 77aa359
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
10 changes: 7 additions & 3 deletions Parsedown.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function setStrictMode($strictMode)
'ftp://',
'ftps://',
'mailto:',
'tel:',
'data:image/png;base64,',
'data:image/gif;base64,',
'data:image/jpeg;base64,',
Expand Down Expand Up @@ -541,7 +542,7 @@ protected function blockHeader($Line)

$Block = array(
'element' => array(
'name' => 'h' . min(6, $level),
'name' => 'h' . $level,
'handler' => array(
'function' => 'lineElements',
'argument' => $text,
Expand Down Expand Up @@ -1119,6 +1120,9 @@ public function line($text, $nonNestables = array())

protected function lineElements($text, $nonNestables = array())
{
# standardize line breaks
$text = str_replace(array("\r\n", "\r"), "\n", $text);

$Elements = array();

$nonNestables = (empty($nonNestables)
Expand Down Expand Up @@ -1476,8 +1480,8 @@ protected function inlineMarkup($Excerpt)

protected function inlineSpecialCharacter($Excerpt)
{
if ($Excerpt['text'][1] !== ' ' and strpos($Excerpt['text'], ';') !== false
and preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches)
if (substr($Excerpt['text'], 1, 1) !== ' ' and strpos($Excerpt['text'], ';') !== false
and preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches)
) {
return array(
'element' => array('rawHtml' => '&' . $matches[1] . ';'),
Expand Down
18 changes: 13 additions & 5 deletions ParsedownExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

class ParsedownExtension extends Parsedown
{
protected $isTocEnabled = false;

protected $rawTocList = [];

protected $isTocEnabled = false;
protected $absoluteUrl = '';
protected $rawTocList = [];
protected $findTocSyntaxRule = '#^<p> *\[TOC\]\s*</p>$#m';

public function setTocEnabled($isTocEnable)
Expand All @@ -24,6 +23,13 @@ public function setTocSyntaxRule($findTocSyntaxRule)
return $this;
}

public function setAbsoluteUrl($absoluteUrl)
{
$this->absoluteUrl = $absoluteUrl;

return $this;
}

public function text($text)
{
$content = parent::text($text);
Expand All @@ -41,9 +47,11 @@ protected function buildToc()
$topHeadLevel = min(array_column($this->rawTocList, 'level'));

foreach ($this->rawTocList as $id => $tocItem) {
$tocMarkdownContent .= sprintf('%s- [%s](#%s)' . PHP_EOL, str_repeat(' ', $tocItem['level'] - $topHeadLevel), $this->line($tocItem['text']), $id);
$tocMarkdownContent .= sprintf('%s- [%s](%s#%s)' . PHP_EOL, str_repeat(' ', $tocItem['level'] - $topHeadLevel), $this->line($tocItem['text']), $this->absoluteUrl, $id);
}

$this->rawTocList = [];

return parent::text($tocMarkdownContent);
}

Expand Down
2 changes: 1 addition & 1 deletion Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @package MarkdownParse
* @author mrgeneral
* @version 1.1.0
* @version 1.1.1
* @link https://www.chengxiaobai.cn/php/markdown-parser-library.html
*/

Expand Down

0 comments on commit 77aa359

Please sign in to comment.