Skip to content

Commit 2948731

Browse files
committed
fix(search): clean markdown elements in search contents
1 parent 4ad3e36 commit 2948731

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"*.js": "eslint --fix"
3737
},
3838
"dependencies": {
39+
"markdown-to-txt": "^2.0.1",
3940
"medium-zoom": "^1.1.0",
4041
"opencollective-postinstall": "^2.0.2",
4142
"prismjs": "^1.29.0",

src/plugins/search/search.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import markdownToTxt from 'markdown-to-txt';
12
import {
23
getAndRemoveConfig,
34
getAndRemoveDocisfyIgnoreConfig,
@@ -34,6 +35,13 @@ function escapeHtml(string) {
3435
return String(string).replace(/[&<>"']/g, s => entityMap[s]);
3536
}
3637

38+
function cleanMarkdown(text) {
39+
if (text) {
40+
text = markdownToTxt(text);
41+
}
42+
return text;
43+
}
44+
3745
function getAllPaths(router) {
3846
const paths = [];
3947

@@ -226,8 +234,8 @@ export function search(query) {
226234

227235
if (matchesScore > 0) {
228236
const matchingPost = {
229-
title: handlePostTitle,
230-
content: postContent ? resultStr : '',
237+
title: cleanMarkdown(handlePostTitle),
238+
content: cleanMarkdown(postContent ? resultStr : ''),
231239
url: postUrl,
232240
score: matchesScore,
233241
};

test/e2e/search.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,23 @@ test.describe('Search Plugin Tests', () => {
232232
await page.keyboard.press('z');
233233
await expect(searchFieldElm).toBeFocused();
234234
});
235+
test('search result should remove markdown', async ({ page }) => {
236+
const docsifyInitConfig = {
237+
markdown: {
238+
homepage: `
239+
# The [mock](example.com) link
240+
There is lots of words.
241+
`,
242+
},
243+
scriptURLs: ['/dist/plugins/search.js'],
244+
};
245+
246+
const searchFieldElm = page.locator('input[type=search]');
247+
const resultsHeadingElm = page.locator('.results-panel h2');
248+
249+
await docsifyInit(docsifyInitConfig);
250+
251+
await searchFieldElm.fill('There');
252+
await expect(resultsHeadingElm).toHaveText('The mock link');
253+
});
235254
});

0 commit comments

Comments
 (0)