Skip to content

Commit 2975d8c

Browse files
committed
feat: search ignore diacritical marks
1 parent 7c3bf98 commit 2975d8c

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/plugins/search/search.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export function genIndex(path, content = '', router, depth) {
131131
return index;
132132
}
133133

134+
export function ignoreDiacriticalMarks(keyword) {
135+
return keyword.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
136+
}
137+
134138
/**
135139
* @param {String} query Search query
136140
* @returns {Array} Array of results
@@ -160,14 +164,21 @@ export function search(query) {
160164
keywords.forEach(keyword => {
161165
// From https://github.com/sindresorhus/escape-string-regexp
162166
const regEx = new RegExp(
163-
keyword.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'),
167+
ignoreDiacriticalMarks(keyword).replace(
168+
/[|\\{}()[\]^$+*?.]/g,
169+
'\\$&'
170+
),
164171
'gi'
165172
);
166173
let indexTitle = -1;
167174
let indexContent = -1;
168175

169-
indexTitle = postTitle ? postTitle.search(regEx) : -1;
170-
indexContent = postContent ? postContent.search(regEx) : -1;
176+
indexTitle = postTitle
177+
? ignoreDiacriticalMarks(postTitle).search(regEx)
178+
: -1;
179+
indexContent = postContent
180+
? ignoreDiacriticalMarks(postContent).search(regEx)
181+
: -1;
171182

172183
if (indexTitle >= 0 || indexContent >= 0) {
173184
matchesScore += indexTitle >= 0 ? 3 : indexContent >= 0 ? 2 : 0;
@@ -187,7 +198,7 @@ export function search(query) {
187198

188199
const matchContent =
189200
'...' +
190-
escapeHtml(postContent)
201+
escapeHtml(ignoreDiacriticalMarks(postContent))
191202
.substring(start, end)
192203
.replace(
193204
regEx,
@@ -201,7 +212,7 @@ export function search(query) {
201212

202213
if (matchesScore > 0) {
203214
const matchingPost = {
204-
title: escapeHtml(postTitle),
215+
title: escapeHtml(ignoreDiacriticalMarks(postTitle)),
205216
content: postContent ? resultStr : '',
206217
url: postUrl,
207218
score: matchesScore,

test/e2e/search.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,23 @@ describe('Search Plugin Tests', function() {
105105
await page.fill('input[type=search]', 'test');
106106
await expect(page).toEqualText('.results-panel h2', 'Test Page');
107107
});
108+
109+
test('search ignore diacritical marks', async () => {
110+
const docsifyInitConfig = {
111+
markdown: {
112+
homepage: `
113+
# Qué es
114+
115+
docsify genera su sitio web de documentación sobre la marcha. A diferencia de GitBook, no genera archivos estáticos html. En cambio, carga y analiza de forma inteligente sus archivos de Markdown y los muestra como sitio web. Todo lo que necesita hacer es crear un index.html para comenzar y desplegarlo en GitHub Pages.
116+
`,
117+
},
118+
scriptURLs: ['/lib/plugins/search.min.js'],
119+
};
120+
await docsifyInit(docsifyInitConfig);
121+
await page.fill('input[type=search]', 'documentacion');
122+
await expect(page).toEqualText('.results-panel h2', 'Que es');
123+
await page.click('.clear-button');
124+
await page.fill('input[type=search]', 'estáticos');
125+
await expect(page).toEqualText('.results-panel h2', 'Que es');
126+
});
108127
});

0 commit comments

Comments
 (0)