From 86823918cceaf7b7d6c44ab3577c31efcc8cbab2 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Sun, 10 Jul 2022 17:51:34 +0200 Subject: [PATCH 1/2] 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); } From 63bee59cf347a4739c8f9370fab073322e50307e Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Sun, 10 Jul 2022 23:30:38 +0200 Subject: [PATCH 2/2] move caret --- packages/plugs/core/text.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/plugs/core/text.ts b/packages/plugs/core/text.ts index 924ae7e..2f72966 100644 --- a/packages/plugs/core/text.ts +++ b/packages/plugs/core/text.ts @@ -61,16 +61,18 @@ export async function linkSelection() { 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) {