Skip to content

Commit

Permalink
🐞 fix: Corrupting content in formula block
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflyt committed Dec 19, 2023
1 parent 4213b5e commit 2f0e450
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,30 +179,35 @@ const main = async () => {
character: range.end.character - startPos.character,
},
};
// Get the code block starter "```" or "~~~"
const codeBlockStarter = state.markdown
.split(File.useCRLF ? "\r\n" : "\n")
[startPos.line - 1]!.slice(startPos.character, startPos.character + 3);
// Get only completion text before code block starter, as in Typora code block it is not possible
// to insert a new code block or end one using "```" or "~~~"
const indexOfCodeBlockStarter = subCmCompletion.text.indexOf(codeBlockStarter);
if (indexOfCodeBlockStarter !== -1) {
subCmCompletion.displayText = subCmCompletion.displayText.slice(
0,
subCmCompletion.displayText.indexOf(codeBlockStarter),
);
subCmCompletion.text = subCmCompletion.text.slice(0, indexOfCodeBlockStarter);
const textAfterCodeBlockStarter = subCmCompletion.text.slice(indexOfCodeBlockStarter);
// Reduce `subCmCompletion.range` to only include text before code block starter
const rows = textAfterCodeBlockStarter.split(File.useCRLF ? "\r\n" : "\n").length - 1;
subCmCompletion.range.end.line -= rows;
subCmCompletion.range.end.character = textAfterCodeBlockStarter
.split(File.useCRLF ? "\r\n" : "\n")
.pop()!.length;
// Get starter of CodeMirror to determine whether it is a code block, formula, etc.
let cmStarter = state.markdown.split(File.useCRLF ? "\r\n" : "\n")[startPos.line - 1];

if (cmStarter) {
if (cmStarter.startsWith("```") || cmStarter.startsWith("~~~")) {
// * Code block *
cmStarter = cmStarter.slice(0, 3);
// Get only completion text before code block starter, as in Typora code block it is not possible
// to insert a new code block or end one using "```" or "~~~"
const indexOfCodeBlockStarter = subCmCompletion.text.indexOf(cmStarter);
if (indexOfCodeBlockStarter !== -1) {
subCmCompletion.displayText = subCmCompletion.displayText.slice(
0,
subCmCompletion.displayText.indexOf(cmStarter),
);
subCmCompletion.text = subCmCompletion.text.slice(0, indexOfCodeBlockStarter);
const textAfterCodeBlockStarter = subCmCompletion.text.slice(indexOfCodeBlockStarter);
// Reduce `subCmCompletion.range` to only include text before code block starter
const rows = textAfterCodeBlockStarter.split(File.useCRLF ? "\r\n" : "\n").length - 1;
subCmCompletion.range.end.line -= rows;
subCmCompletion.range.end.character = textAfterCodeBlockStarter
.split(File.useCRLF ? "\r\n" : "\n")
.pop()!.length;
}
insertCompletionTextToCodeMirror(cm, subCmCompletion);

return;
}
}
insertCompletionTextToCodeMirror(cm, subCmCompletion);

return;
}

const focusedElem = document.querySelector(`[cid=${editor.focusCid}]`);
Expand Down

0 comments on commit 2f0e450

Please sign in to comment.