downgrade to kirby v3
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace Kirby\Parsley\Schema;
|
||||
|
||||
use DOMElement;
|
||||
use DOMText;
|
||||
use Kirby\Parsley\Element;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
@@ -21,22 +19,22 @@ use Kirby\Toolkit\Str;
|
||||
*/
|
||||
class Blocks extends Plain
|
||||
{
|
||||
/**
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return array
|
||||
*/
|
||||
public function blockquote(Element $node): array
|
||||
{
|
||||
$text = [];
|
||||
$citation = null;
|
||||
$text = [];
|
||||
|
||||
// get all the text for the quote
|
||||
foreach ($node->children() as $child) {
|
||||
if ($child instanceof DOMText) {
|
||||
if (is_a($child, 'DOMText') === true) {
|
||||
$text[] = trim($child->textContent);
|
||||
}
|
||||
|
||||
if (
|
||||
$child instanceof DOMElement &&
|
||||
$child->tagName !== 'footer'
|
||||
) {
|
||||
$element = new Element($child);
|
||||
$text[] = $element->innerHTML($this->marks());
|
||||
if (is_a($child, 'DOMElement') === true && $child->tagName !== 'footer') {
|
||||
$text[] = (new Element($child))->innerHTML($this->marks());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +42,9 @@ class Blocks extends Plain
|
||||
$text = implode('', array_filter($text));
|
||||
|
||||
// get the citation from the footer
|
||||
$citation = $node->find('footer')?->innerHTML($this->marks());
|
||||
if ($footer = $node->find('footer')) {
|
||||
$citation = $footer->innerHTML($this->marks());
|
||||
}
|
||||
|
||||
return [
|
||||
'content' => [
|
||||
@@ -58,10 +58,13 @@ class Blocks extends Plain
|
||||
/**
|
||||
* Creates the fallback block type
|
||||
* if no other block can be found
|
||||
*
|
||||
* @param \Kirby\Parsley\Element|string $element
|
||||
* @return array|null
|
||||
*/
|
||||
public function fallback(Element|string $element): array|null
|
||||
public function fallback($element): ?array
|
||||
{
|
||||
if ($element instanceof Element) {
|
||||
if (is_a($element, Element::class) === true) {
|
||||
$html = $element->innerHtml();
|
||||
|
||||
// wrap the inner HTML in a p tag if it doesn't
|
||||
@@ -91,6 +94,9 @@ class Blocks extends Plain
|
||||
|
||||
/**
|
||||
* Converts a heading element to a heading block
|
||||
*
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return array
|
||||
*/
|
||||
public function heading(Element $node): array
|
||||
{
|
||||
@@ -111,14 +117,21 @@ class Blocks extends Plain
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return array
|
||||
*/
|
||||
public function iframe(Element $node): array
|
||||
{
|
||||
$src = $node->attr('src');
|
||||
$figcaption = $node->find('ancestor::figure[1]//figcaption');
|
||||
$caption = $figcaption?->innerHTML($this->marks());
|
||||
$caption = null;
|
||||
$src = $node->attr('src');
|
||||
|
||||
// avoid parsing the caption twice
|
||||
$figcaption?->remove();
|
||||
if ($figcaption = $node->find('ancestor::figure[1]//figcaption')) {
|
||||
$caption = $figcaption->innerHTML($this->marks());
|
||||
|
||||
// avoid parsing the caption twice
|
||||
$figcaption->remove();
|
||||
}
|
||||
|
||||
// reverse engineer video URLs
|
||||
if (preg_match('!player.vimeo.com\/video\/([0-9]+)!i', $src, $array) === 1) {
|
||||
@@ -150,14 +163,25 @@ class Blocks extends Plain
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return array
|
||||
*/
|
||||
public function img(Element $node): array
|
||||
{
|
||||
$link = $node->find('ancestor::a')?->attr('href');
|
||||
$figcaption = $node->find('ancestor::figure[1]//figcaption');
|
||||
$caption = $figcaption?->innerHTML($this->marks());
|
||||
$caption = null;
|
||||
$link = null;
|
||||
|
||||
// avoid parsing the caption twice
|
||||
$figcaption?->remove();
|
||||
if ($figcaption = $node->find('ancestor::figure[1]//figcaption')) {
|
||||
$caption = $figcaption->innerHTML($this->marks());
|
||||
|
||||
// avoid parsing the caption twice
|
||||
$figcaption->remove();
|
||||
}
|
||||
|
||||
if ($a = $node->find('ancestor::a')) {
|
||||
$link = $a->attr('href');
|
||||
}
|
||||
|
||||
return [
|
||||
'content' => [
|
||||
@@ -173,6 +197,9 @@ class Blocks extends Plain
|
||||
|
||||
/**
|
||||
* Converts a list element to HTML
|
||||
*
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return string
|
||||
*/
|
||||
public function list(Element $node): string
|
||||
{
|
||||
@@ -182,30 +209,30 @@ class Blocks extends Plain
|
||||
$innerHtml = '';
|
||||
|
||||
foreach ($li->children() as $child) {
|
||||
if ($child instanceof DOMText) {
|
||||
if (is_a($child, 'DOMText') === true) {
|
||||
$innerHtml .= $child->textContent;
|
||||
} elseif ($child instanceof DOMElement) {
|
||||
} elseif (is_a($child, 'DOMElement') === true) {
|
||||
$child = new Element($child);
|
||||
$list = ['ul', 'ol'];
|
||||
$innerHtml .= match (in_array($child->tagName(), $list)) {
|
||||
true => $this->list($child),
|
||||
default => $child->innerHTML($this->marks())
|
||||
};
|
||||
|
||||
if (in_array($child->tagName(), ['ul', 'ol']) === true) {
|
||||
$innerHtml .= $this->list($child);
|
||||
} else {
|
||||
$innerHtml .= $child->innerHTML($this->marks());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$html[] = '<li>' . trim($innerHtml) . '</li>';
|
||||
}
|
||||
|
||||
$outerHtml = '<' . $node->tagName() . '>';
|
||||
$outerHtml .= implode($html);
|
||||
$outerHtml .= '</' . $node->tagName() . '>';
|
||||
return $outerHtml;
|
||||
return '<' . $node->tagName() . '>' . implode($html) . '</' . $node->tagName() . '>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of allowed inline marks
|
||||
* and their parsing rules
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function marks(): array
|
||||
{
|
||||
@@ -214,7 +241,7 @@ class Blocks extends Plain
|
||||
'tag' => 'a',
|
||||
'attrs' => ['href', 'rel', 'target', 'title'],
|
||||
'defaults' => [
|
||||
'rel' => 'noreferrer'
|
||||
'rel' => 'noopener noreferrer'
|
||||
]
|
||||
],
|
||||
[
|
||||
@@ -264,79 +291,114 @@ class Blocks extends Plain
|
||||
* their parsing rules
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return array
|
||||
*/
|
||||
public function nodes(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'tag' => 'blockquote',
|
||||
'parse' => fn (Element $node) => $this->blockquote($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->blockquote($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'h1',
|
||||
'parse' => fn (Element $node) => $this->heading($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->heading($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'h2',
|
||||
'parse' => fn (Element $node) => $this->heading($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->heading($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'h3',
|
||||
'parse' => fn (Element $node) => $this->heading($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->heading($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'h4',
|
||||
'parse' => fn (Element $node) => $this->heading($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->heading($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'h5',
|
||||
'parse' => fn (Element $node) => $this->heading($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->heading($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'h6',
|
||||
'parse' => fn (Element $node) => $this->heading($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->heading($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'hr',
|
||||
'parse' => fn (Element $node) => ['type' => 'line']
|
||||
'parse' => function (Element $node) {
|
||||
return [
|
||||
'type' => 'line'
|
||||
];
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'iframe',
|
||||
'parse' => fn (Element $node) => $this->iframe($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->iframe($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'img',
|
||||
'parse' => fn (Element $node) => $this->img($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->img($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'ol',
|
||||
'parse' => fn (Element $node) => [
|
||||
'content' => [
|
||||
'text' => $this->list($node)
|
||||
],
|
||||
'type' => 'list',
|
||||
]
|
||||
'parse' => function (Element $node) {
|
||||
return [
|
||||
'content' => [
|
||||
'text' => $this->list($node)
|
||||
],
|
||||
'type' => 'list',
|
||||
];
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'pre',
|
||||
'parse' => fn (Element $node) => $this->pre($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->pre($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'table',
|
||||
'parse' => fn (Element $node) => $this->table($node)
|
||||
'parse' => function (Element $node) {
|
||||
return $this->table($node);
|
||||
}
|
||||
],
|
||||
[
|
||||
'tag' => 'ul',
|
||||
'parse' => fn (Element $node) => [
|
||||
'content' => [
|
||||
'text' => $this->list($node)
|
||||
],
|
||||
'type' => 'list',
|
||||
]
|
||||
'parse' => function (Element $node) {
|
||||
return [
|
||||
'content' => [
|
||||
'text' => $this->list($node)
|
||||
],
|
||||
'type' => 'list',
|
||||
];
|
||||
}
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return array
|
||||
*/
|
||||
public function pre(Element $node): array
|
||||
{
|
||||
$language = 'text';
|
||||
@@ -359,6 +421,10 @@ class Blocks extends Plain
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Kirby\Parsley\Element $node
|
||||
* @return array
|
||||
*/
|
||||
public function table(Element $node): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user