From bc3726853fb2d1f9241927ea0317970ab0c8a2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=94=81?= <52o@qq52o.cn> Date: Fri, 5 Mar 2021 14:23:04 +0800 Subject: [PATCH] feat: Support search when there is no title (#1519) * feat: Support search when there is no title * test: Support search when there is no title --- src/plugins/search/search.js | 11 ++++++++++- test/e2e/search.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 07740e6df..94047c2ee 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -85,7 +85,7 @@ export function genIndex(path, content = '', router, depth) { let slug; let title = ''; - tokens.forEach(token => { + tokens.forEach(function(token, tokenIndex) { if (token.type === 'heading' && token.depth <= depth) { const { str, config } = getAndRemoveConfig(token.text); @@ -106,6 +106,15 @@ export function genIndex(path, content = '', router, depth) { index[slug] = { slug, title: title, body: '' }; } else { + if (tokenIndex === 0) { + slug = router.toURL(path); + index[slug] = { + slug, + title: path !== '/' ? path.slice(1) : 'Home Page', + body: token.text || '', + }; + } + if (!slug) { return; } diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js index 8afce5500..6478e1925 100644 --- a/test/e2e/search.test.js +++ b/test/e2e/search.test.js @@ -124,4 +124,36 @@ describe('Search Plugin Tests', function() { await page.fill('input[type=search]', 'estáticos'); await expect(page).toEqualText('.results-panel h2', 'Que es'); }); + + test('search when there is no title', async () => { + const docsifyInitConfig = { + markdown: { + homepage: ` + This is some description. We assume autoHeader added the # Title. A long paragraph. + `, + sidebar: ` + - [Changelog](changelog) + `, + }, + routes: { + '/changelog.md': ` + feat: Support search when there is no title + + ## Changelog Title + + hello, this is a changelog + `, + }, + scriptURLs: ['/lib/plugins/search.min.js'], + }; + await docsifyInit(docsifyInitConfig); + await page.fill('input[type=search]', 'paragraph'); + await expect(page).toEqualText('.results-panel h2', 'Home Page'); + await page.click('.clear-button'); + await page.fill('input[type=search]', 'Support'); + await expect(page).toEqualText('.results-panel h2', 'changelog'); + await page.click('.clear-button'); + await page.fill('input[type=search]', 'hello'); + await expect(page).toEqualText('.results-panel h2', 'Changelog Title'); + }); });