daux.io/libs/ContentTypes/Markdown/TableOfContentsParser.php

29 lignes
746 B
PHP
Brut Vue normale Historique

<?php namespace Todaymade\Daux\ContentTypes\Markdown;
2016-04-12 08:38:52 +02:00
2019-11-28 22:36:26 +01:00
use League\CommonMark\Block\Parser\BlockParserInterface;
2016-04-12 08:38:52 +02:00
use League\CommonMark\ContextInterface;
use League\CommonMark\Cursor;
2019-11-28 22:36:26 +01:00
class TableOfContentsParser implements BlockParserInterface
2016-04-12 08:38:52 +02:00
{
2019-11-28 22:36:26 +01:00
public function parse(ContextInterface $context, Cursor $cursor): bool
2016-04-12 08:38:52 +02:00
{
if ($cursor->isIndented()) {
return false;
}
$previousState = $cursor->saveState();
2018-09-26 21:38:07 +02:00
$cursor->advanceToNextNonSpaceOrNewline();
2016-04-12 08:38:52 +02:00
$fence = $cursor->match('/^\[TOC\]/');
if (is_null($fence)) {
$cursor->restoreState($previousState);
return false;
}
$context->addBlock(new TableOfContents());
2016-04-12 08:38:52 +02:00
return true;
}
}