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.
Try it
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-linkExample
Section titled “Example”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.
Compose a link popover
Section titled “Compose a link popover”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.
Anatomy
Section titled “Anatomy”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 editorShow the destination and remove the link
Section titled “Show the destination and remove the link”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.
Settings
Section titled “Settings”Pass a LinkOptions value to link(options).
autolink
Section titled “autolink”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() });markdown_links
Section titled “markdown_links”Recognize Markdown such as [Quickstart](/docs/quickstart/) during typing and paste. Boolean, default false.
let links = link(LinkOptions { markdown_links: true, ..Default::default() });link_on_paste
Section titled “link_on_paste”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() });default_protocol
Section titled “default_protocol”Supply the scheme when a detected domain has none. String, default "http".
let links = link(LinkOptions { default_protocol: "https".into(), ..Default::default() });protocols
Section titled “protocols”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() });Commands
Section titled “Commands”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.
setLink
Section titled “setLink”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/"))?;toggleLink
Section titled “toggleLink”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/"))?;unsetLink
Section titled “unsetLink”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)?;