New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow excluding certain elements #832
Comments
Hi! Great question! This is fairly easy to do in the upcoming 2.0.0 release (you'll find a beta release available at the moment if you'd like to try or give feedback). Removing a default blockTo remove the header block rule entirely, you can do the following: use Erusev\Parsedown\Components\Blocks\Header;
use Erusev\Parsedown\Configurables\BlockTypes;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\State;
$State = new State;
$BlocksTypes = $State->get(BlockTypes::class);
$BlocksTypes = $BlocksTypes->removing([Header::class]);
$State = $State->setting($BlocksTypes);
$Parsedown = new Parsedown($State);
echo $Parsedown->toHtml('### Testing! ###'); Which would result in this output: <p>### Testing! ###</p> Replacing a default block with a custom blockIf you wanted to replace all the headers with bold text, this is possible too. What you'd need to do, is write a custom block for the <?php
require 'vendor/autoload.php';
use Erusev\Parsedown\AST\Handler;
use Erusev\Parsedown\Components\Block;
use Erusev\Parsedown\Components\Blocks\Header;
use Erusev\Parsedown\Configurables\BlockTypes;
use Erusev\Parsedown\Html\Renderables\Element;
use Erusev\Parsedown\Parsedown;
use Erusev\Parsedown\Parsing\Context;
use Erusev\Parsedown\State;
final class BoldHeader implements Block
{
/** @var Header */
private $Header;
private function __construct(Header $Header)
{
$this->Header = $Header;
}
/**
* @return static|null
*/
public static function build(
Context $Context,
State $State,
Block $Block = null
) {
$Header = Header::build($Context, $State, $Block);
if (!isset($Header)) {
return null;
}
return new self($Header);
}
/**
* @return Handler<Element>
*/
public function stateRenderable()
{
return new Handler(
/** @return Element */
function (State $State) {
return new Element(
'b',
[],
$State->applyTo(Parsedown::line($this->Header->text(), $State))
);
}
);
}
}
$State = new State;
$BlocksTypes = $State->get(BlockTypes::class);
$BlocksTypes = $BlocksTypes->replacing(Header::class, BoldHeader::class);
$State = $State->setting($BlocksTypes);
$Parsedown = new Parsedown($State);
echo $Parsedown->toHtml('### Testing! ###'); Which outputs: <b>Testing!</b> Walkthrough of the custom blockWalking through the above example: final class BoldHeader implements Block
{ This defines a new
For 1. this is easy enough, just defer to the existing /** @var Header */
private $Header;
private function __construct(Header $Header)
{
$this->Header = $Header;
}
/**
* @return static|null
*/
public static function build(
Context $Context,
State $State,
Block $Block = null
) {
$Header = Header::build($Context, $State, $Block);
if (!isset($Header)) {
return null;
}
return new self($Header);
} For 2. we need to return whatever text the stored /**
* @return Handler<Element>
*/
public function stateRenderable()
{
return new Handler(
/** @return Element */
function (State $State) {
return new Element(
'b',
[],
$State->applyTo(Parsedown::line($this->Header->text(), $State))
);
}
);
} Note that the contents of the bold element is /**
* @return Element
*/
public function stateRenderable()
{
return new Element(
'b',
[],
[new Text($this->Header->text())]
);
} The final lines do the following:
|
Also bear in mind that there is a second type of header called Testing
==== as a header too. The instructions for dealing with this are essentially the same as above, and you could just opt to remove it if you didn't want to display these in bold. |
Hello everybody, |
I haven't found a way at the moment to do this.
What i would like is to be able to pass a rule to the class to not render headers.
I can take them out of the raw string, but if there was a way to apply a setting to not render the ## for example as a header, that would be more efficient.
Also, if there was a way to replace the headers with just bold text, that would be even better.
The text was updated successfully, but these errors were encountered: