This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
171 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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 function getParser(): ParserContext { | ||
return $this->parser; | ||
} | ||
|
||
public function getContext(): RenderContext { | ||
return $this->context; | ||
} | ||
|
||
public function getRenderer(): Renderer<T> { | ||
return $this->renderer; | ||
} | ||
|
||
public function getInlineContext(): Inlines\Context { | ||
return $this->parser->getInlineContext(); | ||
} | ||
|
||
public function getBlockContext(): UnparsedBlocks\Context { | ||
return $this->parser->getBlockContext(); | ||
} | ||
|
||
public function getFilters(): Container<RenderFilter> { | ||
return $this->context->getFilters(); | ||
} | ||
|
||
public function use(Plugin $plugin): void { | ||
$this->getBlockContext() | ||
->prependBlockTypes(...$plugin->getBlockProducers()); | ||
$this->getInlineContext() | ||
->prependInlineTypes(...$plugin->getInlineTypes()); | ||
$this->getContext()->appendFilters(...$plugin->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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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 Plugin { | ||
/** | ||
* @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[]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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; | ||
|
||
function html_environment(): Environment<string> { | ||
$parser = new ParserContext(); | ||
$context = new RenderContext(); | ||
$renderer = new HTMLRenderer($context); | ||
|
||
return new Environment($parser, $context, $renderer); | ||
} | ||
|
||
function unsafe_html_environment(): Environment<string> { | ||
$parser = new ParserContext(); | ||
$context = new RenderContext(); | ||
$renderer = new HTMLRenderer($context); | ||
|
||
$parser->enableHTML_UNSAFE(); | ||
|
||
return new Environment($parser, $context, $renderer); | ||
} |