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)
|
[![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)
|
customized [Mni/FrontYaml](https://github.com/mnapoli/frontyaml)
|
||||||
to use parsedown and a small collection of extensions
|
to add a consistent collection of commonmark extensions used across several tildeverse sites to power the wiki.
|
||||||
used across several tildeverse sites to power the wiki.
|
|
||||||
|
|
||||||
## basic usage
|
## basic usage
|
||||||
|
|
||||||
1. add tildewiki
|
1. add tildewiki
|
||||||
|
|
||||||
```php
|
```php
|
||||||
composer require tildeteam/wiki
|
composer require tildeteam/wiki
|
||||||
```
|
```
|
||||||
|
|
||||||
1. get an instance
|
1. get an instance
|
||||||
|
|
||||||
```php
|
```php
|
||||||
require_once __DIR__.'/vendor/autoload.php';
|
require_once __DIR__.'/vendor/autoload.php';
|
||||||
|
// pass true to the constructor to automatically add bootstrap classes to tables
|
||||||
$parser = tildeteam\wiki::factory();
|
$parser = tildeteam\wiki::factory();
|
||||||
```
|
```
|
||||||
|
|
||||||
1. set options
|
|
||||||
```php
|
|
||||||
tildeteam\wiki::$bootstrap = true;
|
|
||||||
tildeteam\wiki::$forkawesome = true;
|
|
||||||
```
|
|
||||||
|
|
||||||
1. parse stuff
|
1. parse stuff
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$parsed = $parser->parse(file_get_contents("my.md"));
|
$parsed = $parser->parse(file_get_contents("my.md"));
|
||||||
echo $parsed->getContent();
|
echo $parsed->getContent();
|
||||||
```
|
```
|
||||||
|
|
||||||
1. get yaml frontmatter values
|
1. get yaml frontmatter values
|
||||||
|
|
||||||
```php
|
```php
|
||||||
echo $parsed->getYAML()["my_yaml_key"];
|
echo $parsed->getYAML()["my_yaml_key"];
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "tildeteam/wiki",
|
"name": "tildeteam/wiki",
|
||||||
"description": "parsedown extensions for tilde wikis",
|
"description": "unified extensions for tilde wikis",
|
||||||
"keywords": ["markdown", "wiki"],
|
"keywords": ["markdown", "wiki"],
|
||||||
"homepage": "https://tildegit.org/team/tildewiki",
|
"homepage": "https://tildegit.org/team/tildewiki",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"require": {
|
"require": {
|
||||||
"erusev/parsedown-extra": "^0.8.1",
|
"mnapoli/front-yaml": "^2.0.0"
|
||||||
"mnapoli/front-yaml": "^1.8.0"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
1121
composer.lock
generated
1121
composer.lock
generated
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;
|
namespace tildeteam;
|
||||||
|
|
||||||
|
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension;
|
||||||
use Mni\FrontYAML;
|
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 {
|
class wiki implements FrontYAML\Markdown\MarkdownParser
|
||||||
public static $bootstrap = false;
|
{
|
||||||
public static $forkawesome = false;
|
private MarkdownConverter $parser;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct(bool $bootstrap)
|
||||||
$this->mdparser = new tildeparsedown();
|
{
|
||||||
|
$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) {
|
public function parse(string $markdown): string
|
||||||
$this->mdparser->bootstrap = self::$bootstrap;
|
{
|
||||||
$this->mdparser->forkawesome = self::$forkawesome;
|
return $this->parser->convert($markdown)->getContent();
|
||||||
|
|
||||||
return $this->mdparser->text($markdown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function factory() {
|
public static function factory(bool $bootstrap = false): FrontYAML\Parser
|
||||||
self::$bootstrap = false;
|
{
|
||||||
self::$forkawesome = false;
|
return new FrontYAML\Parser(null, new wiki($bootstrap));
|
||||||
|
|
||||||
return new FrontYAML\Parser(null, new wiki());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,28 @@
|
||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use tildeteam\wiki;
|
use tildeteam\wiki;
|
||||||
|
|
||||||
final class WikiTest extends TestCase {
|
final class WikiTest extends TestCase
|
||||||
public function testHeaderIds(): void {
|
{
|
||||||
|
public function testHeaderIds(): void
|
||||||
|
{
|
||||||
$parser = wiki::factory();
|
$parser = wiki::factory();
|
||||||
$header = '# hello there';
|
$header = '# hello there';
|
||||||
$this->assertEquals(
|
$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()
|
$parser->parse($header)->getContent()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHeaderIdsWithBootstrap(): void {
|
public function testTableClasses(): 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 {
|
|
||||||
$parser = wiki::factory();
|
$parser = wiki::factory();
|
||||||
$table = '| tilde name | description | where to join | notes |
|
$table = '| tilde name | description | where to join | notes |
|
||||||
| --- | --- | --- | ---|
|
| --- | --- | --- | ---|
|
||||||
|
@ -47,19 +45,20 @@ final class WikiTest extends TestCase {
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>',
|
</table>
|
||||||
|
',
|
||||||
$parser->parse($table)->getContent()
|
$parser->parse($table)->getContent()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTableClassesWithBootstrap(): void {
|
public function testTableClassesWithBootstrap(): void
|
||||||
$parser = wiki::factory();
|
{
|
||||||
wiki::$bootstrap = true;
|
$parser = wiki::factory(true);
|
||||||
$table = '| tilde name | description | where to join | notes |
|
$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/) | |';
|
| [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(
|
$this->assertEquals(
|
||||||
'<table class="table table-striped">
|
'<div class="table table-striped"><table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>tilde name</th>
|
<th>tilde name</th>
|
||||||
|
@ -76,7 +75,9 @@ final class WikiTest extends TestCase {
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>',
|
</table></div>
|
||||||
|
'
|
||||||
|
,
|
||||||
$parser->parse($table)->getContent()
|
$parser->parse($table)->getContent()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user