Kirby Editor: Changing Link default behaviour

My client is pasting content from Google Docs (via a downloaded .odt because Google Doc’s HTML is awful) that contains lots of links to external websites. Is there a way I can change the default behaviour of the ‘Open in new window’ toggle to true without changing the plugin source code so they don’t have to go through and change every link themselves?

I can also just add an attribute using JS if there’s no good way to do this.

You could overwrite the paragraph snippet and add target="_blank" to the <a> tags: https://github.com/getkirby/editor/wiki/Block-snippets#block-snippet-names

I don’t want to sound Stack Overflow-y but is this actually a good idea?

2 Likes

Thanks, I’ll give it a try! I agree, it’s not a good idea. “Not my circus, not my monkeys”…

I don’t see any <a> tags in the source snippet:

<p><?= $content ?></p>

Unless there’s a way to change the default value of the toggle in the CMS, I might as well add them using JS.

I meant something like <p><?= str_replace('<a ', '<a target="_blank" ', $content); ?></p> (untested)

Old thread but I’ve same issue and found a solution. May be someone need sample codes:

// this is example for text block
$doc = new DOMDocument;

 // Load your HTML
$doc->loadHTML('<!DOCTYPE html><meta charset="UTF-8">' . $block->toHtml());

$xpath = new DOMXPath($doc);
$links = $xpath->query('//a[starts-with(@href, "http://") or starts-with(@href, "https://")]');

foreach ($links as $link) {
    $link->setAttribute('target', '_blank');
}

echo $doc->saveHTML();
1 Like