mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-05 21:33:52 +00:00
Add support for anchored searches
This commit is contained in:
parent
dbaccf792d
commit
3afaa9de9a
@ -43,6 +43,7 @@ exports.search = function(source,operator,options) {
|
|||||||
caseSensitive: hasFlag("casesensitive"),
|
caseSensitive: hasFlag("casesensitive"),
|
||||||
literal: hasFlag("literal"),
|
literal: hasFlag("literal"),
|
||||||
whitespace: hasFlag("whitespace"),
|
whitespace: hasFlag("whitespace"),
|
||||||
|
anchored: hasFlag("anchored"),
|
||||||
regexp: hasFlag("regexp"),
|
regexp: hasFlag("regexp"),
|
||||||
words: hasFlag("words")
|
words: hasFlag("words")
|
||||||
});
|
});
|
||||||
|
@ -1065,6 +1065,7 @@ Options available:
|
|||||||
invert: If true returns tiddlers that do not contain the specified string
|
invert: If true returns tiddlers that do not contain the specified string
|
||||||
caseSensitive: If true forces a case sensitive search
|
caseSensitive: If true forces a case sensitive search
|
||||||
field: If specified, restricts the search to the specified field, or an array of field names
|
field: If specified, restricts the search to the specified field, or an array of field names
|
||||||
|
anchored: If true, forces all but regexp searches to be anchored to the start of text
|
||||||
excludeField: If true, the field options are inverted to specify the fields that are not to be searched
|
excludeField: If true, the field options are inverted to specify the fields that are not to be searched
|
||||||
The search mode is determined by the first of these boolean flags to be true
|
The search mode is determined by the first of these boolean flags to be true
|
||||||
literal: searches for literal string
|
literal: searches for literal string
|
||||||
@ -1079,12 +1080,13 @@ exports.search = function(text,options) {
|
|||||||
invert = !!options.invert;
|
invert = !!options.invert;
|
||||||
// Convert the search string into a regexp for each term
|
// Convert the search string into a regexp for each term
|
||||||
var terms, searchTermsRegExps,
|
var terms, searchTermsRegExps,
|
||||||
flags = options.caseSensitive ? "" : "i";
|
flags = options.caseSensitive ? "" : "i",
|
||||||
|
anchor = options.anchored ? "^" : "";
|
||||||
if(options.literal) {
|
if(options.literal) {
|
||||||
if(text.length === 0) {
|
if(text.length === 0) {
|
||||||
searchTermsRegExps = null;
|
searchTermsRegExps = null;
|
||||||
} else {
|
} else {
|
||||||
searchTermsRegExps = [new RegExp("(" + $tw.utils.escapeRegExp(text) + ")",flags)];
|
searchTermsRegExps = [new RegExp("(" + anchor + $tw.utils.escapeRegExp(text) + ")",flags)];
|
||||||
}
|
}
|
||||||
} else if(options.whitespace) {
|
} else if(options.whitespace) {
|
||||||
terms = [];
|
terms = [];
|
||||||
@ -1093,7 +1095,7 @@ exports.search = function(text,options) {
|
|||||||
terms.push($tw.utils.escapeRegExp(term));
|
terms.push($tw.utils.escapeRegExp(term));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
searchTermsRegExps = [new RegExp("(" + terms.join("\\s+") + ")",flags)];
|
searchTermsRegExps = [new RegExp("(" + anchor + terms.join("\\s+") + ")",flags)];
|
||||||
} else if(options.regexp) {
|
} else if(options.regexp) {
|
||||||
try {
|
try {
|
||||||
searchTermsRegExps = [new RegExp("(" + text + ")",flags)];
|
searchTermsRegExps = [new RegExp("(" + text + ")",flags)];
|
||||||
@ -1108,7 +1110,7 @@ exports.search = function(text,options) {
|
|||||||
} else {
|
} else {
|
||||||
searchTermsRegExps = [];
|
searchTermsRegExps = [];
|
||||||
for(t=0; t<terms.length; t++) {
|
for(t=0; t<terms.length; t++) {
|
||||||
searchTermsRegExps.push(new RegExp("(" + $tw.utils.escapeRegExp(terms[t]) + ")",flags));
|
searchTermsRegExps.push(new RegExp("(" + anchor + $tw.utils.escapeRegExp(terms[t]) + ")",flags));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -313,6 +313,7 @@ function runTests(wiki) {
|
|||||||
expect(wiki.filterTiddlers("[search:modifier:regexp[(d|bl)o(ggs|e)]sort[title]]").join(",")).toBe("$:/TiddlerTwo,a fourth tiddler,one,Tiddler Three,TiddlerOne");
|
expect(wiki.filterTiddlers("[search:modifier:regexp[(d|bl)o(ggs|e)]sort[title]]").join(",")).toBe("$:/TiddlerTwo,a fourth tiddler,one,Tiddler Three,TiddlerOne");
|
||||||
expect(wiki.filterTiddlers("[search:-modifier,authors:[g]sort[title]]").join(",")).toBe("$:/ShadowPlugin,Tiddler Three");
|
expect(wiki.filterTiddlers("[search:-modifier,authors:[g]sort[title]]").join(",")).toBe("$:/ShadowPlugin,Tiddler Three");
|
||||||
expect(wiki.filterTiddlers("[search:*:[g]sort[title]]").join(",")).toBe("$:/ShadowPlugin,Tiddler Three,TiddlerOne");
|
expect(wiki.filterTiddlers("[search:*:[g]sort[title]]").join(",")).toBe("$:/ShadowPlugin,Tiddler Three,TiddlerOne");
|
||||||
|
expect(wiki.filterTiddlers("[search:text:anchored[the]]").join(",")).toBe("TiddlerOne,$:/TiddlerTwo,Tiddler Three,a fourth tiddler");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should yield search results that have search tokens spread across different fields", function() {
|
it("should yield search results that have search tokens spread across different fields", function() {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
created: 20140410103123179
|
created: 20140410103123179
|
||||||
modified: 20181025082022690
|
modified: 20190731212738712
|
||||||
tags: [[Filter Operators]] [[Common Operators]] [[Field Operators]] [[Negatable Operators]]
|
tags: [[Filter Operators]] [[Common Operators]] [[Field Operators]] [[Negatable Operators]]
|
||||||
title: search Operator
|
title: search Operator
|
||||||
type: text/vnd.tiddlywiki
|
type: text/vnd.tiddlywiki
|
||||||
@ -40,5 +40,6 @@ The available flags are:
|
|||||||
** ''regexp'': treats the search string as a regular expression. Note that the ''regexp'' option obviates the need for the older <<.olink regexp>> operator.
|
** ''regexp'': treats the search string as a regular expression. Note that the ''regexp'' option obviates the need for the older <<.olink regexp>> operator.
|
||||||
** ''words'': (the default) treats the search string as a list of tokens separated by whitespace, and matches if all of the tokens appear in the string (regardless of ordering and whether there is other text in between)
|
** ''words'': (the default) treats the search string as a list of tokens separated by whitespace, and matches if all of the tokens appear in the string (regardless of ordering and whether there is other text in between)
|
||||||
* ''casesensitive'': if present, this flag forces a case-sensitive match, where upper and lower case letters are considered different. By default, upper and lower case letters are considered identical for matching purposes.
|
* ''casesensitive'': if present, this flag forces a case-sensitive match, where upper and lower case letters are considered different. By default, upper and lower case letters are considered identical for matching purposes.
|
||||||
|
* ''anchored'': <<.from-version "5.1.20">> anchors the search to the start of the string (applies to ''whitespace'', ''literal'' and ''words'' modes)
|
||||||
|
|
||||||
<<.operator-examples "search">>
|
<<.operator-examples "search">>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user