From 86823918cceaf7b7d6c44ab3577c31efcc8cbab2 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Sun, 10 Jul 2022 17:51:34 +0200 Subject: [PATCH] Add link command --- packages/plugs/core/core.plug.yaml | 6 ++++++ packages/plugs/core/text.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/plugs/core/core.plug.yaml b/packages/plugs/core/core.plug.yaml index 83ff40e..5428099 100644 --- a/packages/plugs/core/core.plug.yaml +++ b/packages/plugs/core/core.plug.yaml @@ -215,6 +215,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 3061cc3..924ae7e 100644 --- a/packages/plugs/core/text.ts +++ b/packages/plugs/core/text.ts @@ -56,6 +56,23 @@ 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 = `[]()`; + if (textSelection.length > 0) { + try { + new URL(textSelection); + linkedText = `[](${textSelection})`; + } catch { + linkedText = `[${textSelection}]()`; + + } + } + await replaceRange(selection.from, selection.to, linkedText) +} + export function wrapSelection(cmdDef: any) { return insertMarker(cmdDef.wrapper); }