mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
fix(telegram): preserve literal backticks in inbound code (#130800)
This commit is contained in:
committed by
GitHub
parent
b4d6aff098
commit
1d28e156fc
@@ -152,6 +152,43 @@ describe("joinTelegramTextParts", () => {
|
||||
expect(parsed.text).toBe(result.text);
|
||||
expect(parsed.links.map((link) => link.href)).toEqual([messageUrl, captionUrl]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"npm test",
|
||||
"a`b",
|
||||
"`npm",
|
||||
"npm`",
|
||||
"`npm`",
|
||||
"``npm```",
|
||||
"`",
|
||||
" npm",
|
||||
"npm ",
|
||||
" npm ",
|
||||
" ",
|
||||
" ",
|
||||
])("preserves literal inline code %j from joined text and captions", (code) => {
|
||||
const prefix = "😀 Code: ";
|
||||
const text = `${prefix}${code} end`;
|
||||
const entities: MessageEntity[] = [
|
||||
{ type: "code", offset: prefix.length, length: code.length },
|
||||
];
|
||||
const result = joinTelegramTextParts(
|
||||
[
|
||||
asTelegramMessage({ text, entities }),
|
||||
asTelegramMessage({ caption: text, caption_entities: entities }),
|
||||
],
|
||||
"\n",
|
||||
);
|
||||
|
||||
const parsed = markdownToIR(renderTelegramTextEntities(result.text, result.entities));
|
||||
|
||||
expect(parsed.text).toBe(result.text);
|
||||
expect(
|
||||
parsed.styles
|
||||
.filter((span) => span.style === "code")
|
||||
.map((span) => parsed.text.slice(span.start, span.end)),
|
||||
).toEqual([code, code]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("renderTelegramTextEntities quoted blocks", () => {
|
||||
|
||||
@@ -61,10 +61,9 @@ function longestBacktickRun(text: string): number {
|
||||
|
||||
function markdownInlineCodeDelimiters(content: string): [string, string] {
|
||||
const delimiter = "`".repeat(longestBacktickRun(content) + 1);
|
||||
if (content.startsWith(" ") || content.endsWith(" ")) {
|
||||
return [`${delimiter} `, ` ${delimiter}`];
|
||||
}
|
||||
return [delimiter, delimiter];
|
||||
// Padding keeps literal edge backticks separate from the Markdown delimiters.
|
||||
const padding = /^[ `]|[ `]$/u.test(content) ? " " : "";
|
||||
return [`${delimiter}${padding}`, `${padding}${delimiter}`];
|
||||
}
|
||||
|
||||
function markdownPreAffixes(
|
||||
|
||||
Reference in New Issue
Block a user