Skip to content

Commit

Permalink
Widen support for tags in TipTaps HTML to rich text transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
apfelbox committed Sep 19, 2024
1 parent 761c988 commit eefb7d9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.14.2
======

* (improvement) Widen support for tags in TipTaps HTML to rich text transformer


3.14.1
======

Expand Down
19 changes: 18 additions & 1 deletion src/RichText/HtmlToRichTextTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
namespace Torr\Storyblok\RichText;

use Tiptap\Editor;
use Tiptap\Extensions\StarterKit;
use Tiptap\Marks\Highlight;
use Tiptap\Marks\Link;
use Tiptap\Marks\Subscript;
use Tiptap\Marks\Superscript;
use Tiptap\Marks\Underline;
use Tiptap\Nodes\BulletList;
use Tiptap\Nodes\CodeBlock;
use Tiptap\Nodes\HardBreak;
Expand All @@ -21,7 +27,18 @@ final class HtmlToRichTextTransformer
*/
public function __construct ()
{
$this->editor = new Editor();
$this->editor = new Editor([
"extensions" => [
new StarterKit(),
new Link(),
new Highlight([
"multicolor" => true,
]),
new Superscript(),
new Subscript(),
new Underline(),
],
]);
}

/**
Expand Down
131 changes: 131 additions & 0 deletions tests/RichText/HtmlToRichTextTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public function testBasic () : void
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
<p>Text with
<strong>bold 1</strong>
<b>bold 2</b>
<em>italic 1</em>
<i>italic 2</i>
<a href="/test" target="_blank">External Link</a>
<a href="/test">Link</a>
<mark>highlight</mark>
<mark data-color="#ef2b7c">highlight with color</mark>
<sup>superscript</sup>
<sub>subscript</sub>
<u>underline</u>
<br>
<code>code</code>
</p>
<hr>
HTML;

// use assertSame() as the order is important
Expand Down Expand Up @@ -155,6 +171,121 @@ public function testBasic () : void
],
],
],
[
"type" => "paragraph",
"content" => [
[
"type" => "text",
"text" => "Text with\n",
],
[
"type" => "text",
"text" => "bold 1",
"marks" => [
["type" => "bold"],
],
],
[
"type" => "text",
"text" => "bold 2",
"marks" => [
["type" => "bold"],
],
],
[
"type" => "text",
"text" => "italic 1",
"marks" => [
["type" => "italic"],
],
],
[
"type" => "text",
"text" => "italic 2",
"marks" => [
["type" => "italic"],
],
],
[
"type" => "text",
"text" => "External Link",
"marks" => [
[
"type" => "link",
"attrs" => [
"href" => "/test",
"target" => "_blank",
],
],
],
],
[
"type" => "text",
"text" => "Link",
"marks" => [
[
"type" => "link",
"attrs" => [
"href" => "/test",
],
],
],
],
[
"type" => "text",
"text" => "highlight",
"marks" => [
["type" => "highlight"],
],
],
[
"type" => "text",
"text" => "highlight with color",
"marks" => [
[
"type" => "highlight",
"attrs" => [
"color" => "#ef2b7c",
],
],
],
],
[
"type" => "text",
"text" => "superscript",
"marks" => [
["type" => "superscript"],
],
],
[
"type" => "text",
"text" => "subscript",
"marks" => [
["type" => "subscript"],
],
],
[
"type" => "text",
"text" => "underline",
"marks" => [
["type" => "underline"],
],
],
[
"type" => "hard_break",
],
[
"type" => "text",
"text" => "code",
"marks" => [
["type" => "code"],
],
],
],
],
[
"type" => "horizontal_rule",
],
],
], $transformer->parseHtmlToRichText($html));
}
Expand Down

0 comments on commit eefb7d9

Please sign in to comment.