Skip to content

Commit bc37268

Browse files
authored
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
1 parent abda30d commit bc37268

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/plugins/search/search.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function genIndex(path, content = '', router, depth) {
8585
let slug;
8686
let title = '';
8787

88-
tokens.forEach(token => {
88+
tokens.forEach(function(token, tokenIndex) {
8989
if (token.type === 'heading' && token.depth <= depth) {
9090
const { str, config } = getAndRemoveConfig(token.text);
9191

@@ -106,6 +106,15 @@ export function genIndex(path, content = '', router, depth) {
106106

107107
index[slug] = { slug, title: title, body: '' };
108108
} else {
109+
if (tokenIndex === 0) {
110+
slug = router.toURL(path);
111+
index[slug] = {
112+
slug,
113+
title: path !== '/' ? path.slice(1) : 'Home Page',
114+
body: token.text || '',
115+
};
116+
}
117+
109118
if (!slug) {
110119
return;
111120
}

test/e2e/search.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,36 @@ describe('Search Plugin Tests', function() {
124124
await page.fill('input[type=search]', 'estáticos');
125125
await expect(page).toEqualText('.results-panel h2', 'Que es');
126126
});
127+
128+
test('search when there is no title', async () => {
129+
const docsifyInitConfig = {
130+
markdown: {
131+
homepage: `
132+
This is some description. We assume autoHeader added the # Title. A long paragraph.
133+
`,
134+
sidebar: `
135+
- [Changelog](changelog)
136+
`,
137+
},
138+
routes: {
139+
'/changelog.md': `
140+
feat: Support search when there is no title
141+
142+
## Changelog Title
143+
144+
hello, this is a changelog
145+
`,
146+
},
147+
scriptURLs: ['/lib/plugins/search.min.js'],
148+
};
149+
await docsifyInit(docsifyInitConfig);
150+
await page.fill('input[type=search]', 'paragraph');
151+
await expect(page).toEqualText('.results-panel h2', 'Home Page');
152+
await page.click('.clear-button');
153+
await page.fill('input[type=search]', 'Support');
154+
await expect(page).toEqualText('.results-panel h2', 'changelog');
155+
await page.click('.clear-button');
156+
await page.fill('input[type=search]', 'hello');
157+
await expect(page).toEqualText('.results-panel h2', 'Changelog Title');
158+
});
127159
});

0 commit comments

Comments
 (0)