diff --git a/packages/plugs/core/core.plug.yaml b/packages/plugs/core/core.plug.yaml index fb001043..bbad1a58 100644 --- a/packages/plugs/core/core.plug.yaml +++ b/packages/plugs/core/core.plug.yaml @@ -221,6 +221,12 @@ functions: path: ./text.ts:numberListifySelection command: name: "Text: Number Listify Selection" + linkSelection: + path: ./text.ts:linkSelection + command: + name: "Text: Link Selection" + key: "Ctrl-Shift-k" + mac: "Cmd-Shift-k" bold: path: ./text.ts:wrapSelection command: diff --git a/packages/plugs/core/text.ts b/packages/plugs/core/text.ts index 3061cc3f..2f72966a 100644 --- a/packages/plugs/core/text.ts +++ b/packages/plugs/core/text.ts @@ -56,6 +56,25 @@ export async function numberListifySelection() { await replaceRange(from, selection.to, text); } +export async function linkSelection() { + const text = await getText(); + const selection = await getSelection(); + const textSelection = text.slice(selection.from, selection.to); + let linkedText = `[]()`; + let pos = 1; + if (textSelection.length > 0) { + try { + new URL(textSelection); + linkedText = `[](${textSelection})`; + } catch { + linkedText = `[${textSelection}]()`; + pos = linkedText.length - 1; + } + } + await replaceRange(selection.from, selection.to, linkedText) + await moveCursor(selection.from + pos); +} + export function wrapSelection(cmdDef: any) { return insertMarker(cmdDef.wrapper); }