-
Notifications
You must be signed in to change notification settings - Fork 4
Added Link mark supporting email, anchors and custom attributes #32
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
Open
RicLeP
wants to merge
11
commits into
storyblok:main
Choose a base branch
from
RicLeP:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8bc843d
Add support for custom classes with ‘styled’ Mark. Fixes #23
RicLeP 1c9e7c2
Changed PHPStan settings
RicLeP 0b851c4
Update src/Mark/Styled.php
edodusi 57c858e
Added declare(strict_types=1); and copyright comment to top of Styled…
RicLeP 597e024
Merge remote-tracking branch 'origin/main'
RicLeP c15ed07
Handle Storyblok link functionality
RicLeP a8b9729
Merge remote-tracking branch 'origin/main'
RicLeP 8fdcc3b
PHPstan and fix link custom attributes
RicLeP c315723
Load Storyblok Link extension instead of default TipTap one
RicLeP 216807d
Fix custom attrs on Link and add tests
RicLeP 9e43659
Remove target from link
RicLeP 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
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,72 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of Storyblok PHP Tiptap Extension. | ||
| * | ||
| * (c) Storyblok GmbH | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Storyblok\Tiptap\Mark; | ||
|
|
||
| use Tiptap\Core\Mark; | ||
| use Tiptap\Utils\HTML; | ||
|
|
||
| class Link extends Mark | ||
| { | ||
| public static $name = 'link'; | ||
|
|
||
| public function addOptions() | ||
| { | ||
| return [ | ||
| 'HTMLAttributes' => [ | ||
| 'target' => '_blank', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function parseHTML() | ||
| { | ||
| return [ | ||
| [ | ||
| 'tag' => 'a[href]', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function addAttributes() | ||
| { | ||
| return [ | ||
| 'href' => [], | ||
| 'target' => [], | ||
| 'rel' => [], | ||
| ]; | ||
| } | ||
|
|
||
| public function renderHTML($mark, $HTMLAttributes = []) | ||
| { | ||
| if (isset($mark->attrs->linktype) && $mark->attrs->linktype === 'email') { | ||
| $HTMLAttributes['href'] = 'mailto:' . $mark->attrs->href; | ||
| } | ||
|
|
||
| if (isset($mark->attrs->anchor) && $mark->attrs->anchor) { | ||
| $HTMLAttributes['href'] = $mark->attrs->href . '#' . $mark->attrs->anchor; | ||
| } | ||
|
|
||
| if (isset($mark->attrs->custom)) { | ||
| foreach ($mark->attrs->custom as $key => $value) { | ||
| $HTMLAttributes[$key] = $value; | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| 'a', | ||
| HTML::mergeAttributes($this->options['HTMLAttributes'], $HTMLAttributes), | ||
| 0, | ||
| ]; | ||
| } | ||
| } | ||
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,143 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of Storyblok PHP Tiptap Extension. | ||
| * | ||
| * (c) Storyblok GmbH | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Storyblok\Tiptap\Tests\Unit\Mark; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Storyblok\Tiptap\Mark\Link; | ||
| use Tiptap\Editor; | ||
|
|
||
| final class LinkTest extends TestCase | ||
| { | ||
| /** | ||
| * @test | ||
| */ | ||
| public function name(): void | ||
| { | ||
| self::assertSame('link', (new Link())::$name); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function addOptions(): void | ||
| { | ||
| self::assertSame([ | ||
| 'HTMLAttributes' => [ | ||
| 'target' => '_blank' | ||
| ], | ||
| ], (new Link())->addOptions()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function prependsMailto(): void | ||
| { | ||
| $document = [ | ||
| 'type' => 'doc', | ||
| 'content' => [ | ||
| [ | ||
| 'type' => 'text', | ||
| 'text' => 'Example Email', | ||
| 'marks' => [ | ||
| [ | ||
| 'type' => 'link', | ||
| 'attrs' => [ | ||
| 'href' => '[email protected]', | ||
| 'linktype' => 'email', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]; | ||
|
|
||
| $result = (new Editor([ | ||
| 'extensions' => [ | ||
| new Link, | ||
| ], | ||
| ]))->setContent($document)->getHTML(); | ||
|
|
||
| self::assertSame('<a target="_blank" href="mailto:[email protected]">Example Email</a>', $result); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function appendsAnchors(): void | ||
| { | ||
| $document = [ | ||
| 'type' => 'doc', | ||
| 'content' => [ | ||
| [ | ||
| 'type' => 'text', | ||
| 'text' => 'Example Link', | ||
| 'marks' => [ | ||
| [ | ||
| 'type' => 'link', | ||
| 'attrs' => [ | ||
| 'href' => 'https://storyblok.com', | ||
| 'anchor' => 'anchor', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]; | ||
|
|
||
| $result = (new Editor([ | ||
| 'extensions' => [ | ||
| new Link, | ||
| ], | ||
| ]))->setContent($document)->getHTML(); | ||
|
|
||
| self::assertSame('<a target="_blank" href="https://storyblok.com#anchor">Example Link</a>', $result); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function addsCustomAttributes(): void | ||
| { | ||
| $document = [ | ||
| 'type' => 'doc', | ||
| 'content' => [ | ||
| [ | ||
| 'type' => 'text', | ||
| 'text' => 'Example Link', | ||
| 'marks' => [ | ||
| [ | ||
| 'type' => 'link', | ||
| 'attrs' => [ | ||
| 'href' => 'https://storyblok.com', | ||
| 'custom' => [ | ||
| 'title' => 'title', | ||
| 'custom' => 'custom', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]; | ||
|
|
||
| $result = (new Editor([ | ||
| 'extensions' => [ | ||
| new Link, | ||
| ], | ||
| ]))->setContent($document)->getHTML(); | ||
|
|
||
| self::assertSame('<a target="_blank" href="https://storyblok.com" title="title" custom="custom">Example Link</a>', $result); | ||
| } | ||
| } |
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
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.
not sure this should be the default, why do you think it should?
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.
@edodusi I agree, this was taken from the base Link class in TipTap, I wanted to minimise what changes my replacement makes but as Storyblok includes a target in the JSON we don’t need this. I’ll update the PR.