feat: add handling for emaillinks

This commit is contained in:
Michael Große
2018-01-29 15:26:17 +01:00
parent e3e6a41c89
commit ea31cc0bac
6 changed files with 119 additions and 0 deletions

50
_test/json/emaillink.json Normal file
View File

@ -0,0 +1,50 @@
{
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Email addresses like this one: "
},
{
"type": "emaillink",
"content": [
{
"type": "text",
"text": "andi@splitbrain.org"
}
],
"attrs": {
"href": "mailto:andi@splitbrain.org",
"class": "mail",
"title": "andi@splitbrain.org"
}
},
{
"type": "text",
"text": " or "
},
{
"type": "emaillink",
"content": [
{
"type": "text",
"text": "write me!"
}
],
"attrs": {
"href": "mailto:andi@splitbrain.org",
"class": "mail",
"title": "andi@splitbrain.org"
}
},
{
"type": "text",
"text": " are recognized, too."
}
]
}
]
}

1
_test/json/emaillink.txt Normal file
View File

@ -0,0 +1 @@
Email addresses like this one: [[andi@splitbrain.org]] or [[andi@splitbrain.org|write me!]] are recognized, too.

32
parser/EmailLinkNode.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace dokuwiki\plugin\prosemirror\parser;
class EmailLinkNode extends Node
{
protected $data;
public function __construct($data, $parent)
{
$this->data = $data;
}
public function toSyntax()
{
list(, $address) = explode(':', $this->data['attrs']['href'], 2);
$title = '';
if ($this->data['content'][0]['type'] === 'image') {
$imageNode = new ImageNode($this->data['content'][0], $this);
$title = '|' . $imageNode->toSyntax();
} else {
if ($address !== $this->data['content'][0]['text']) {
$title = '|' . $this->data['content'][0]['text'];
}
}
return '[[' . $address . $title . ']]';
}
}

View File

@ -13,6 +13,7 @@ abstract class Node {
'interwikilink' => InterwikiLinkNode::class,
'internallink' => InternalLinkNode::class,
'externallink' => ExternalLinkNode::class,
'emaillink' => EmailLinkNode::class,
'locallink' => LocalLinkNode::class,
'preformatted' => PreformattedNode::class,
'code_block' => CodeBlockNode::class,

View File

@ -393,6 +393,26 @@ class renderer_plugin_prosemirror extends Doku_Renderer {
$this->nodestack->drop('interwikilink');
}
public function emaillink($address, $name = null)
{
if (null === $name) {
$name = $address;
}
$isImage = is_array($name);
if ($isImage) {
$class = 'media';
} else {
$class = 'mail';
}
$emailLink = new Node('emaillink');
$emailLink->attr('href', 'mailto:' . $address);
$emailLink->attr('class', $class);
$emailLink->attr('title', $address);
$this->nodestack->addTop($emailLink);
$this->cdata($name ?: $address);
$this->nodestack->drop('emaillink');
}
/** @inheritDoc */
function linebreak() {
$this->nodestack->add(new Node('hard_break'));

View File

@ -132,6 +132,21 @@ nodes = nodes.addToEnd('locallink', {
},
});
nodes = nodes.addToEnd('emaillink', {
content: 'text|image',
group: 'inline', // fixme should later be changed to substition? or add substitution?
inline: true,
attrs: {
class: {},
href: {},
title: {},
},
toDOM(node) {
return ['a', node.attrs, 0];
},
});
nodes = nodes.addToEnd('footnote', {
content: 'inline',
group: 'inline',