Skip to content

Commit

Permalink
Fix quotes in TOC fixes #240
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Dec 30, 2020
1 parent 0c8941b commit 0f03a24
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 103 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"guzzlehttp/guzzle": "~6.0 || ~7.0",
"league/commonmark": "^1.0.0",
"league/plates": "~3.1",
"myclabs/deep-copy": "^1.5",
"scrivo/highlight.php": "^9.15",
"symfony/console": "^4.4|^5.0",
"symfony/http-foundation": "^4.4|^5.0",
Expand Down
81 changes: 16 additions & 65 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 2 additions & 37 deletions libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;

use DeepCopy\DeepCopy;
use League\CommonMark\Block\Element\Document;
use League\CommonMark\Block\Element\Heading;
use League\CommonMark\Block\Element\ListBlock;
Expand All @@ -11,7 +10,6 @@
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Node\Node;
use ReflectionMethod;
use Todaymade\Daux\Config;
use Todaymade\Daux\ContentTypes\Markdown\TableOfContents;
use Todaymade\Daux\DauxHelper;
Expand Down Expand Up @@ -186,7 +184,8 @@ protected function render(array $entries)

$content = $entry->getContent();
if ($content != null) {
foreach ($this->cloneChildren($content) as $node) {
$cloned = clone $content;
foreach ($cloned->children() as $node) {
$a->appendChild($node);
}
}
Expand All @@ -206,39 +205,5 @@ protected function render(array $entries)
return $list;
}

/**
* Set the specified property to null on the object.
*
* @param Heading $object The object to modify
* @param string $property The property to nullify
*/
protected function setNull(Heading $object, $property)
{
$prop = new \ReflectionProperty(get_class($object), $property);
$prop->setAccessible(true);
$prop->setValue($object, null);
}

/**
* @return Node[]
*/
protected function cloneChildren(Heading $node)
{
$firstClone = clone $node;

// We have no choice but to hack into the
// system to reset the parent, previous and next
$this->setNull($firstClone, 'parent');
$this->setNull($firstClone, 'previous');
$this->setNull($firstClone, 'next');

// Also, the child elements need to know the next parents
foreach ($firstClone->children() as $subnode) {
$method = new ReflectionMethod(get_class($subnode), 'setParent');
$method->setAccessible(true);
$method->invoke($subnode, $firstClone);
}

return (new DeepCopy())->copy($firstClone)->children();
}
}
19 changes: 19 additions & 0 deletions tests/Format/HTML/TableOfContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ public function testEscapedTOC()
</ul>
<h1 id="page_TEST_Test">TEST : Test</h1>

EXPECTED;

$this->assertEquals($expected, $converter->convertToHtml($source));
}


public function testQuotesWorkCorrectly()
{
$converter = new CommonMarkConverter($this->getConfig());

$source = "[TOC]\n# Daux's bug";
$expected = <<<'EXPECTED'
<ul class="TableOfContents">
<li>
<p><a href="#page_Daux_s_bug">Daux’s bug</a></p>
</li>
</ul>
<h1 id="page_Daux_s_bug">Daux’s bug</h1>

EXPECTED;

$this->assertEquals($expected, $converter->convertToHtml($source));
Expand Down

0 comments on commit 0f03a24

Please sign in to comment.