From a2ebb2192ac73211a8924111d736df9574abf61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=94=81?= <52o@qq52o.cn> Date: Sun, 8 Nov 2020 16:48:43 +0800 Subject: [PATCH] fix: search titles containing ignored characters (#1395) * fix: search titles containing ignored characters * fix * add default value * add test * fix test Co-authored-by: Koy --- src/plugins/search/search.js | 12 +++++++++++- test/e2e/search.test.js | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 5c7f38bca..aba15d0b7 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -83,6 +83,7 @@ export function genIndex(path, content = '', router, depth) { const slugify = window.Docsify.slugify; const index = {}; let slug; + let title = ''; tokens.forEach(token => { if (token.type === 'heading' && token.depth <= depth) { @@ -94,7 +95,16 @@ export function genIndex(path, content = '', router, depth) { slug = router.toURL(path, { id: slugify(escapeHtml(token.text)) }); } - index[slug] = { slug, title: str, body: '' }; + if (str) { + title = str + .replace(//, '') + .replace(/{docsify-ignore}/, '') + .replace(//, '') + .replace(/{docsify-ignore-all}/, '') + .trim(); + } + + index[slug] = { slug, title: title, body: '' }; } else { if (!slug) { return; diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js index 3866b4250..a74f752c0 100644 --- a/test/e2e/search.test.js +++ b/test/e2e/search.test.js @@ -35,4 +35,42 @@ describe('Search Plugin Tests', function() { await page.fill('input[type=search]', 'test'); await expect(page).toEqualText('.results-panel h2', 'Test Page'); }); + + test('search ignore title', async () => { + const docsifyInitConfig = { + markdown: { + homepage: ` + # Hello World + + This is the homepage. + `, + sidebar: ` + - [Home page](/) + - [GitHub Pages](github) + `, + }, + routes: { + '/github.md': ` + # GitHub Pages + + This is the GitHub Pages. + + ## GitHub Pages ignore1 + + There're three places to populate your docs for your Github repository1. + + ## GitHub Pages ignore2 {docsify-ignore} + + There're three places to populate your docs for your Github repository2. + `, + }, + scriptURLs: ['/lib/plugins/search.min.js'], + }; + await docsifyInit(docsifyInitConfig); + await page.fill('input[type=search]', 'repository1'); + await expect(page).toEqualText('.results-panel h2', 'GitHub Pages ignore1'); + await page.click('.clear-button'); + await page.fill('input[type=search]', 'repository2'); + await expect(page).toEqualText('.results-panel h2', 'GitHub Pages ignore2'); + }); });