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 <koy@ko8e24.top>
This commit is contained in:
沈唁 2020-11-08 16:48:43 +08:00 committed by GitHub
parent 58fbca00eb
commit a2ebb2192a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -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(/<!-- {docsify-ignore} -->/, '')
.replace(/{docsify-ignore}/, '')
.replace(/<!-- {docsify-ignore-all} -->/, '')
.replace(/{docsify-ignore-all}/, '')
.trim();
}
index[slug] = { slug, title: title, body: '' };
} else {
if (!slug) {
return;

View File

@ -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 <!-- {docsify-ignore} -->
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');
});
});