Files
RuneStone/src/lib/editor/linkCommands.ts
T

16 lines
941 B
TypeScript

import { replacement, type TextSelection } from "./markdownCommands.ts";
export function externalLinkText(source: string, selection: TextSelection) {
const label = source.slice(selection.from, selection.to).replace(/[\\[\]]/g, "\\$&").replace(/\r?\n/g, " ");
// Select the empty URL when a label exists. Otherwise start in the label.
const cursor = label ? label.length + 3 : 1;
return replacement(selection, `[${label}]()`, cursor, cursor);
}
export function workspaceLinkText(source: string, selection: TextSelection, path: string) {
const label = source.slice(selection.from, selection.to).replace(/[\[\]|]/g, "").replace(/\s+/g, " ").trim();
// Root paths avoid a same-name note in the current folder. Encode syntax characters.
const target = "/" + path.replaceAll("\\", "/").replace(/^\/+/, "").split("/").map(encodeURIComponent).join("/");
return replacement(selection, `[[${target}${label ? "|" + label : ""}]]`);
}