Finalize table of contents feature, also for confluence upload
This commit is contained in:
parent
fb7343c109
commit
d3f8d19f88
@ -1,7 +1,15 @@
|
||||
Adding a table of contents becomes very easy with Daux.io
|
||||
|
||||
## Manual
|
||||
|
||||
Add `[TOC]` anywhere in your document and it will be replaced by a table of contents.
|
||||
|
||||
You can add it more than once in a page.
|
||||
|
||||
## Automatic
|
||||
|
||||
> Works only for html mode
|
||||
|
||||
A table of contents can be added automatically to all pages.
|
||||
|
||||
If `[TOC]` isn't present it will add it at the beginning of the page.
|
||||
@ -10,10 +18,10 @@ You can enable this feature in your configuration
|
||||
|
||||
```json
|
||||
{
|
||||
"html": {
|
||||
"auto_toc": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Manual
|
||||
|
||||
Add `[TOC]` anywhere in your document and it will be replaced by a table of contents
|
||||
|
@ -18,8 +18,6 @@
|
||||
|
||||
"timezone": "America/Los_Angeles",
|
||||
|
||||
"auto_toc": false,
|
||||
|
||||
"live": {
|
||||
"inherit_index": false,
|
||||
"clean_urls": false
|
||||
@ -33,6 +31,7 @@
|
||||
"float": false,
|
||||
"auto_landing": true,
|
||||
"search": true,
|
||||
"auto_toc": false,
|
||||
|
||||
"repo": "",
|
||||
"twitter": [],
|
||||
|
@ -3,7 +3,9 @@
|
||||
use League\CommonMark\DocParser;
|
||||
use League\CommonMark\Environment;
|
||||
use League\CommonMark\HtmlRenderer;
|
||||
use Todaymade\Daux\Config;
|
||||
use Todaymade\Daux\ContentTypes\Markdown\TOC\Parser;
|
||||
use Todaymade\Daux\ContentTypes\Markdown\TOC\Renderer;
|
||||
use Todaymade\Daux\ContentTypes\Markdown\TOC\TOCProcessor;
|
||||
use Webuni\CommonMark\TableExtension\TableExtension;
|
||||
|
||||
@ -21,10 +23,9 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
|
||||
$environment->addExtension(new TableExtension());
|
||||
|
||||
// Table of Contents
|
||||
$environment->addBlockParser(new Parser());
|
||||
$environment->addDocumentProcessor(new TOCProcessor($config['daux']));
|
||||
$environment->addBlockParser(new TableOfContentsParser());
|
||||
|
||||
$this->extendEnvironment($environment);
|
||||
$this->extendEnvironment($environment, $config['daux']);
|
||||
|
||||
if (array_key_exists('processor_instance', $config['daux'])) {
|
||||
$config['daux']['processor_instance']->extendCommonMarkEnvironment($environment);
|
||||
@ -39,7 +40,7 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
|
||||
return new LinkRenderer($environment->getConfig('daux'));
|
||||
}
|
||||
|
||||
protected function extendEnvironment(Environment $environment)
|
||||
protected function extendEnvironment(Environment $environment, Config $config)
|
||||
{
|
||||
$environment->addInlineRenderer('Link', $this->getLinkRenderer($environment));
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
|
||||
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Cursor;
|
||||
|
||||
class Element extends AbstractBlock
|
||||
class TableOfContents extends AbstractBlock
|
||||
{
|
||||
|
||||
/**
|
@ -1,19 +1,10 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: onigoetz
|
||||
* Date: 09/04/16
|
||||
* Time: 23:03
|
||||
*/
|
||||
|
||||
namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
|
||||
|
||||
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Block\Parser\AbstractBlockParser;
|
||||
use League\CommonMark\ContextInterface;
|
||||
use League\CommonMark\Cursor;
|
||||
|
||||
class Parser extends AbstractBlockParser
|
||||
class TableOfContentsParser extends AbstractBlockParser
|
||||
{
|
||||
|
||||
/**
|
||||
@ -37,7 +28,7 @@ class Parser extends AbstractBlockParser
|
||||
return false;
|
||||
}
|
||||
|
||||
$context->addBlock(new Element());
|
||||
$context->addBlock(new TableOfContents());
|
||||
|
||||
return true;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Environment;
|
||||
use Todaymade\Daux\Config;
|
||||
|
||||
class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMarkConverter
|
||||
{
|
||||
@ -9,9 +10,11 @@ class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMa
|
||||
return new LinkRenderer($environment->getConfig('daux'));
|
||||
}
|
||||
|
||||
protected function extendEnvironment(Environment $environment)
|
||||
protected function extendEnvironment(Environment $environment, Config $config)
|
||||
{
|
||||
parent::extendEnvironment($environment);
|
||||
parent::extendEnvironment($environment, $config);
|
||||
|
||||
$environment->addBlockRenderer('Todaymade\Daux\ContentTypes\Markdown\TableOfContents', new TOCRenderer());
|
||||
|
||||
//Add code renderer
|
||||
$environment->addBlockRenderer('FencedCode', new FencedCodeRenderer());
|
||||
|
13
libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php
Normal file
13
libs/Format/Confluence/ContentTypes/Markdown/TOCRenderer.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Block\Renderer\BlockRendererInterface;
|
||||
use League\CommonMark\ElementRendererInterface;
|
||||
|
||||
class TOCRenderer implements BlockRendererInterface
|
||||
{
|
||||
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
|
||||
{
|
||||
return '<ac:structured-macro ac:name="toc"></ac:structured-macro>';
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown;
|
||||
|
||||
use League\CommonMark\Environment;
|
||||
use Todaymade\Daux\Config;
|
||||
|
||||
class CommonMarkConverter extends \Todaymade\Daux\ContentTypes\Markdown\CommonMarkConverter
|
||||
{
|
||||
protected function extendEnvironment(Environment $environment, Config $config)
|
||||
{
|
||||
parent::extendEnvironment($environment, $config);
|
||||
|
||||
$environment->addDocumentProcessor(new TOC\Processor($config));
|
||||
$environment->addBlockRenderer('Todaymade\Daux\ContentTypes\Markdown\TableOfContents', new TOC\Renderer());
|
||||
}
|
||||
}
|
12
libs/Format/HTML/ContentTypes/Markdown/ContentType.php
Normal file
12
libs/Format/HTML/ContentTypes/Markdown/ContentType.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown;
|
||||
|
||||
use Todaymade\Daux\Config;
|
||||
|
||||
class ContentType extends \Todaymade\Daux\ContentTypes\Markdown\ContentType
|
||||
{
|
||||
public function __construct(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->converter = new CommonMarkConverter(['daux' => $config]);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
|
||||
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
|
||||
|
||||
use League\CommonMark\Block\Element\Heading;
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
|
||||
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
|
||||
|
||||
use DeepCopy\DeepCopy;
|
||||
use League\CommonMark\Block\Element\Document;
|
||||
@ -13,9 +13,10 @@ 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;
|
||||
|
||||
class TOCProcessor implements DocumentProcessorInterface
|
||||
class Processor implements DocumentProcessorInterface
|
||||
{
|
||||
protected $config;
|
||||
|
||||
@ -26,7 +27,7 @@ class TOCProcessor implements DocumentProcessorInterface
|
||||
|
||||
public function hasAutoTOC()
|
||||
{
|
||||
return array_key_exists('auto_toc', $this->config) && $this->config['auto_toc'];
|
||||
return array_key_exists('html', $this->config) && array_key_exists('auto_toc', $this->config['html']) && $this->config['html']['auto_toc'];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,7 +37,7 @@ class TOCProcessor implements DocumentProcessorInterface
|
||||
*/
|
||||
public function processDocument(Document $document)
|
||||
{
|
||||
/** @var Element[] $tocs */
|
||||
/** @var TableOfContents[] $tocs */
|
||||
$tocs = [];
|
||||
|
||||
$headings = [];
|
||||
@ -45,7 +46,7 @@ class TOCProcessor implements DocumentProcessorInterface
|
||||
while ($event = $walker->next()) {
|
||||
$node = $event->getNode();
|
||||
|
||||
if ($node instanceof Element && !$event->isEntering()) {
|
||||
if ($node instanceof TableOfContents && !$event->isEntering()) {
|
||||
$tocs[] = $node;
|
||||
continue;
|
||||
}
|
||||
@ -64,7 +65,7 @@ class TOCProcessor implements DocumentProcessorInterface
|
||||
|
||||
if (count($tocs)) {
|
||||
foreach ($tocs as $toc) {
|
||||
$toc->replaceWith($this->render($generated->getChildren()));
|
||||
$toc->appendChild($this->render($generated->getChildren()));
|
||||
}
|
||||
} else {
|
||||
$document->prependChild($this->render($generated->getChildren()));
|
13
libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php
Normal file
13
libs/Format/HTML/ContentTypes/Markdown/TOC/Renderer.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
|
||||
|
||||
use League\CommonMark\Block\Element\AbstractBlock;
|
||||
use League\CommonMark\Block\Renderer\BlockRendererInterface;
|
||||
use League\CommonMark\ElementRendererInterface;
|
||||
|
||||
class Renderer implements BlockRendererInterface
|
||||
{
|
||||
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false)
|
||||
{
|
||||
return $htmlRenderer->renderBlocks($block->children());
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
<?php namespace Todaymade\Daux\ContentTypes\Markdown\TOC;
|
||||
<?php namespace Todaymade\Daux\Format\HTML\ContentTypes\Markdown\TOC;
|
||||
|
||||
class RootEntry extends Entry
|
||||
{
|
Loading…
Reference in New Issue
Block a user