Skip to content
Merged
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
25 changes: 13 additions & 12 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 codeText = 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: '',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first token is processed below; start empty to avoid indexing it twice.

path: path,
indexKey: indexKey,
};
Expand All @@ -280,16 +282,17 @@ export function genIndex(path, content = '', router, depth, indexKey) {
// @ts-expect-error
token.text = getListData(token);

// @ts-expect-error
index[slug].body += '\n' + markdownToTxt(token.text || '');
index[slug].body +=
'\n' +
(codeText ?? markdownToTxt(/** @type {any} */ (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 =
codeText ?? markdownToTxt(/** @type {any} */ (token).text || '');
}

index[slug].path = path;
Expand Down Expand Up @@ -337,6 +340,11 @@ export function search(query) {
const postUrl = post.slug || '';

if (postTitle) {
handlePostTitle = escapeHtml(ignoreDiacriticalMarks(postTitle));
handlePostContent = postContent
? escapeHtml(ignoreDiacriticalMarks(postContent))
: postContent;

keywords.forEach(keyword => {
// From https://github.com/sindresorhus/escape-string-regexp
const regEx = new RegExp(
Expand All @@ -346,13 +354,6 @@ export function search(query) {
),
'gi',
);
handlePostTitle = postTitle
? escapeHtml(ignoreDiacriticalMarks(postTitle))
: postTitle;
handlePostContent = postContent
? escapeHtml(ignoreDiacriticalMarks(postContent))
: postContent;

const indexTitle = postTitle ? handlePostTitle.search(regEx) : -1;
let indexContent = postContent ? handlePostContent.search(regEx) : -1;

Expand Down
37 changes: 37 additions & 0 deletions test/e2e/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,43 @@ 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 />',
);
const resultText = await page
.locator('.results-panel .content')
.textContent();
expect(resultText?.match(/<customwidget \/>/g)).toHaveLength(1);
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