From 2975d8c857cb6ea4eaaee31042bbe55e40b14ea7 Mon Sep 17 00:00:00 2001
From: sy-records <52o@qq52o.cn>
Date: Mon, 30 Nov 2020 08:51:27 +0800
Subject: [PATCH 1/5] feat: search ignore diacritical marks
---
src/plugins/search/search.js | 21 ++++++++++++++++-----
test/e2e/search.test.js | 19 +++++++++++++++++++
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js
index e16c3a313..b649b2257 100644
--- a/src/plugins/search/search.js
+++ b/src/plugins/search/search.js
@@ -131,6 +131,10 @@ export function genIndex(path, content = '', router, depth) {
return index;
}
+export function ignoreDiacriticalMarks(keyword) {
+ return keyword.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
+}
+
/**
* @param {String} query Search query
* @returns {Array} Array of results
@@ -160,14 +164,21 @@ export function search(query) {
keywords.forEach(keyword => {
// From https://github.com/sindresorhus/escape-string-regexp
const regEx = new RegExp(
- keyword.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'),
+ ignoreDiacriticalMarks(keyword).replace(
+ /[|\\{}()[\]^$+*?.]/g,
+ '\\$&'
+ ),
'gi'
);
let indexTitle = -1;
let indexContent = -1;
- indexTitle = postTitle ? postTitle.search(regEx) : -1;
- indexContent = postContent ? postContent.search(regEx) : -1;
+ indexTitle = postTitle
+ ? ignoreDiacriticalMarks(postTitle).search(regEx)
+ : -1;
+ indexContent = postContent
+ ? ignoreDiacriticalMarks(postContent).search(regEx)
+ : -1;
if (indexTitle >= 0 || indexContent >= 0) {
matchesScore += indexTitle >= 0 ? 3 : indexContent >= 0 ? 2 : 0;
@@ -187,7 +198,7 @@ export function search(query) {
const matchContent =
'...' +
- escapeHtml(postContent)
+ escapeHtml(ignoreDiacriticalMarks(postContent))
.substring(start, end)
.replace(
regEx,
@@ -201,7 +212,7 @@ export function search(query) {
if (matchesScore > 0) {
const matchingPost = {
- title: escapeHtml(postTitle),
+ title: escapeHtml(ignoreDiacriticalMarks(postTitle)),
content: postContent ? resultStr : '',
url: postUrl,
score: matchesScore,
diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js
index dcdc6c872..8afce5500 100644
--- a/test/e2e/search.test.js
+++ b/test/e2e/search.test.js
@@ -105,4 +105,23 @@ describe('Search Plugin Tests', function() {
await page.fill('input[type=search]', 'test');
await expect(page).toEqualText('.results-panel h2', 'Test Page');
});
+
+ test('search ignore diacritical marks', async () => {
+ const docsifyInitConfig = {
+ markdown: {
+ homepage: `
+ # Qué es
+
+ 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.
+ `,
+ },
+ scriptURLs: ['/lib/plugins/search.min.js'],
+ };
+ await docsifyInit(docsifyInitConfig);
+ await page.fill('input[type=search]', 'documentacion');
+ await expect(page).toEqualText('.results-panel h2', 'Que es');
+ await page.click('.clear-button');
+ await page.fill('input[type=search]', 'estáticos');
+ await expect(page).toEqualText('.results-panel h2', 'Que es');
+ });
});
From 2102b695017f61d6cc798c59b33233706914f3c3 Mon Sep 17 00:00:00 2001
From: sy-records <52o@qq52o.cn>
Date: Wed, 2 Dec 2020 17:55:44 +0800
Subject: [PATCH 2/5] Check if normalize is supported
---
docs/plugins.md | 6 ++++++
src/plugins/search/search.js | 5 ++++-
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/docs/plugins.md b/docs/plugins.md
index c56514d1a..f25c4177f 100644
--- a/docs/plugins.md
+++ b/docs/plugins.md
@@ -62,6 +62,12 @@ By default, the hyperlink on the current page is recognized and the content is s
```
+The search ignores diacritical marks by default, but is not supported by IE. If you need support, please load this `ponyfill`
+
+```html
+
+```
+
## Google Analytics
Install the plugin and configure the track id.
diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js
index b649b2257..ce3504e60 100644
--- a/src/plugins/search/search.js
+++ b/src/plugins/search/search.js
@@ -132,7 +132,10 @@ export function genIndex(path, content = '', router, depth) {
}
export function ignoreDiacriticalMarks(keyword) {
- return keyword.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
+ if (keyword && keyword.normalize) {
+ return keyword.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
+ }
+ return keyword;
}
/**
From 803e6b67d91e40339bbb74fe719b000f2e787849 Mon Sep 17 00:00:00 2001
From: sy-records <52o@qq52o.cn>
Date: Tue, 15 Dec 2020 16:22:45 +0800
Subject: [PATCH 3/5] Update docs
---
docs/plugins.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/plugins.md b/docs/plugins.md
index f25c4177f..cd7016d6b 100644
--- a/docs/plugins.md
+++ b/docs/plugins.md
@@ -62,7 +62,7 @@ By default, the hyperlink on the current page is recognized and the content is s
```
-The search ignores diacritical marks by default, but is not supported by IE. If you need support, please load this `ponyfill`
+This plugin ignores diacritical marks when performing a full text search (e.g., "cafe" will also match "café"). Legacy browsers like IE11 require the following [String.normalize()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) polyfill to ignore diacritical marks:
```html
From 6d0a1bf4cc3bc4bb89c63e984137dcb0dd7ec9d8 Mon Sep 17 00:00:00 2001
From: sy-records <52o@qq52o.cn>
Date: Tue, 15 Dec 2020 19:15:22 +0800
Subject: [PATCH 4/5] Fix escape
---
src/plugins/search/search.js | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js
index ce3504e60..3fc8bdd55 100644
--- a/src/plugins/search/search.js
+++ b/src/plugins/search/search.js
@@ -159,6 +159,8 @@ export function search(query) {
const post = data[i];
let matchesScore = 0;
let resultStr = '';
+ let handlePostTitle = '';
+ let handlePostContent = '';
const postTitle = post.title && post.title.trim();
const postContent = post.body && post.body.trim();
const postUrl = post.slug || '';
@@ -167,7 +169,7 @@ export function search(query) {
keywords.forEach(keyword => {
// From https://github.com/sindresorhus/escape-string-regexp
const regEx = new RegExp(
- ignoreDiacriticalMarks(keyword).replace(
+ escapeHtml(ignoreDiacriticalMarks(keyword)).replace(
/[|\\{}()[\]^$+*?.]/g,
'\\$&'
),
@@ -175,13 +177,15 @@ export function search(query) {
);
let indexTitle = -1;
let indexContent = -1;
+ handlePostTitle = postTitle
+ ? escapeHtml(ignoreDiacriticalMarks(postTitle))
+ : postTitle;
+ handlePostContent = postContent
+ ? escapeHtml(ignoreDiacriticalMarks(postContent))
+ : postContent;
- indexTitle = postTitle
- ? ignoreDiacriticalMarks(postTitle).search(regEx)
- : -1;
- indexContent = postContent
- ? ignoreDiacriticalMarks(postContent).search(regEx)
- : -1;
+ indexTitle = postTitle ? handlePostTitle.search(regEx) : -1;
+ indexContent = postContent ? handlePostContent.search(regEx) : -1;
if (indexTitle >= 0 || indexContent >= 0) {
matchesScore += indexTitle >= 0 ? 3 : indexContent >= 0 ? 2 : 0;
@@ -201,7 +205,7 @@ export function search(query) {
const matchContent =
'...' +
- escapeHtml(ignoreDiacriticalMarks(postContent))
+ handlePostContent
.substring(start, end)
.replace(
regEx,
@@ -215,7 +219,7 @@ export function search(query) {
if (matchesScore > 0) {
const matchingPost = {
- title: escapeHtml(ignoreDiacriticalMarks(postTitle)),
+ title: handlePostTitle,
content: postContent ? resultStr : '',
url: postUrl,
score: matchesScore,
From 91f8d791ff99e007ada4fb3509681edb5fb44bec Mon Sep 17 00:00:00 2001
From: sy-records <52o@qq52o.cn>
Date: Fri, 25 Dec 2020 18:57:16 +0800
Subject: [PATCH 5/5] remove escapeHtml
---
src/plugins/search/search.js | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js
index 3fc8bdd55..dd809d791 100644
--- a/src/plugins/search/search.js
+++ b/src/plugins/search/search.js
@@ -169,7 +169,7 @@ export function search(query) {
keywords.forEach(keyword => {
// From https://github.com/sindresorhus/escape-string-regexp
const regEx = new RegExp(
- escapeHtml(ignoreDiacriticalMarks(keyword)).replace(
+ ignoreDiacriticalMarks(keyword).replace(
/[|\\{}()[\]^$+*?.]/g,
'\\$&'
),
@@ -178,10 +178,10 @@ export function search(query) {
let indexTitle = -1;
let indexContent = -1;
handlePostTitle = postTitle
- ? escapeHtml(ignoreDiacriticalMarks(postTitle))
+ ? ignoreDiacriticalMarks(postTitle)
: postTitle;
handlePostContent = postContent
- ? escapeHtml(ignoreDiacriticalMarks(postContent))
+ ? ignoreDiacriticalMarks(postContent)
: postContent;
indexTitle = postTitle ? handlePostTitle.search(regEx) : -1;