Skip to content

Commit 506287f

Browse files
committed
updated tests and logic
1 parent bce16f4 commit 506287f

File tree

2 files changed

+425
-328
lines changed

2 files changed

+425
-328
lines changed

src/features/commands.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,16 +1274,15 @@ Authentication is a critical part of most web applications. Here are some resour
12741274
- [TheCopenhagenBook](https://thecopenhagenbook.com/)
12751275
- [OWASP Auth Cheatsheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)
12761276
- [OWASP Session Cheatsheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)
1277-
- [Lucia](https://lucia-auth.com/)
12781277
`,
12791278
inline: true,
12801279
},
12811280
{
12821281
name: "Authentication Libraries",
12831282
value: `
1283+
- [Lucia](https://lucia-auth.com/)
12841284
- [Auth.js](https://authjs.dev/)
12851285
- [Passport](http://www.passportjs.org/)
1286-
- [Better Auth](https://www.better-auth.com/)
12871286
`,
12881287
inline: true,
12891288
},
@@ -1331,15 +1330,48 @@ create-react-app is deprecated and no longer recommended for use. It is not main
13311330
];
13321331

13331332
//Regex to check commands inside codeblocks
1334-
const shouldTriggerCommand = (
1333+
// Keep this helper function as it is correct
1334+
const escapeRegex = (string: string): string => {
1335+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1336+
};
1337+
1338+
// Replace the old `shouldTriggerCommand` and remove `removeBacktickContent`
1339+
export const shouldTriggerCommand = (
13351340
content: string,
13361341
commandWord: string,
13371342
): boolean => {
1343+
// A command word must exist to trigger a command.
1344+
if (!commandWord) {
1345+
return false;
1346+
}
1347+
1348+
// This simple parsing strategy correctly removes content inside code blocks.
1349+
// 1. Split by ```. Even-indexed parts are outside code blocks.
1350+
const sanitizedContent = content
1351+
.replace(/\\```/g, "\uE001") // Placeholder for escaped ```
1352+
.replace(/\\`/g, "\uE000"); // Placeholder for escaped `
1353+
1354+
const partsOutsideTripleBackticks = sanitizedContent
1355+
.split("```")
1356+
.filter((_, i) => i % 2 === 0);
1357+
1358+
const partsOutsideAllBackticks = partsOutsideTripleBackticks.flatMap((part) =>
1359+
part.split("`").filter((_, i) => i % 2 === 0),
1360+
);
1361+
1362+
const processedContent = partsOutsideAllBackticks.join("");
1363+
1364+
// Restore the literal backticks from the placeholders.
1365+
const finalContent = processedContent
1366+
.replace(/\uE001/g, "```")
1367+
.replace(/\uE000/g, "`");
1368+
13381369
const commandRegex = new RegExp(
1339-
`\\b${commandWord}\\b(?=[^\\\`]*(?:\\\`[^\\\`]*\\\`[^\\\`]*)*$)`,
1370+
`(?<!\\w)${escapeRegex(commandWord)}(?!\\w)`,
13401371
"i",
13411372
);
1342-
return commandRegex.test(content);
1373+
1374+
return commandRegex.test(finalContent);
13431375
};
13441376

13451377
const createCommandsMessage = () => {

0 commit comments

Comments
 (0)