mirror of
https://github.com/laurent22/joplin.git
synced 2025-02-01 19:15:01 +02:00
Mobile: Fixes #1082: Highlight correct keywords when doing a search
This commit is contained in:
parent
5a00214fd2
commit
f308fe71f9
44
CliClient/tests/StringUtils.js
Normal file
44
CliClient/tests/StringUtils.js
Normal file
@ -0,0 +1,44 @@
|
||||
require('app-module-path').addPath(__dirname);
|
||||
|
||||
const { time } = require('lib/time-utils.js');
|
||||
const { fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
|
||||
const StringUtils = require('lib/string-utils');
|
||||
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
||||
});
|
||||
|
||||
describe('StringUtils', function() {
|
||||
|
||||
beforeEach(async (done) => {
|
||||
done();
|
||||
});
|
||||
|
||||
it('should surround keywords with strings', async (done) => {
|
||||
const testCases = [
|
||||
[['test'], 'test', 'a', 'b', 'atestb'],
|
||||
[['test'], 'Test', 'a', 'b', 'aTestb'],
|
||||
[['te[]st'], 'Te[]st', 'a', 'b', 'aTe[]stb'],
|
||||
[['test1', 'test2'], 'bla test1 blabla test1 bla test2 not this one - test22', 'a', 'b', 'bla atest1b blabla atest1b bla atest2b not this one - test22'],
|
||||
[['test1', 'test2'], 'bla test1 test1 bla test2', '<span class="highlighted-keyword">', '</span>', 'bla <span class="highlighted-keyword">test1</span> <span class="highlighted-keyword">test1</span> bla <span class="highlighted-keyword">test2</span>'],
|
||||
[[{ type:'regex', value:'test.*?'}], 'bla test1 test1 bla test2 test tttest', 'a', 'b', 'bla atest1b atest1b bla atest2b atestb tttest'],
|
||||
];
|
||||
|
||||
for (let i = 0; i < testCases.length; i++) {
|
||||
const t = testCases[i];
|
||||
|
||||
const keywords = t[0];
|
||||
const input = t[1];
|
||||
const prefix = t[2];
|
||||
const suffix = t[3];
|
||||
const expected = t[4];
|
||||
|
||||
const actual = StringUtils.surroundKeywords(keywords, input, prefix, suffix);
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
@ -7,7 +7,7 @@ const { shim } = require('lib/shim.js');
|
||||
const { _ } = require('lib/locale');
|
||||
const md5 = require('md5');
|
||||
const MdToHtml_Katex = require('lib/MdToHtml_Katex');
|
||||
const { pregQuote } = require('lib/string-utils.js');
|
||||
const StringUtils = require('lib/string-utils.js');
|
||||
|
||||
class MdToHtml {
|
||||
|
||||
@ -415,22 +415,7 @@ class MdToHtml {
|
||||
}
|
||||
|
||||
applyHighlightedKeywords_(body, keywords) {
|
||||
for (let i = 0; i < keywords.length; i++) {
|
||||
const k = keywords[i];
|
||||
|
||||
let regexString = '';
|
||||
|
||||
if (k.type === 'regex') {
|
||||
regexString = k.value;
|
||||
} else {
|
||||
regexString = pregQuote(k);
|
||||
}
|
||||
|
||||
const re = new RegExp('(^|\n|\b)(' + regexString + ')(\n|\b|$)', 'gi');
|
||||
body = body.replace(re, '$1<span class="highlighted-keyword">$2</span>$3');
|
||||
}
|
||||
|
||||
return body;
|
||||
return StringUtils.surroundKeywords(keywords, body, '<span class="highlighted-keyword">', '</span>');
|
||||
}
|
||||
|
||||
render(body, style, options = null) {
|
||||
|
@ -108,7 +108,8 @@ class SearchEngine {
|
||||
|
||||
let regexString = pregQuote(term);
|
||||
if (regexString[regexString.length - 1] === '*') {
|
||||
regexString = regexString.substr(0, regexString.length - 2) + '[^' + pregQuote(' \t\n\r,.,+-*?!={}<>|:"\'()[]') + ']' + '*';
|
||||
// regexString = regexString.substr(0, regexString.length - 2) + '[^' + pregQuote(' \t\n\r,.,+-*?!={}<>|:"\'()[]') + ']' + '*';
|
||||
regexString = regexString.substr(0, regexString.length - 2) + '.*?';
|
||||
}
|
||||
|
||||
return regexString;
|
||||
|
@ -224,8 +224,21 @@ function escapeHtml(s) {
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function pregQuote(str, delimiter) {
|
||||
function pregQuote(str, delimiter = '') {
|
||||
return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
|
||||
}
|
||||
|
||||
module.exports = { removeDiacritics, escapeFilename, wrap, splitCommandString, padLeft, toTitleCase, escapeHtml, pregQuote };
|
||||
function surroundKeywords(keywords, text, prefix, suffix) {
|
||||
let regexString = keywords.map((k) => {
|
||||
if (k.type === 'regex') {
|
||||
return k.value;
|
||||
} else {
|
||||
return pregQuote(k);
|
||||
}
|
||||
}).join('|');
|
||||
regexString = '\\b(' + regexString + ')\\b'
|
||||
const re = new RegExp(regexString, 'gi');
|
||||
return text.replace(re, prefix + '$1' + suffix);
|
||||
}
|
||||
|
||||
module.exports = { removeDiacritics, escapeFilename, wrap, splitCommandString, padLeft, toTitleCase, escapeHtml, pregQuote, surroundKeywords };
|
Loading…
x
Reference in New Issue
Block a user