Skip to content
40 changes: 40 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,43 @@ parameters:
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Styled\\:\\:renderHTML\\(\\) has parameter \\$mark with no type specified\\.$#"
count: 1
path: src/Mark/Styled.php

-
message: "#^Property Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:\\$name has no type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:addOptions\\(\\) has no return type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:addAttributes\\(\\) has no return type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:parseHTML\\(\\) has no return type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:renderHTML\\(\\) has no return type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:renderHTML\\(\\) has parameter \\$HTMLAttributes with no type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Method Storyblok\\\\Tiptap\\\\Mark\\\\Link\\:\\:renderHTML\\(\\) has parameter \\$mark with no type specified\\.$#"
count: 1
path: src/Mark/Link.php

-
message: "#^Access to an undefined property object::\\$href\\.$#"
count: 2
path: src/Mark/Link.php
2 changes: 1 addition & 1 deletion src/Extension/Storyblok.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Storyblok\Tiptap\Extension;

use Storyblok\Tiptap\Mark\Link;
use Storyblok\Tiptap\Mark\Styled;
use Storyblok\Tiptap\Node\Blok;
use Storyblok\Tiptap\Node\BulletList;
Expand All @@ -26,7 +27,6 @@
use Tiptap\Marks\Code;
use Tiptap\Marks\Highlight;
use Tiptap\Marks\Italic;
use Tiptap\Marks\Link;
use Tiptap\Marks\Strike;
use Tiptap\Marks\Subscript;
use Tiptap\Marks\Superscript;
Expand Down
72 changes: 72 additions & 0 deletions src/Mark/Link.php
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',
Copy link
Contributor

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?

Copy link
Contributor Author

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.

],
];
}

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,
];
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Extension/StoryblokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PHPUnit\Framework\TestCase;
use Storyblok\Tiptap\Extension\Storyblok;
use Storyblok\Tiptap\Mark\Link;
use Storyblok\Tiptap\Mark\Styled;
use Storyblok\Tiptap\Node\Blok;
use Storyblok\Tiptap\Node\BulletList;
Expand All @@ -27,7 +28,6 @@
use Tiptap\Marks\Code;
use Tiptap\Marks\Highlight;
use Tiptap\Marks\Italic;
use Tiptap\Marks\Link;
use Tiptap\Marks\Strike;
use Tiptap\Marks\Subscript;
use Tiptap\Marks\Superscript;
Expand Down
143 changes: 143 additions & 0 deletions tests/Unit/Mark/LinkTest.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPUnit\Framework\TestCase;
use Storyblok\Tiptap\Mark\Styled;

final class MarkTest extends TestCase
final class StyledTest extends TestCase
{
/**
* @test
Expand Down