1
0

Add link command

This commit is contained in:
Guillermo Vaya 2022-07-10 17:51:34 +02:00
parent a29ea35937
commit 86823918cc
2 changed files with 23 additions and 0 deletions

View File

@ -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:

View File

@ -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);
}