Show markdown on pages.

This commit is contained in:
Jan Böhmer 2019-10-11 23:53:12 +02:00
parent 46959b74ed
commit 7ec406d4a1
9 changed files with 462 additions and 275 deletions

View file

@ -154,7 +154,7 @@ showing the sidebar (on devices with md or higher)
@media (max-width: 576px) {
#sidebar-toggle-button {
display: none;
display: none;
}
}
@ -739,4 +739,59 @@ table.dataTable {
.tt-suggestion p {
margin: 0;
}
}
/***************************************************
* Markdown styles
***************************************************/
.markdown code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
border-radius: 4px;
}
.markdown pre {
display: block;
padding: 9.5px;
margin: 0 0 10px;
font-size: 13px;
line-height: 1.42857143;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 4px;
}
.markdown table {
border-width: 1px 0 0 1px;
border-color: #bbb;
border-style: solid;
width: 100%;
margin-bottom: 1rem;
color: #212529;
}
.markdown table td, .markdown table th {
border-width: 0 1px 1px 0;
border-color: #bbb;
border-style: solid;
padding: 10px;
}
.markdown table tbody tr:hover {
color: #212529;
background-color: rgba(0, 0, 0, 0.075);
}
.markdown table tbody tr:nth-of-type(odd) {
background-color: rgba(0, 0, 0, 0.05);
}
.markdown img {
max-width: 35%;
vertical-align: middle;
}

View file

@ -5,12 +5,13 @@
"php": "^7.1.3",
"ext-bcmath": "*",
"ext-ctype": "*",
"ext-gd": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-gd": "*",
"doctrine/annotations": "^1.6",
"erusev/parsedown": "^1.8-dev",
"florianv/swap": "^4.0",
"friendsofsymfony/ckeditor-bundle": "^2.0",
"liip/imagine-bundle": "^2.2",

536
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -68,8 +68,9 @@
//This is useful, to convert unsupported HTML feauters to plain text and adds an basic XSS protection
//The HTML is inside an iframe so an XSS attack can not do much harm.
data = stripHtml(data);
return converter.makeHtml(data);
}
return tmp = converter.makeHtml(data);
},
};
}
@ -80,8 +81,17 @@
CKEDITOR.addCss(
//Show borders on tables generated by Showdown
'table, th, td {\n' +
' border: 1px solid black;\n' +
'table {\n' +
' border-width: 1px 0 0 1px;\n' +
' border-color: #bbb;\n' +
' border-style: solid;\n' +
' }\n' +
'\n' +
'table td, table th {\n' +
' border-width: 0 1px 1px 0;\n' +
' border-color: #bbb;\n' +
' border-style: solid;\n' +
' padding: 10px;\n' +
'}' +
//Show code blocks
'pre {\n' +

View file

@ -0,0 +1,87 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Services;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
/**
* This class allows you to convert markdown text to HTML.
* @package App\Services
*/
class MarkdownParser
{
protected $cache;
/** @var \Parsedown */
protected $parsedown;
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
$this->initParsedown();
}
protected function initParsedown()
{
$this->parsedown = new \Parsedown();
$this->parsedown->setSafeMode(true);
}
/**
* Converts the given markdown text to HTML.
* The result is cached.
* @param string $markdown The markdown text that should be parsed to html.
* @param bool $inline_mode Only allow inline markdown codes like (*bold* or **italic**), not something like tables
* @return string The HTML version of the given text.
* @throws \Psr\Cache\InvalidArgumentException
*/
public function parse(string $markdown, bool $inline_mode = false) : string
{
//Generate key
if ($inline_mode) {
$key = 'markdown_i_' . md5($markdown);
} else {
$key = 'markdown_' . md5($markdown);
}
return $this->cache->get($key, function (ItemInterface $item) use ($markdown, $inline_mode) {
//Expire text after 2 months
$item->expiresAfter(311040000);
if ($inline_mode) {
return $this->parsedown->line($markdown);
}
return '<div class="markdown">' . $this->parsedown->text($markdown) . '</div>';
});
}
}

View file

@ -36,12 +36,13 @@ use App\Entity\PriceInformations\Currency;
use App\Services\AmountFormatter;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\EntityURLGenerator;
use App\Services\MarkdownParser;
use App\Services\MoneyFormatter;
use App\Services\SIFormatter;
use App\Services\TreeBuilder;
use Money\Currencies;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Cache\CacheInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
@ -51,7 +52,7 @@ use Twig\TwigTest;
class AppExtension extends AbstractExtension
{
protected $entityURLGenerator;
protected $cache;
protected $markdownParser;
protected $serializer;
protected $treeBuilder;
protected $moneyFormatter;
@ -59,14 +60,14 @@ class AppExtension extends AbstractExtension
protected $amountFormatter;
protected $attachmentURLGenerator;
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
public function __construct(EntityURLGenerator $entityURLGenerator, MarkdownParser $markdownParser,
SerializerInterface $serializer, TreeBuilder $treeBuilder,
MoneyFormatter $moneyFormatter,
SIFormatter $SIFormatter, AmountFormatter $amountFormatter,
AttachmentURLGenerator $attachmentURLGenerator)
{
$this->entityURLGenerator = $entityURLGenerator;
$this->cache = $cache;
$this->markdownParser = $markdownParser;
$this->serializer = $serializer;
$this->treeBuilder = $treeBuilder;
$this->moneyFormatter = $moneyFormatter;
@ -79,7 +80,7 @@ class AppExtension extends AbstractExtension
{
return [
new TwigFilter('entityURL', [$this, 'generateEntityURL']),
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
new TwigFilter('markdown', [$this->markdownParser, 'parse'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
new TwigFilter('siFormat', [$this, 'siFormat']),
new TwigFilter('amountFormat', [$this, 'amountFormat']),
@ -128,22 +129,6 @@ class AppExtension extends AbstractExtension
return $this->entityURLGenerator->getURL($entity, $method);
}
public function parseBBCode(string $bbcode): string
{
if ('' === $bbcode) {
return '';
}
$item = $this->cache->getItem('bbcode_'.md5($bbcode));
if (!$item->isHit()) {
$xml = TextFormatter::parse($bbcode);
$item->set(TextFormatter::render($xml));
$this->cache->save($item);
}
return $item->get();
}
public function formatCurrency($amount, Currency $currency = null, int $decimals = 5)
{
return $this->moneyFormatter->format($amount, $currency, $decimals);

View file

@ -105,6 +105,9 @@
"egulias/email-validator": {
"version": "2.1.7"
},
"erusev/parsedown": {
"version": "1.7.3"
},
"felixfbecker/advanced-json-rpc": {
"version": "v3.0.4"
},

View file

@ -27,7 +27,7 @@
<a href="{{ part|entityURL('edit') }}"><i class="fas fa-fw fa-sm fa-edit"></i></a>
{% endif %}
</h3>
<h6 class="text-muted"><span title="{% trans %}description.label{% endtrans %}">{{ part.description|bbCode }}</span></h6>
<h6 class="text-muted"><span title="{% trans %}description.label{% endtrans %}">{{ part.description|markdown(true) }}</span></h6>
<h6 class="">
<i class="fas fa-tag fa-fw" title="{% trans %}category.label{% endtrans %}"></i>
<span class="text-muted">{{ helper.structural_entity_link(part.category) }}</span>

View file

@ -84,7 +84,7 @@
{% if part.comment is not empty %}
<div class="tab-pane fade show" id="comment" role="tabpanel" aria-labelledby="home-tab">
<div class="container-fluid mt-2">
{{ part.comment|bbCode }}
{{ part.comment|markdown }}
</div>
</div>
{% endif %}