Skip to content

Commit 7272bfa

Browse files
xu91102sy-records
andauthored
fix(search): preserve literal code block content in index (#2816)
Co-authored-by: Luffy <lufei@docsifyjs.org>
1 parent a5519a0 commit 7272bfa

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎src/plugins/search/search.js‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,14 @@ export function genIndex(path, content = '', router, depth, indexKey) {
257257
indexKey: indexKey,
258258
};
259259
} else {
260+
const codeText = token.type === 'code' ? token.text : null;
261+
260262
if (tokenIndex === 0) {
261263
slug = router.toURL(path);
262264
index[slug] = {
263265
slug,
264266
title: path !== '/' ? path.slice(1) : 'Home Page',
265-
body: markdownToTxt(/** @type {any} */ (token).text || ''),
267+
body: '',
266268
path: path,
267269
indexKey: indexKey,
268270
};
@@ -280,16 +282,17 @@ export function genIndex(path, content = '', router, depth, indexKey) {
280282
// @ts-expect-error
281283
token.text = getListData(token);
282284

283-
// @ts-expect-error
284-
index[slug].body += '\n' + markdownToTxt(token.text || '');
285+
index[slug].body +=
286+
'\n' +
287+
(codeText ?? markdownToTxt(/** @type {any} */ (token).text || ''));
285288
} else {
286289
// @ts-expect-error
287290
token.text = getTableData(token);
288291
// @ts-expect-error
289292
token.text = getListData(token);
290293

291-
// @ts-expect-error
292-
index[slug].body = markdownToTxt(token.text || '');
294+
index[slug].body =
295+
codeText ?? markdownToTxt(/** @type {any} */ (token).text || '');
293296
}
294297

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

339342
if (postTitle) {
343+
handlePostTitle = escapeHtml(ignoreDiacriticalMarks(postTitle));
344+
handlePostContent = postContent
345+
? escapeHtml(ignoreDiacriticalMarks(postContent))
346+
: postContent;
347+
340348
keywords.forEach(keyword => {
341349
// From https://github.com/sindresorhus/escape-string-regexp
342350
const regEx = new RegExp(
@@ -346,13 +354,6 @@ export function search(query) {
346354
),
347355
'gi',
348356
);
349-
handlePostTitle = postTitle
350-
? escapeHtml(ignoreDiacriticalMarks(postTitle))
351-
: postTitle;
352-
handlePostContent = postContent
353-
? escapeHtml(ignoreDiacriticalMarks(postContent))
354-
: postContent;
355-
356357
const indexTitle = postTitle ? handlePostTitle.search(regEx) : -1;
357358
let indexContent = postContent ? handlePostContent.search(regEx) : -1;
358359

‎test/e2e/search.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,43 @@ console.log('Hello World');
524524
);
525525
});
526526

527+
for (const [name, markdown, title] of [
528+
[
529+
'after a heading',
530+
'# Code examples\n\n```html\n<customwidget />\n```',
531+
'Code examples',
532+
],
533+
[
534+
'after a paragraph',
535+
'# Code examples\n\nAn example:\n\n```html\n<customwidget />\n```',
536+
'Code examples',
537+
],
538+
['without a heading', '```html\n<customwidget />\n```', 'Home Page'],
539+
[
540+
'in an indented code block',
541+
'# Code examples\n\n <customwidget />',
542+
'Code examples',
543+
],
544+
]) {
545+
test(`searches HTML examples ${name}`, async ({ page }) => {
546+
await docsifyInit({
547+
markdown: { homepage: markdown },
548+
scriptURLs: ['/dist/plugins/search.js'],
549+
});
550+
551+
await page.locator('input[type=search]').fill('customwidget');
552+
await expect(page.locator('.results-panel .title')).toHaveText(title);
553+
await expect(page.locator('.results-panel .content')).toContainText(
554+
'<customwidget />',
555+
);
556+
const resultText = await page
557+
.locator('.results-panel .content')
558+
.textContent();
559+
expect(resultText?.match(/<customwidget \/>/g)).toHaveLength(1);
560+
await expect(page.locator('.results-panel customwidget')).toHaveCount(0);
561+
});
562+
}
563+
527564
test('search result should remove file markdown and keep href attribution for files', async ({
528565
page,
529566
}) => {

0 commit comments

Comments
 (0)