From e30a5f6dbb61eac54f9d70c1e513333ebd6e651c Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 13 Oct 2020 18:30:26 +0800 Subject: [PATCH 1/5] fix: search titles containing ignored characters --- src/plugins/search/component.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/plugins/search/component.js b/src/plugins/search/component.js index 388de395d..03a8e1e95 100644 --- a/src/plugins/search/component.js +++ b/src/plugins/search/component.js @@ -148,6 +148,25 @@ function doSearch(value) { let html = ''; matchs.forEach(post => { + if (/<!-- {docsify-ignore} -->/g.test(post.title)) { + post.title = post.title.replace('<!-- {docsify-ignore} -->', ''); + } + + if (/{docsify-ignore}/g.test(post.title)) { + post.title = post.title.replace('{docsify-ignore}', ''); + } + + if (/<!-- {docsify-ignore-all} -->/g.test(post.title)) { + post.title = post.title.replace( + '<!-- {docsify-ignore-all} -->', + '' + ); + } + + if (/{docsify-ignore-all}/g.test(post.title)) { + post.title = post.title.replace('{docsify-ignore-all}', ''); + } + html += `

${post.title}

From 8fb9165a829d63ad2e11b3dcbee3389ef375b523 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 14 Oct 2020 10:40:21 +0800 Subject: [PATCH 2/5] fix --- src/plugins/search/component.js | 19 ------------------- src/plugins/search/search.js | 12 +++++++++++- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/plugins/search/component.js b/src/plugins/search/component.js index 03a8e1e95..388de395d 100644 --- a/src/plugins/search/component.js +++ b/src/plugins/search/component.js @@ -148,25 +148,6 @@ function doSearch(value) { let html = ''; matchs.forEach(post => { - if (/<!-- {docsify-ignore} -->/g.test(post.title)) { - post.title = post.title.replace('<!-- {docsify-ignore} -->', ''); - } - - if (/{docsify-ignore}/g.test(post.title)) { - post.title = post.title.replace('{docsify-ignore}', ''); - } - - if (/<!-- {docsify-ignore-all} -->/g.test(post.title)) { - post.title = post.title.replace( - '<!-- {docsify-ignore-all} -->', - '' - ); - } - - if (/{docsify-ignore-all}/g.test(post.title)) { - post.title = post.title.replace('{docsify-ignore-all}', ''); - } - html += `

${post.title}

diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 18fb88ea4..8ded7f88d 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; From 7e5e2dc0941e9032f759580305c5c2358d73a13d Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 14 Oct 2020 11:10:03 +0800 Subject: [PATCH 3/5] add default value --- src/plugins/search/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 8ded7f88d..8c87d2799 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -83,7 +83,7 @@ export function genIndex(path, content = '', router, depth) { const slugify = window.Docsify.slugify; const index = {}; let slug; - let title; + let title = ''; tokens.forEach(token => { if (token.type === 'heading' && token.depth <= depth) { From a17c0dcc8eea4c11764097f1055a7b9e6cfa5dad Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 15 Oct 2020 18:06:13 +0800 Subject: [PATCH 4/5] add test --- test/e2e/search.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js index 3866b4250..034e0c2b8 100644 --- a/test/e2e/search.test.js +++ b/test/e2e/search.test.js @@ -35,4 +35,27 @@ 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. + + ## Test ignore title + + This is the test ignore title. + `, + }, + scriptURLs: ['/lib/plugins/search.min.js'], + styleURLs: ['/lib/themes/vue.css'], + }; + await docsifyInit(docsifyInitConfig); + await page.fill('input[type=search]', 'Test ignore title'); + expect(await page.innerText('.results-panel h2')).toEqual( + 'Test ignore title' + ); + }); }); From a59b4170ea384c16aeb570030dad83102db63313 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 15 Oct 2020 19:02:13 +0800 Subject: [PATCH 5/5] fix test --- test/e2e/search.test.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js index 034e0c2b8..a74f752c0 100644 --- a/test/e2e/search.test.js +++ b/test/e2e/search.test.js @@ -40,22 +40,37 @@ describe('Search Plugin Tests', function() { const docsifyInitConfig = { markdown: { homepage: ` - # Hello World + # Hello World + + This is the homepage. + `, + sidebar: ` + - [Home page](/) + - [GitHub Pages](github) + `, + }, + routes: { + '/github.md': ` + # GitHub Pages - This is the homepage. + This is the GitHub Pages. - ## Test ignore title + ## GitHub Pages ignore1 - This is the test ignore title. - `, + 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'], - styleURLs: ['/lib/themes/vue.css'], }; await docsifyInit(docsifyInitConfig); - await page.fill('input[type=search]', 'Test ignore title'); - expect(await page.innerText('.results-panel h2')).toEqual( - 'Test ignore title' - ); + 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'); }); });