Skip to content
gpdoc
Playground

Link

Add links to selected text or detect addresses as users type and paste.

Link adds the link mark. It stores a destination in href and can also store a link title, target, relation, and class.

Terminal window
cargo add gpdoc-plugin-link

Register link formatting and activation in the Quickstart editor configuration.

use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_link::{LinkOptions, link, link_activation, link_format};
use gpdoc_plugin_paragraph::paragraph;
let config = EditorConfig::builder()
.plugin(paragraph())
.plugin(link(LinkOptions::default()).with_view(link_views))
.build()
.expect("configure editor");
fn link_views() -> [ViewPlugin; 2] {
[link_format(), link_activation()]
}

Type https://example.com followed by a space. The editor underlines the address. Double-click the link or click it with the primary modifier to open it.

Use link_popover to show controls above the caret when the editor has focus and the caret is inside a link. The plugin positions the panel and keeps focus in the editor when you click it.

link_popover(render) Anchors your controls above the caret
└── div() Your panel
├── Destination label Reads href from the link attributes
└── Remove link button Runs Unset through the editor

Add the following imports and replace link_views in the example above. Read the destination from link.attrs and use link.editor to remove the mark.

use gpdoc_plugin_link::{Unset, link_popover};
use gpui::{div, prelude::*};
fn link_views() -> [ViewPlugin; 3] {
let popover = link_popover(|link, _, _| {
let href = link.attrs.get("href").and_then(|value| value.as_str());
let label = href.unwrap_or("No destination").to_owned();
let remove = div()
.id("remove-link")
.child("Remove link")
.on_click(move |_, window, cx| {
link.editor.update(cx, |view, cx| {
if let Err(error) = view.run(Unset, window, cx) {
eprintln!("Could not remove link: {error}");
}
});
});
div().child(label).child(remove)
});
[link_format(), link_activation(), popover]
}

Place the caret inside the address from the example above. The panel shows https://example.com. Click Remove link to dismiss the panel and keep the original words.

Pass a LinkOptions value to link(options).

Detect an address when the user types a following space. When enabled, the link mark also extends to text typed at its end. Boolean, default true.

let links = link(LinkOptions { autolink: false, ..Default::default() });

Recognize Markdown such as [Quickstart](/docs/quickstart/) during typing and paste. Boolean, default false.

let links = link(LinkOptions { markdown_links: true, ..Default::default() });

When the user selects text and pastes one URL, keep the selected words as the link label. Boolean, default true.

let links = link(LinkOptions { link_on_paste: false, ..Default::default() });

Supply the scheme when a detected domain has none. String, default "http".

let links = link(LinkOptions { default_protocol: "https".into(), ..Default::default() });

Allow additional schemes. The default is an empty Vec<String>. Built-in schemes are http, https, ftp, ftps, mailto, tel, callto, sms, cid, and xmpp.

let links = link(LinkOptions { protocols: vec!["myapp".into()], ..Default::default() });

Link attributes accept strings or null. On a new mark, href, class, and title default to null, target to "_blank", and rel to "noopener noreferrer nofollow". editor.run returns Result<CommandOutcome, Error>. Set and toggle return NotApplicable for a disallowed scheme.

Apply a link to selected words or update their existing link. Pass a relative path or an allowed URI in href.

editor.run(gpdoc_plugin_link::Set::new("/docs/quickstart/"))?;

Remove the link if all eligible text matches the supplied attributes. Otherwise apply the link. With a caret inside a link, the command removes the contiguous link.

editor.run(gpdoc_plugin_link::Toggle::new("/docs/quickstart/"))?;

Remove the link but keep the label. At a caret inside a link, the command clears the entire contiguous link.

editor.run(gpdoc_plugin_link::Unset)?;