Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,14 @@ export function genIndex(path, content = '', router, depth, indexKey) {
indexKey: indexKey,
};
} else {
const code = token.type === 'code' ? token.text : null;

if (tokenIndex === 0) {
slug = router.toURL(path);
index[slug] = {
slug,
title: path !== '/' ? path.slice(1) : 'Home Page',
body: markdownToTxt(/** @type {any} */ (token).text || ''),
body: code ?? markdownToTxt(/** @type {any} */ (token).text || ''),
path: path,
indexKey: indexKey,
};
Expand All @@ -281,15 +283,15 @@ export function genIndex(path, content = '', router, depth, indexKey) {
token.text = getListData(token);

// @ts-expect-error
index[slug].body += '\n' + markdownToTxt(token.text || '');
index[slug].body += '\n' + (code ?? markdownToTxt(token.text || ''));
} else {
// @ts-expect-error
token.text = getTableData(token);
// @ts-expect-error
token.text = getListData(token);

// @ts-expect-error
index[slug].body = markdownToTxt(token.text || '');
index[slug].body = code ?? markdownToTxt(token.text || '');
}

index[slug].path = path;
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,39 @@ console.log('Hello World');
);
});

for (const [name, markdown, title] of [
[
'after a heading',
'# Code examples\n\n```html\n<customwidget />\n```',
'Code examples',
],
[
'after a paragraph',
'# Code examples\n\nAn example:\n\n```html\n<customwidget />\n```',
'Code examples',
],
['without a heading', '```html\n<customwidget />\n```', 'Home Page'],
[
'in an indented code block',
'# Code examples\n\n <customwidget />',
'Code examples',
],
]) {
test(`searches HTML examples ${name}`, async ({ page }) => {
await docsifyInit({
markdown: { homepage: markdown },
scriptURLs: ['/dist/plugins/search.js'],
});

await page.locator('input[type=search]').fill('customwidget');
await expect(page.locator('.results-panel .title')).toHaveText(title);
await expect(page.locator('.results-panel .content')).toContainText(
'<customwidget />',
);
await expect(page.locator('.results-panel customwidget')).toHaveCount(0);
});
}

test('search result should remove file markdown and keep href attribution for files', async ({
page,
}) => {
Expand Down
Loading