Files
plainleaf/client/codemirror/smart_quotes.ts
T
Matouš Jan FialkaandGitHub b83c1e03d2 Fix Markdown table rendering and remove deprecated command button remnants (#1879)
* Fix table row parser

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

* Remove all `{[command button]}` remnants

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

* Add tests

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

* Fix extended task rendering in case of following EoL

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>

---------

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
2026-03-12 13:46:48 +01:00

111 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { KeyBinding } from "@codemirror/view";
import { syntaxTree } from "@codemirror/language";
import { EditorSelection } from "@codemirror/state";
import type { Client } from "../client.ts";
import type { SmartQuotesConfig } from "../../plug-api/types/config.ts";
const straightQuoteContexts = [
"CommentBlock",
"CodeBlock",
"CodeText",
"FencedCode",
"InlineCode",
"FrontMatterCode",
"Attribute",
"LuaDirective",
];
// TODO: Add support for selection (put quotes around or create blockquote block?)
function keyBindingForQuote(
originalQuote: string,
left: string,
right: string,
): KeyBinding {
return {
any: (target, event): boolean => {
// Moving this check here rather than using the regular "key" property because
// for some reason the "ä" key is not recognized as a quote key by CodeMirror.
if (event.key !== originalQuote) {
return false;
}
const cursorPos = target.state.selection.main.from;
const chBefore = target.state.sliceDoc(cursorPos - 1, cursorPos);
// Figure out the context, if in some sort of code/comment fragment don't be smart
let node = syntaxTree(target.state).resolveInner(cursorPos);
while (node) {
if (
straightQuoteContexts.find((sqc) => sqc.startsWith(node.type.name))
) {
return false;
}
if (node.parent) {
node = node.parent;
} else {
break;
}
}
// Ok, still here, let's use a smart quote
const changes = target.state.changeByRange((range) => {
if (!range.empty) {
return {
changes: [
{ insert: left, from: range.from },
{ insert: right, from: range.to },
],
range: EditorSelection.range(
range.anchor + left.length,
range.head + left.length,
),
};
} else {
const quote =
/\W/.exec(chBefore) && !/[!?,.\-=“]/.exec(chBefore) ? left : right;
return {
changes: {
insert: quote,
from: cursorPos,
},
range: EditorSelection.cursor(range.anchor + quote.length),
};
}
});
target.dispatch(changes);
return true;
},
};
}
export function createSmartQuoteKeyBindings(client: Client): KeyBinding[] {
const smartQuotes = client.config.get<SmartQuotesConfig>("smartQuotes", {});
if (smartQuotes.enabled === false) {
return [];
}
let doubleLeft = "“";
let doubleRight = "”";
let singleLeft = "‘";
let singleRight = "’";
if (typeof smartQuotes.double?.left === "string") {
doubleLeft = smartQuotes.double!.left;
}
if (typeof smartQuotes.double?.right === "string") {
doubleRight = smartQuotes.double!.right;
}
if (typeof smartQuotes.single?.left === "string") {
singleLeft = smartQuotes.single!.left;
}
if (typeof smartQuotes.single?.right === "string") {
singleRight = smartQuotes.single!.right;
}
return [
keyBindingForQuote('"', doubleLeft, doubleRight),
keyBindingForQuote("'", singleLeft, singleRight),
];
}