@@ -1446,3 +1446,94 @@ index 457a809..20a1f39 100755
14461446- -
144714472.7.4
14481448
1449+ From 007f00ba9fce5214028d999b29594fae45f3f425 Mon Sep 17 00:00:00 2001
1450+ From: =?UTF-8?q?St=C3=A9phane=20Goetz?= <
[email protected] >
1451+ Date: Thu, 18 May 2017 23:43:02 +0200
1452+ Subject: [PATCH] Fix TOC links that are escaped, also handle uniqueness, fixes
1453+ #461
1454+
1455+ ---
1456+ .../HTML/ContentTypes/Markdown/TOC/Processor.php | 38 +++++++++++++++++-----
1457+ 1 file changed, 29 insertions(+), 9 deletions(-)
1458+
1459+ diff --git a/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php b/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php
1460+ index bfe6a75..c7f3090 100644
1461+ --- a/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php
1462+ +++ b/libs/Format/HTML/ContentTypes/Markdown/TOC/Processor.php
1463+ @@ -42,6 +42,7 @@ class Processor implements DocumentProcessorInterface
1464+
1465+ $headings = [];
1466+
1467+ + $document->heading_ids = [];
1468+ $walker = $document->walker();
1469+ while ($event = $walker->next()) {
1470+ $node = $event->getNode();
1471+ @@ -55,9 +56,8 @@ class Processor implements DocumentProcessorInterface
1472+ continue;
1473+ }
1474+
1475+ - $id = $this->addId($node);
1476+ -
1477+ - $headings[] = new Entry($node, $id);
1478+ + $this->ensureHeadingHasId($document, $node);
1479+ + $headings[] = new Entry($node);
1480+ }
1481+
1482+ if (count($headings) && (count($tocs) || $this->hasAutoTOC())) {
1483+ @@ -74,18 +74,39 @@ class Processor implements DocumentProcessorInterface
1484+ }
1485+ }
1486+
1487+ - protected function addId(Heading $node)
1488+ + protected function getUniqueId(Document $document, $proposed) {
1489+ + if ($proposed == "page_") {
1490+ + $proposed = "page_section_" . (count($document->heading_ids) + 1);
1491+ + }
1492+ +
1493+ + // Quick path, it's a unique ID
1494+ + if (!in_array($proposed, $document->heading_ids)) {
1495+ + $document->heading_ids[] = $proposed;
1496+ + return $proposed;
1497+ + }
1498+ +
1499+ + $extension = 1; // Initialize the variable at one, so on the first iteration we have 2
1500+ + do {
1501+ + $extension++;
1502+ + } while (in_array("$proposed-$extension", $document->heading_ids));
1503+ +
1504+ + return "$proposed-$extension";
1505+ + }
1506+ +
1507+ + /**
1508+ + * @param Heading $node
1509+ + */
1510+ + protected function ensureHeadingHasId(Document $document, Heading $node)
1511+ {
1512+ - // If the node has an ID, no need to generate it
1513+ + // If the node has an ID, no need to generate it, just check it's unique
1514+ $attributes = $node->getData('attributes', []);
1515+ if (array_key_exists('id', $attributes) && !empty($attributes['id'])) {
1516+ - // TODO :: check for uniqueness
1517+ + $node->data['attributes']['id'] = $this->getUniqueId($document, $attributes['id']);
1518+
1519+ return $attributes['id'];
1520+ }
1521+
1522+ // Well, seems we have to generate an ID
1523+ -
1524+ $walker = $node->walker();
1525+ $inside = [];
1526+ while ($event = $walker->next()) {
1527+ @@ -107,8 +128,7 @@ class Processor implements DocumentProcessorInterface
1528+
1529+ $text = 'page_' . DauxHelper::slug(trim($text));
1530+
1531+ - // TODO :: check for uniqueness
1532+ - $node->data['attributes']['id'] = $text;
1533+ + $node->data['attributes']['id'] = $this->getUniqueId($document, $text);
1534+ }
1535+
1536+ /**
1537+ - -
1538+ 2.14.3
1539+
0 commit comments