Skip to content

Commit 0524b83

Browse files
authored
fix: react documentation commands can not be in sentence (#355)
1 parent 2669c0d commit 0524b83

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/features/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import cooldown from "./cooldown";
1111
import { ChannelHandlers } from "../types";
1212
import { isStaff } from "../helpers/discord";
1313
import {
14+
extractSearchKey,
1415
getReactDocsContent,
1516
getReactDocsSearchKey,
1617
} from "../helpers/react-docs";
@@ -430,8 +431,7 @@ Here's an article explaining the difference between the two: https://goshakkk.na
430431
help: "Allows you to search the React docs, usage: !docs useState",
431432
category: "Web",
432433
handleMessage: async (msg) => {
433-
const [, search] = msg.content.split(" ");
434-
434+
const search = extractSearchKey(msg.content);
435435
const searchKey = getReactDocsSearchKey(search);
436436

437437
if (!searchKey) {

src/helpers/react-docs.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, test } from "vitest";
2+
import { extractSearchKey, getReactDocsSearchKey } from "./react-docs";
3+
4+
describe("React documentation command", () => {
5+
test.each([
6+
["Hello world, please look into !docs useState Hello", "react/useState"],
7+
["!docs react/useState", "react/useState"],
8+
["Foo !docs react/useState bar", "react/useState"],
9+
["!react-docs react/useState", "react/useState"],
10+
["Foo bar, !react-docs , useState", undefined],
11+
[
12+
"Hello world, check react dom docs, !docs createPortal hello",
13+
"react-dom/createPortal",
14+
],
15+
["!docs react-dom/createPortal", "react-dom/createPortal"],
16+
])(`should match the command in the message`, (message, expected) => {
17+
const search = extractSearchKey(message);
18+
const searchKey = getReactDocsSearchKey(search);
19+
20+
expect(searchKey).toBe(expected);
21+
});
22+
});

src/helpers/react-docs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { gitHubReadToken } from "./env";
44
const LOOKUP_REGEX = /<Intro>\s*(.*?)\s*<\/Intro>/gs;
55
const LINK_REGEX = /\[([^\]]+)\]\((?!https?:\/\/)([^)]+)\)/g;
66

7+
const EXTRACT_SEARCH_KEY_REGEX = /(?<=!(docs|react-docs)\s)[^\s]+/;
8+
79
const BASE_URL =
810
"https://api.github.com/repos/reactjs/react.dev/contents/src/content/reference/";
911

@@ -45,6 +47,11 @@ export const getReactDocsSearchKey = (search: string) => {
4547
});
4648
};
4749

50+
export const extractSearchKey = (search: string) => {
51+
const matches = search.match(EXTRACT_SEARCH_KEY_REGEX);
52+
return matches ? matches[0] : "";
53+
};
54+
4855
const processReactDocumentation = (content: string) => {
4956
const patchedContentLinks = content.replace(LINK_REGEX, (_, text, link) => {
5057
return `[${text}](https://react.dev${link})`;

0 commit comments

Comments
 (0)