Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support search when there is no title #1519

Merged
merged 4 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});