Link

create#

This method creates a link in the selected text or if the cursor is collapsed. You can use properties such as:

  • text
  • url
  • target

const $link = app.link.create( url: 'https://example.com' );

update#

This method updates the properties of the links selected in the text.

Change data for all selected links


app.link.update( url: 'https://example.com' );

Add a target for all selected links


app.link.update( target: '_blank' );

Remove target for all selected links


app.link.update( target: null );

Update the text of all selected links


const $links = app.link.get();
$links.each($link => {
    $link.text('New text');
});

Remove the text from the link and put the cursor at the beginning of the link


const $link = app.link.update({ text: '' });
const caret = app.create('caret');
caret.set($link, 'start');

get#

This method returns an array with the Dom elements of all links in the selected text.

Get all links in the selected text


const $links = app.link.get();

Get the first selected link


const $link = app.link.get().first();
$link.addClass('link-classname');

Check if there are links in the selection


const $links = app.link.get();
if ($links.length) {
   // some action
}

Get the link and select it


const $link = app.link.get().first();
const selection = app.create('selection');

selection.select($link);

Get a link or links and modify their Dom properties


const $links = app.link.get();
$links.addClass('link-classname');

Get the URL of all selected links


const $links = app.link.get();
const urls = $links.map($link => $link.attr('href'));

Check if there are links with a certain data attribute


const $links = app.link.get();
const hasDataAttr = $links.some(link => link.attr('data-id'));
if (hasDataAttr) {
    console.log('Has links with data-id');
}

This method removes links in the selected content, keeping only the link text.

Make unlink for all selected links


this.app.link.unlink();

Remove only certain links by classname


const $links = app.link.get();
$links.filter('.link-classname').each(link => {
    app.link.unlink(link);
});