mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Do not suggest Emoji when in open code blocks (#14337)
There was a check for closed code blocks (which had both opening and closing markups), but it did not work for the case when the text ends in an open code block.
This commit is contained in:
parent
5cce1b38e4
commit
856732786f
@ -469,19 +469,22 @@ const CODE_BLOCKS_REGEX = /^( |\t).*|`[^`]+`|^```[^]*?^```|\[code\][^]*?\[\/c
|
|||||||
// |
|
// |
|
||||||
// +------- paragraphs starting with 4 spaces or tab
|
// +------- paragraphs starting with 4 spaces or tab
|
||||||
|
|
||||||
export function inCodeBlock(text, pos) {
|
const OPEN_CODE_BLOCKS_REGEX = /`[^`]+|^```[^]*?|\[code\][^]*?/gm;
|
||||||
let result = false;
|
|
||||||
|
|
||||||
let match;
|
export function inCodeBlock(text, pos) {
|
||||||
while ((match = CODE_BLOCKS_REGEX.exec(text)) !== null) {
|
let end = 0;
|
||||||
const begin = match.index;
|
for (const match of text.matchAll(CODE_BLOCKS_REGEX)) {
|
||||||
const end = match.index + match[0].length;
|
end = match.index + match[0].length;
|
||||||
if (begin <= pos && pos <= end) {
|
if (match.index <= pos && pos <= end) {
|
||||||
result = true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
// Character at position `pos` can be in a code block that is unfinished.
|
||||||
|
// To check this case, we look for any open code blocks after the last closed
|
||||||
|
// code block.
|
||||||
|
const lastOpenBlock = text.substr(end).search(OPEN_CODE_BLOCKS_REGEX);
|
||||||
|
return lastOpenBlock !== -1 && pos >= end + lastOpenBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This prevents a mini racer crash
|
// This prevents a mini racer crash
|
||||||
|
@ -251,18 +251,28 @@ discourseModule("Unit | Utilities", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("inCodeBlock", function (assert) {
|
test("inCodeBlock", function (assert) {
|
||||||
const text =
|
const texts = [
|
||||||
"000\n\n```\n111\n```\n\n000\n\n`111 111`\n\n000\n\n[code]\n111\n[/code]\n\n 111\n\t111\n\n000`000";
|
// closed code blocks
|
||||||
for (let i = 0; i < text.length; ++i) {
|
"000\n\n 111\n\n000",
|
||||||
if (text[i] === "0") {
|
"000 `111` 000",
|
||||||
assert.notOk(
|
"000\n```\n111\n```\n000",
|
||||||
inCodeBlock(text, i),
|
"000\n[code]111[/code]\n000",
|
||||||
`position ${i} is not in code block`
|
// open code blocks
|
||||||
);
|
"000\n\n 111",
|
||||||
} else if (text[i] === "1") {
|
"000 `111",
|
||||||
assert.ok(inCodeBlock(text, i), `position ${i} is in code block`);
|
"000\n```\n111",
|
||||||
|
"000\n[code]111",
|
||||||
|
// complex test
|
||||||
|
"000\n\n```\n111\n```\n\n000\n\n`111 111`\n\n000\n\n[code]\n111\n[/code]\n\n 111\n\t111\n\n000`111",
|
||||||
|
];
|
||||||
|
|
||||||
|
texts.forEach((text) => {
|
||||||
|
for (let i = 0; i < text.length; ++i) {
|
||||||
|
if (text[i] === "0" || text[i] === "1") {
|
||||||
|
assert.equal(inCodeBlock(text, i), text[i] === "1");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
skip("inCodeBlock - runs fast", function (assert) {
|
skip("inCodeBlock - runs fast", function (assert) {
|
||||||
|
Loading…
Reference in New Issue
Block a user