Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion source/Quotes.popclipext/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const styles = [
'"…"',
"'…'",
"`…`",
"```…```",
"«…»",
"《…》",
"‹…›",
Expand All @@ -18,6 +19,10 @@ const styles = [

// generate square icon
function makeIcon(style: string): string {
// Handle the case of three backticks.
if (style.startsWith("```")) {
return "[[```]]";
}
return `[[${style[0]}${style[2]}]]`;
}

Expand All @@ -43,7 +48,22 @@ const extension: Extension = {
title: styles[index],
icon: makeIcon(style),
code: (selection) => {
popclip.pasteText(style[0] + selection.text + style[2]);
// Handle the case of three backticks.
if (style.startsWith("```")) {
// Detect the longest continuous backquote sequence in the text
const backtickMatch = selection.text.match(/`+/g);
let maxBackticks = 0;
if (backtickMatch) {
maxBackticks = Math.max(...backtickMatch.map(m => m.length));
}
// If the text contains three or more backticks, use one more backtick than the longest sequence
const wrapperLength = Math.max(3, maxBackticks + 1);
const wrapper = "`".repeat(wrapperLength);
// Add line breaks before and after each line to make the backticks occupy a single line
popclip.pasteText(wrapper + "\n" + selection.text + "\n" + wrapper);
} else {
popclip.pasteText(style[0] + selection.text + style[2]);
}
},
};
});
Expand Down