Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ function render(string $markdown): string {

return $html;
}

// or simply

function render(string $markdown): string {
return Markdown\Environment::html()->convert($markdown);
}
```

For complete compatibility with GitHub Flavored Markdown, support for embedded HTML must be enabled; it is disabled
Expand All @@ -51,6 +57,12 @@ by default as a security precaution.
```Hack
$ctx = (new Markdown\ParserContext())->enableHTML_UNSAFE();
$ast = Markdown\parse($ctx, $markdown);

// using `Facebook\Markdown\Environment`

$unsafe = true;
$environment = Markdown\Environment::html($unsafe);
$ast = $environment->parse($markdown);
```

If you are re-using contexts to render multiple independent snippets, you will need to call `->resetFileData()` on the context.
Expand Down
99 changes: 99 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

namespace Facebook\Markdown;

final class Environment<T> {
public function __construct(
private ParserContext $parser,
private RenderContext $context,
private Renderer<T> $renderer,
) {}

public static function html(bool $unsafe = false): Environment<string> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we consider bool parameters to be an antipattern

For this particular one, we definitely want the obvious explicit naming at the ca llers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably also be better as a function than a static method - then new ones can be added by third-party libraries and created in a separate library

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I think this coud be split into 2 functions, html and unsafeHtml or something like that, instead of the bool flag.

$parser = new ParserContext();
$context = new RenderContext();
$renderer = new HTMLRenderer($context);

if ($unsafe) {
$parser->enableHTML_UNSAFE();
}

return new self($parser, $context, $renderer);
}

public function setParser(ParserContext $parser): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these getters and setters? I would definitely remove the setters at least (we don't want someone to change the context in the middle of rendering, or really at any point after it's assigned in the constructor).

$this->parser = $parser;
}

public function getParser(): ParserContext {
return $this->parser;
}

public function setContext(RenderContext $context): void {
$this->context = $context;
}

public function getContext(): RenderContext {
return $this->context;
}

public function setRenderer(Renderer<T> $renderer): void {
$this->renderer = $renderer;
}

public function getRenderer(): Renderer<T> {
return $this->renderer;
}

public function setInlineContext(Inlines\Context $context): void {
$this->parser->setInlineContext($context);
}

public function getInlineContext(): Inlines\Context {
return $this->parser->getInlineContext();
}

public function setBlockContext(UnparsedBlocks\Context $context): void {
$this->parser->setBlockContext($context);
}

public function getBlockContext(): UnparsedBlocks\Context {
return $this->parser->getBlockContext();
}

public function getFilters(): Container<RenderFilter> {
return $this->context->getFilters();
}

public function addFilters(RenderFilter ...$filters): void {
$this->context->appendFilters(...$filters);
}

public function use(Extension $extension): void {
$this->getBlockContext()
->prependBlockTypes(...$extension->getBlockProducers());
$this->getInlineContext()
->prependInlineTypes(...$extension->getInlineTypes());
$this->addFilters(...$extension->getRenderFilters());
}

public function parse(string $markdown): ASTNode {
return parse($this->parser, $markdown);
}

public function render(ASTNode $markdown): T {
return $this->renderer->render($markdown);
}

public function convert(string $markdown): T {
return $this->render($this->parse($markdown));
}
}
34 changes: 34 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

namespace Facebook\Markdown;

abstract class Extension {
/**
* @see Facebook\Markdown\RenderContext::appendFilters()
*/
public function getRenderFilters(): Container<RenderFilter> {
return vec[];
}

/**
* @see Facebook\Markdown\Inlines\Context::prependInlineTypes()
*/
public function getInlineTypes(): Container<classname<Inlines\Inline>> {
return vec[];
}

/**
* @see Facebook\Markdown\UnparsedBlocks\Context::prependBlockTypes()
*/
public function getBlockProducers(): Container<classname<UnparsedBlocks\BlockProducer>> {
return vec[];
}
}