1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Require Inlines to provide a best plaintext rendering

This allows markdown to be parsed "inside" the alt
attribute of an image, and then the best plaintext
can be used as the rest.
This improves CommonMark compliance.
This commit is contained in:
Aidan Woods 2019-01-22 19:06:26 +00:00
parent 576a2c4519
commit 82c981657d
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
13 changed files with 117 additions and 1 deletions

View File

@ -3,6 +3,7 @@
namespace Erusev\Parsedown\Components;
use Erusev\Parsedown\Component;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
@ -28,4 +29,9 @@ interface Inline extends Component
* @return int|null
* */
public function modifyStartPositionTo();
/**
* @return Text
*/
public function bestPlaintext();
}

View File

@ -58,4 +58,12 @@ final class Code implements Inline
{
return new Element('code', [], [new Text($this->text)]);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->text);
}
}

View File

@ -64,4 +64,12 @@ final class Email implements Inline
{
return new Element('a', ['href' => $this->url], [new Text($this->text)]);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->text);
}
}

View File

@ -85,4 +85,12 @@ final class Emphasis implements Inline
}
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->text);
}
}

View File

@ -5,6 +5,7 @@ namespace Erusev\Parsedown\Components\Inlines;
use Erusev\Parsedown\AST\StateRenderable;
use Erusev\Parsedown\Components\Inline;
use Erusev\Parsedown\Html\Renderables\RawHtml;
use Erusev\Parsedown\Html\Renderables\Text;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Excerpt;
use Erusev\Parsedown\State;
@ -48,4 +49,12 @@ final class EscapeSequence implements Inline
{
return new RawHtml($this->html);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->html);
}
}

View File

@ -61,7 +61,20 @@ final class Image implements Inline
function (State $State) use ($Parsedown) {
$attributes = [
'src' => $this->Link->url(),
'alt' => $this->Link->label(),
'alt' => \array_reduce(
$Parsedown->inlines($this->Link->label()),
/**
* @param string $text
* @return string
*/
function ($text, Inline $Inline) {
return (
$text
. $Inline->bestPlaintext()->getStringBacking()
);
},
''
),
];
$title = $this->Link->title();
@ -78,4 +91,12 @@ final class Image implements Inline
}
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->Link->label());
}
}

View File

@ -134,4 +134,12 @@ final class Link implements Inline
}
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->label);
}
}

View File

@ -74,4 +74,12 @@ final class Markup implements Inline
}
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->html);
}
}

View File

@ -72,4 +72,12 @@ final class PlainText implements Inline
}
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->text);
}
}

View File

@ -51,4 +51,12 @@ final class SpecialCharacter implements Inline
'&' . (new Text($this->charCodeHtml))->getHtml() . ';'
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text('&'.$this->charCodeHtml.';');
}
}

View File

@ -64,4 +64,12 @@ final class Strikethrough implements Inline
}
);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->text);
}
}

View File

@ -71,4 +71,12 @@ final class Url implements Inline
{
return new Element('a', ['href' => $this->url], [new Text($this->url)]);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->url);
}
}

View File

@ -48,4 +48,12 @@ final class UrlTag implements Inline
{
return new Element('a', ['href' => $this->url], [new Text($this->url)]);
}
/**
* @return Text
*/
public function bestPlaintext()
{
return new Text($this->url);
}
}