-
-
Notifications
You must be signed in to change notification settings - Fork 200
Import the Attributes extension (#484) #489
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
Merged
Merged
Changes from all commits
Commits
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,58 @@ | ||
| --- | ||
| layout: default | ||
| title: Attributes Extension | ||
| description: The AttributesExtension allows HTML attributes to be added from within the document. | ||
| redirect_from: /extensions/attributes/ | ||
| --- | ||
|
|
||
| # Attributes | ||
|
|
||
| The `AttributesExtension` allows HTML attributes to be added from within the document. | ||
|
|
||
| ## Attribute Syntax | ||
colinodell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| The basic syntax was inspired by [Kramdown](http://kramdown.gettalong.org/syntax.html#attribute-list-definitions)'s Attribute Lists feature. | ||
|
|
||
| You can assign any attribute to a block-level element. Just directly prepend or follow the block with a block inline attribute list. | ||
| That consists of a left curly brace, optionally followed by a colon, the attribute definitions and a right curly brace: | ||
|
|
||
| ```markdown | ||
| > A nice blockquote | ||
| {: title="Blockquote title"} | ||
|
|
||
| {#id .class} | ||
| ## Header | ||
| ``` | ||
|
|
||
| As with a block-level element you can assign any attribute to a span-level elements using a span inline attribute list, | ||
| that has the same syntax and must immediately follow the span-level element: | ||
|
|
||
| ```markdown | ||
| This is *red*{style="color: red"}. | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Configure your `Environment` as usual and simply add the `AttributesExtension`: | ||
|
|
||
| ```php | ||
| <?php | ||
| use League\CommonMark\CommonMarkConverter; | ||
| use League\CommonMark\Environment; | ||
| use League\CommonMark\Extension\Attributes\AttributesExtension; | ||
|
|
||
| // Obtain a pre-configured Environment with all the CommonMark parsers/renderers ready-to-go | ||
| $environment = Environment::createCommonMarkEnvironment(); | ||
|
|
||
| // Add the extension | ||
| $environment->addExtension(new AttributesExtension()); | ||
|
|
||
| // Set your configuration if needed | ||
| $config = [ | ||
| // ... | ||
| ]; | ||
|
|
||
| // Instantiate the converter engine and start converting some Markdown! | ||
| $converter = new CommonMarkConverter($config, $environment); | ||
| echo $converter->convertToHtml('# Hello World!'); | ||
| ``` | ||
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
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,32 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the league/commonmark package. | ||
| * | ||
| * (c) Colin O'Dell <[email protected]> | ||
| * (c) 2015 Martin Hasoň <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace League\CommonMark\Extension\Attributes; | ||
|
|
||
| use League\CommonMark\ConfigurableEnvironmentInterface; | ||
| use League\CommonMark\Event\DocumentParsedEvent; | ||
| use League\CommonMark\Extension\Attributes\Event\AttributesListener; | ||
| use League\CommonMark\Extension\Attributes\Parser\AttributesBlockParser; | ||
| use League\CommonMark\Extension\Attributes\Parser\AttributesInlineParser; | ||
| use League\CommonMark\Extension\ExtensionInterface; | ||
|
|
||
| final class AttributesExtension implements ExtensionInterface | ||
| { | ||
| public function register(ConfigurableEnvironmentInterface $environment) | ||
| { | ||
| $environment->addBlockParser(new AttributesBlockParser()); | ||
| $environment->addInlineParser(new AttributesInlineParser()); | ||
| $environment->addEventListener(DocumentParsedEvent::class, [new AttributesListener(), 'processDocument']); | ||
| } | ||
| } |
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,174 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the league/commonmark package. | ||
| * | ||
| * (c) Colin O'Dell <[email protected]> | ||
| * (c) 2015 Martin Hasoň <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace League\CommonMark\Extension\Attributes\Event; | ||
|
|
||
| use League\CommonMark\Block\Element\AbstractBlock; | ||
| use League\CommonMark\Block\Element\FencedCode; | ||
| use League\CommonMark\Block\Element\ListBlock; | ||
| use League\CommonMark\Block\Element\ListItem; | ||
| use League\CommonMark\Event\DocumentParsedEvent; | ||
| use League\CommonMark\Extension\Attributes\Node\Attributes; | ||
| use League\CommonMark\Extension\Attributes\Node\AttributesInline; | ||
| use League\CommonMark\Inline\Element\AbstractInline; | ||
| use League\CommonMark\Node\Node; | ||
|
|
||
| final class AttributesListener | ||
| { | ||
| private const DIRECTION_PREFIX = 'prefix'; | ||
| private const DIRECTION_SUFFIX = 'suffix'; | ||
|
|
||
| public function processDocument(DocumentParsedEvent $event): void | ||
| { | ||
| $walker = $event->getDocument()->walker(); | ||
| while ($event = $walker->next()) { | ||
| $node = $event->getNode(); | ||
| if (!$node instanceof AttributesInline && ($event->isEntering() || !$node instanceof Attributes)) { | ||
| continue; | ||
| } | ||
|
|
||
| [$target, $direction] = self::findTargetAndDirection($node); | ||
|
|
||
| if ($target instanceof AbstractBlock || $target instanceof AbstractInline) { | ||
| $parent = $target->parent(); | ||
| if ($parent instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) { | ||
| $target = $parent; | ||
| } | ||
|
|
||
| if ($direction === self::DIRECTION_SUFFIX) { | ||
| $attributes = self::merge($target, $node->getAttributes()); | ||
| } else { | ||
| $attributes = self::merge($node->getAttributes(), $target); | ||
| } | ||
|
|
||
| $target->data['attributes'] = $attributes; | ||
| } | ||
|
|
||
| if ($node instanceof AbstractBlock && $node->endsWithBlankLine() && $node->next() && $node->previous()) { | ||
| $previous = $node->previous(); | ||
| if ($previous instanceof AbstractBlock) { | ||
| $previous->setLastLineBlank(true); | ||
| } | ||
| } | ||
|
|
||
| $node->detach(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param Node $node | ||
| * | ||
| * @return array<Node|string|null> | ||
| */ | ||
| private static function findTargetAndDirection(Node $node): array | ||
| { | ||
| $target = null; | ||
| $direction = null; | ||
| $previous = $next = $node; | ||
| while (true) { | ||
| $previous = self::getPrevious($previous); | ||
| $next = self::getNext($next); | ||
|
|
||
| if ($previous === null && $next === null) { | ||
| if (!$node->parent() instanceof FencedCode) { | ||
| $target = $node->parent(); | ||
| $direction = self::DIRECTION_SUFFIX; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| if ($node instanceof AttributesInline && ($previous === null || ($previous instanceof AbstractInline && $node->isBlock()))) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($previous !== null && !self::isAttributesNode($previous)) { | ||
| $target = $previous; | ||
| $direction = self::DIRECTION_SUFFIX; | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| if ($next !== null && !self::isAttributesNode($next)) { | ||
| $target = $next; | ||
| $direction = self::DIRECTION_PREFIX; | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| return [$target, $direction]; | ||
| } | ||
|
|
||
| private static function getPrevious(?Node $node = null): ?Node | ||
| { | ||
| $previous = $node instanceof Node ? $node->previous() : null; | ||
|
|
||
| if ($previous instanceof AbstractBlock && $previous->endsWithBlankLine()) { | ||
| $previous = null; | ||
| } | ||
|
|
||
| return $previous; | ||
| } | ||
|
|
||
| private static function getNext(?Node $node = null): ?Node | ||
| { | ||
| $next = $node instanceof Node ? $node->next() : null; | ||
|
|
||
| if ($node instanceof AbstractBlock && $node->endsWithBlankLine()) { | ||
| $next = null; | ||
| } | ||
|
|
||
| return $next; | ||
| } | ||
|
|
||
| private static function isAttributesNode(Node $node): bool | ||
| { | ||
| return $node instanceof Attributes || $node instanceof AttributesInline; | ||
| } | ||
|
|
||
| /** | ||
| * @param AbstractBlock|AbstractInline|array<string, mixed> $attributes1 | ||
| * @param AbstractBlock|AbstractInline|array<string, mixed> $attributes2 | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private static function merge($attributes1, $attributes2): array | ||
| { | ||
| $attributes = []; | ||
| foreach ([$attributes1, $attributes2] as $arg) { | ||
| if ($arg instanceof AbstractBlock || $arg instanceof AbstractInline) { | ||
| $arg = $arg->data['attributes'] ?? []; | ||
| } | ||
|
|
||
| /** @var array<string, mixed> $arg */ | ||
| $arg = (array) $arg; | ||
| if (isset($arg['class'])) { | ||
| foreach (\array_filter(\explode(' ', \trim($arg['class']))) as $class) { | ||
| $attributes['class'][] = $class; | ||
| } | ||
|
|
||
| unset($arg['class']); | ||
| } | ||
|
|
||
| $attributes = \array_merge($attributes, $arg); | ||
| } | ||
|
|
||
| if (isset($attributes['class'])) { | ||
| $attributes['class'] = \implode(' ', $attributes['class']); | ||
| } | ||
|
|
||
| return $attributes; | ||
| } | ||
| } |
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,62 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the league/commonmark package. | ||
| * | ||
| * (c) Colin O'Dell <[email protected]> | ||
| * (c) 2015 Martin Hasoň <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace League\CommonMark\Extension\Attributes\Node; | ||
|
|
||
| use League\CommonMark\Block\Element\AbstractBlock; | ||
| use League\CommonMark\Cursor; | ||
|
|
||
| final class Attributes extends AbstractBlock | ||
| { | ||
| /** @var array<string, mixed> */ | ||
| private $attributes; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $attributes | ||
| */ | ||
| public function __construct(array $attributes) | ||
| { | ||
| $this->attributes = $attributes; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function getAttributes(): array | ||
| { | ||
| return $this->attributes; | ||
| } | ||
|
|
||
| public function canContain(AbstractBlock $block): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public function isCode(): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public function matchesNextLine(Cursor $cursor): bool | ||
| { | ||
| $this->setLastLineBlank($cursor->isBlank()); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool | ||
| { | ||
| return false; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.