Skip to content

Commit f69b76a

Browse files
committed
fix: change link and code block logic
1 parent 1d1807c commit f69b76a

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

custom/MarkdownEditor.vue

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,22 +547,41 @@ const applyFormat = (type: string) => {
547547
const handleCodeBlock = () => {
548548
const trimmed = selectedText.trim();
549549
const match = trimmed.match(/^(`{3,})[^\n]*\n([\s\S]*)\n\1$/);
550+
550551
if (match) {
551552
const content = match[2];
552553
applyEdits('unwrap-code', [{ range: selection, text: content, forceMoveMarkers: true }]);
553554
} else {
554555
const fence = fenceForCodeBlock(selectedText);
555-
applyEdits('wrap-code', [{ range: selection, text: `\n${fence}\n${selectedText}\n${fence}\n`, forceMoveMarkers: true }]);
556+
const insertText = `\n${fence}\n${selectedText}\n${fence}\n`;
557+
558+
applyEdits('wrap-code', [{ range: selection, text: insertText, forceMoveMarkers: true }]);
559+
560+
const startLine = selection.startLineNumber;
561+
const addedLines = insertText.split('\n').length - 1;
562+
const endLine = startLine + addedLines;
563+
564+
editor!.setSelection(new monaco.Selection(startLine, 1, endLine, model!.getLineMaxColumn(endLine)));
556565
}
557566
};
558567
559568
const handleLink = () => {
560-
const match = selectedText.match(/^\[(.*?)\]\(.*?\)$/);
569+
const trimmed = selectedText.trim();
570+
const match = trimmed.match(/^\[(.*?)\]\(.*?\)$/);
571+
561572
if (match) {
562573
applyEdits('unlink', [{ range: selection, text: match[1], forceMoveMarkers: true }]);
563574
} else {
564-
const text = selectedText || 'text';
565-
applyEdits('insert-link', [{ range: selection, text: `[${escapeMarkdownLinkText(text)}](url)`, forceMoveMarkers: true }]);
575+
const textToInsert = selectedText || '';
576+
const newText = `[${textToInsert}](url)`;
577+
578+
applyEdits('insert-link', [{ range: selection, text: newText, forceMoveMarkers: true }]);
579+
580+
const line = selection.startLineNumber;
581+
const startCol = selection.startColumn;
582+
const endCol = startCol + newText.length;
583+
584+
editor!.setSelection(new monaco.Selection(line, startCol, line, endCol));
566585
}
567586
};
568587

0 commit comments

Comments
 (0)