This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Add Markdown Environment and Extensions support #7
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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> { | ||
| $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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
| } | ||
This file contains hidden or 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,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[]; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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,
htmlandunsafeHtmlor something like that, instead of the bool flag.