Skip to content

Commit

Permalink
fix(plugin-api-docgen): search index should include generated API doc (
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter authored Dec 27, 2024
1 parent e853b4e commit 3a00050
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
7 changes: 7 additions & 0 deletions e2e/tests/api-docgen.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, test } from '@playwright/test';
import path from 'node:path';
import { getPort, killProcess, runDevCommand } from '../utils/runCommands';
import { searchInPage } from '../utils/search';

const fixtureDir = path.resolve(__dirname, '../fixtures');

Expand Down Expand Up @@ -47,4 +48,10 @@ test.describe('api-docgen test', async () => {
expect(tableContent).toContain('-');
expect(tableContent).toContain("'default'");
});

test('search index should include api-docgen result', async ({ page }) => {
await page.goto(`http://localhost:${appPort}`);
const suggestItems = await searchInPage(page, 'disabled');
expect(suggestItems.length).toBe(1);
});
});
26 changes: 26 additions & 0 deletions e2e/utils/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Page } from '@playwright/test';
import assert from 'node:assert';

async function getSearchButton(page: Page) {
const searchButton = await page.$('.rspress-nav-search-button');
return searchButton;
}

/**
* @returns suggestItems domList
*/
export async function searchInPage(page: Page, searchText: string) {
const searchButton = await getSearchButton(page);
assert(searchButton);

await searchButton.click();

const searchInput = await page.$('.rspress-search-panel-input');
assert(searchInput);
const isEditable = await searchInput.isEditable();
assert(isEditable);
await searchInput.focus();
await page.keyboard.type(searchText);
await page.waitForTimeout(500);
return await page.$$('.rspress-search-suggest-item');
}
5 changes: 3 additions & 2 deletions packages/plugin-api-docgen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ export function pluginApiDocgen(options?: PluginOptions): RspressPlugin {
return;
}
while (matchResult !== null) {
const [matchContent, moduleName] = matchResult;
const matchContent = matchResult[0];
const moduleName = matchResult[2] ?? matchResult[5] ?? '';
const apiDoc =
apiDocMap[moduleName] || apiDocMap[`${moduleName}-${lang}`];
apiDocMap[moduleName] ?? apiDocMap[`${moduleName}-${lang}`] ?? '';
content = content.replace(matchContent, apiDoc);
matchResult = new RegExp(apiCompRegExp).exec(content);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/theme-default/src/components/Search/SearchPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,10 @@ export function SearchPanel({ focused, setFocused }: SearchPanelProps) {
<SvgWrapper icon={SearchSvg} />
</label>
<input
className={styles.input}
className={`rspress-search-panel-input ${styles.input}`}
ref={searchInputRef}
placeholder={searchPlaceholderText}
aria-label="Search"
aria-label="SearchPanelInput"
autoComplete="off"
autoFocus
onChange={e => handleQueryChange(e.target.value)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function SuggestItem({
return (
<li
key={suggestion.link}
className={`${styles.suggestItem} ${isCurrent ? styles.current : ''}`}
className={`rspress-search-suggest-item ${styles.suggestItem} ${isCurrent ? styles.current : ''}`}
onMouseEnter={setCurrentSuggestionIndex}
onMouseMove={onMouseMove}
ref={selfRef}
Expand Down

0 comments on commit 3a00050

Please sign in to comment.