1
0

Merge pull request #4 from Willyfrog/links

Add link command
This commit is contained in:
Zef Hemel 2022-07-11 13:56:26 +02:00 committed by GitHub
commit a1d89a9234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -221,6 +221,12 @@ functions:
path: ./text.ts:numberListifySelection path: ./text.ts:numberListifySelection
command: command:
name: "Text: Number Listify Selection" name: "Text: Number Listify Selection"
linkSelection:
path: ./text.ts:linkSelection
command:
name: "Text: Link Selection"
key: "Ctrl-Shift-k"
mac: "Cmd-Shift-k"
bold: bold:
path: ./text.ts:wrapSelection path: ./text.ts:wrapSelection
command: command:

View File

@ -56,6 +56,25 @@ export async function numberListifySelection() {
await replaceRange(from, selection.to, text); 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) { export function wrapSelection(cmdDef: any) {
return insertMarker(cmdDef.wrapper); return insertMarker(cmdDef.wrapper);
} }