mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-09 16:21:15 +00:00
fix(matrix): ignore escaped backticks in mention masking
This commit is contained in:
@@ -219,6 +219,17 @@ describe("markdownToMatrixHtml", () => {
|
||||
expect(result.mentions).toEqual({});
|
||||
});
|
||||
|
||||
it("keeps escaped mentions literal after escaped backticks", async () => {
|
||||
const result = await renderMarkdownToMatrixHtmlWithMentions({
|
||||
markdown: "\\`literal then \\@alice:example.org",
|
||||
client: createMentionClient(),
|
||||
});
|
||||
|
||||
expect(result.html).toContain("`literal then @alice:example.org");
|
||||
expect(result.html).not.toContain("matrix.to");
|
||||
expect(result.mentions).toEqual({});
|
||||
});
|
||||
|
||||
it("restores escaped mentions in markdown link labels without linking them", async () => {
|
||||
const result = await renderMarkdownToMatrixHtmlWithMentions({
|
||||
markdown: "[\\@alice:example.org](https://example.com)",
|
||||
|
||||
@@ -68,7 +68,7 @@ function maskEscapedMentions(markdown: string): string {
|
||||
let codeFenceLength = 0;
|
||||
|
||||
while (idx < markdown.length) {
|
||||
if (markdown[idx] === "`") {
|
||||
if (markdown[idx] === "`" && !isMarkdownEscaped(markdown, idx)) {
|
||||
let runLength = 1;
|
||||
while (markdown[idx + runLength] === "`") {
|
||||
runLength += 1;
|
||||
@@ -94,6 +94,16 @@ function maskEscapedMentions(markdown: string): string {
|
||||
return masked;
|
||||
}
|
||||
|
||||
function isMarkdownEscaped(markdown: string, idx: number): boolean {
|
||||
let slashCount = 0;
|
||||
let cursor = idx - 1;
|
||||
while (cursor >= 0 && markdown[cursor] === "\\") {
|
||||
slashCount += 1;
|
||||
cursor -= 1;
|
||||
}
|
||||
return slashCount % 2 === 1;
|
||||
}
|
||||
|
||||
function restoreEscapedMentions(text: string): string {
|
||||
return text.replaceAll(ESCAPED_MENTION_SENTINEL, "@");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user