switch parsedown to commonmark
mnapoli/frontyaml has switched to commonmark so we can use their builtin extensions to achieve the same thing we did with our custom extensions previously. this still keeps it bundled up nicely.
This commit is contained in:
parent
519497aa9e
commit
adc22f05e1
15
README.md
15
README.md
|
@ -3,36 +3,33 @@
|
|||
[![Build Status](https://drone.tildegit.org/api/badges/team/tildewiki/status.svg)](https://drone.tildegit.org/team/tildewiki)
|
||||
|
||||
customized [Mni/FrontYaml](https://github.com/mnapoli/frontyaml)
|
||||
to use parsedown and a small collection of extensions
|
||||
used across several tildeverse sites to power the wiki.
|
||||
to add a consistent collection of commonmark extensions used across several tildeverse sites to power the wiki.
|
||||
|
||||
## basic usage
|
||||
|
||||
1. add tildewiki
|
||||
|
||||
```php
|
||||
composer require tildeteam/wiki
|
||||
```
|
||||
|
||||
1. get an instance
|
||||
|
||||
```php
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
// pass true to the constructor to automatically add bootstrap classes to tables
|
||||
$parser = tildeteam\wiki::factory();
|
||||
```
|
||||
|
||||
1. set options
|
||||
```php
|
||||
tildeteam\wiki::$bootstrap = true;
|
||||
tildeteam\wiki::$forkawesome = true;
|
||||
```
|
||||
|
||||
1. parse stuff
|
||||
|
||||
```php
|
||||
$parsed = $parser->parse(file_get_contents("my.md"));
|
||||
echo $parsed->getContent();
|
||||
```
|
||||
|
||||
1. get yaml frontmatter values
|
||||
|
||||
```php
|
||||
echo $parsed->getYAML()["my_yaml_key"];
|
||||
```
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
{
|
||||
"name": "tildeteam/wiki",
|
||||
"description": "parsedown extensions for tilde wikis",
|
||||
"description": "unified extensions for tilde wikis",
|
||||
"keywords": ["markdown", "wiki"],
|
||||
"homepage": "https://tildegit.org/team/tildewiki",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"erusev/parsedown-extra": "^0.8.1",
|
||||
"mnapoli/front-yaml": "^1.8.0"
|
||||
"mnapoli/front-yaml": "^2.0.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
namespace tildeteam;
|
||||
|
||||
class tildeparsedown extends \ParsedownExtra {
|
||||
public $bootstrap = false;
|
||||
public $forkawesome = false;
|
||||
|
||||
protected function blockHeader($line) {
|
||||
$header = parent::blockHeader($line);
|
||||
|
||||
if (!isset($header))
|
||||
return null;
|
||||
|
||||
$id = preg_replace('/[^a-z0-9]/', '-', strtolower($header['element']['text']));
|
||||
$header['element']['attributes']['id'] = $id;
|
||||
|
||||
// add anchor link to header text
|
||||
$text = '<small><a ';
|
||||
$text .= $this->bootstrap ?? false ? 'class="text-muted" ' : '';
|
||||
$text .= 'href="#' . $id . '">';
|
||||
$text .= $this->forkawesome ?? false ? '<i class="fa fa-link"></i>' : '🔗';
|
||||
$text .= '</a></small> ' . $header['element']['text'];
|
||||
|
||||
$header['element']['text'] = $text;
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
protected function blockTable($line, array $block = null) {
|
||||
$table = parent::blockTable($line, $block);
|
||||
|
||||
if (!isset($table))
|
||||
return null;
|
||||
|
||||
if ($this->bootstrap)
|
||||
$table['element']['attributes']['class'] = "table table-striped";
|
||||
|
||||
return $table;
|
||||
}
|
||||
}
|
||||
|
56
src/wiki.php
56
src/wiki.php
|
@ -1,27 +1,51 @@
|
|||
<?php
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace tildeteam;
|
||||
|
||||
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
|
||||
use Mni\FrontYAML;
|
||||
use League\CommonMark\Environment\Environment;
|
||||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||||
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension;
|
||||
use League\CommonMark\Extension\Table\TableExtension;
|
||||
use League\CommonMark\MarkdownConverter;
|
||||
|
||||
class wiki implements FrontYAML\Markdown\MarkdownParser {
|
||||
public static $bootstrap = false;
|
||||
public static $forkawesome = false;
|
||||
class wiki implements FrontYAML\Markdown\MarkdownParser
|
||||
{
|
||||
private MarkdownConverter $parser;
|
||||
|
||||
public function __construct() {
|
||||
$this->mdparser = new tildeparsedown();
|
||||
public function __construct(bool $bootstrap)
|
||||
{
|
||||
$config = [
|
||||
'table' => [
|
||||
'wrap' => [
|
||||
'enabled' => $bootstrap,
|
||||
'tag' => 'div',
|
||||
'attributes' => ['class' => 'table table-striped']
|
||||
]
|
||||
],
|
||||
'heading_permalink' => [
|
||||
'id_prefix' => '',
|
||||
'fragment_prefix' => ''
|
||||
]
|
||||
];
|
||||
|
||||
$env = new Environment($config);
|
||||
$env->addExtension(new CommonMarkCoreExtension());
|
||||
$env->addExtension(new HeadingPermalinkExtension());
|
||||
$env->addExtension(new TableExtension());
|
||||
$env->addExtension(new TableOfContentsExtension());
|
||||
|
||||
$this->parser = new MarkdownConverter($env);
|
||||
}
|
||||
|
||||
public function parse($markdown) {
|
||||
$this->mdparser->bootstrap = self::$bootstrap;
|
||||
$this->mdparser->forkawesome = self::$forkawesome;
|
||||
|
||||
return $this->mdparser->text($markdown);
|
||||
public function parse(string $markdown): string
|
||||
{
|
||||
return $this->parser->convert($markdown)->getContent();
|
||||
}
|
||||
|
||||
public static function factory() {
|
||||
self::$bootstrap = false;
|
||||
self::$forkawesome = false;
|
||||
|
||||
return new FrontYAML\Parser(null, new wiki());
|
||||
public static function factory(bool $bootstrap = false): FrontYAML\Parser
|
||||
{
|
||||
return new FrontYAML\Parser(null, new wiki($bootstrap));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,28 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use tildeteam\wiki;
|
||||
|
||||
final class WikiTest extends TestCase {
|
||||
public function testHeaderIds(): void {
|
||||
final class WikiTest extends TestCase
|
||||
{
|
||||
public function testHeaderIds(): void
|
||||
{
|
||||
$parser = wiki::factory();
|
||||
$header = '# hello there';
|
||||
$this->assertEquals(
|
||||
'<h1 id="hello-there"><small><a href="#hello-there">🔗</a></small> hello there</h1>',
|
||||
'<ul class="table-of-contents">
|
||||
<li>
|
||||
<a href="#hello-there">hello there</a>
|
||||
</li>
|
||||
</ul>
|
||||
<h1><a id="hello-there" href="#hello-there" class="heading-permalink" aria-hidden="true" title="Permalink">¶</a>hello there</h1>
|
||||
',
|
||||
$parser->parse($header)->getContent()
|
||||
);
|
||||
}
|
||||
|
||||
public function testHeaderIdsWithBootstrap(): void {
|
||||
$parser = wiki::factory();
|
||||
wiki::$bootstrap = true;
|
||||
wiki::$forkawesome = true;
|
||||
$header = '# hello there';
|
||||
|
||||
$this->assertEquals(
|
||||
'<h1 id="hello-there"><small><a class="text-muted" href="#hello-there"><i class="fa fa-link"></i></a></small> hello there</h1>',
|
||||
$parser->parse($header)->getContent()
|
||||
);
|
||||
}
|
||||
|
||||
public function testTableClasses(): void {
|
||||
public function testTableClasses(): void
|
||||
{
|
||||
$parser = wiki::factory();
|
||||
$table = '| tilde name | description | where to join | notes |
|
||||
| --- | --- | --- | ---|
|
||||
|
@ -47,19 +45,20 @@ final class WikiTest extends TestCase {
|
|||
<td></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>',
|
||||
</table>
|
||||
',
|
||||
$parser->parse($table)->getContent()
|
||||
);
|
||||
}
|
||||
|
||||
public function testTableClassesWithBootstrap(): void {
|
||||
$parser = wiki::factory();
|
||||
wiki::$bootstrap = true;
|
||||
public function testTableClassesWithBootstrap(): void
|
||||
{
|
||||
$parser = wiki::factory(true);
|
||||
$table = '| tilde name | description | where to join | notes |
|
||||
| --- | --- | --- | ---|
|
||||
| [breadpunk.club](https://breadpunk.club) | breadpunk.club is a small tilde focused on bread-making and community-building. see the [manifesto](https://breadpunk.club/docs/manifesto/) for more information on our mission. | [signup information here](https://breadpunk.club/join/) | |';
|
||||
$this->assertEquals(
|
||||
'<table class="table table-striped">
|
||||
'<div class="table table-striped"><table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>tilde name</th>
|
||||
|
@ -76,7 +75,9 @@ final class WikiTest extends TestCase {
|
|||
<td></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>',
|
||||
</table></div>
|
||||
'
|
||||
,
|
||||
$parser->parse($table)->getContent()
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue