mirror of https://github.com/openclaw/openclaw.git
fix(matrix): ignore escaped backticks in mention masking
This commit is contained in:
parent
be52594766
commit
5c331687ff
|
|
@ -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, "@");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue