1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 01:20:30 +00:00

Extend search filter to allow field to be specified

This commit is contained in:
Jermolene 2014-11-06 20:56:32 +00:00
parent f1a2d8c2b9
commit 8260d000be
3 changed files with 25 additions and 9 deletions

View File

@ -17,10 +17,18 @@ Export our filter function
*/ */
exports.search = function(source,operator,options) { exports.search = function(source,operator,options) {
var invert = operator.prefix === "!"; var invert = operator.prefix === "!";
return options.wiki.search(operator.operand,{ if(operator.suffix) {
source: source, return options.wiki.search(operator.operand,{
invert: invert source: source,
}); invert: invert,
field: operator.suffix
});
} else {
return options.wiki.search(operator.operand,{
source: source,
invert: invert
});
}
}; };
})(); })();

View File

@ -969,6 +969,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
literal: If true, searches for literal string, rather than separate search terms literal: If true, searches for literal string, rather than separate search terms
field: If specified, restricts the search to the specified field
*/ */
exports.search = function(text,options) { exports.search = function(text,options) {
options = options || {}; options = options || {};
@ -1007,13 +1008,17 @@ exports.search = function(text,options) {
var contentTypeInfo = $tw.config.contentTypeInfo[tiddler.fields.type] || $tw.config.contentTypeInfo["text/vnd.tiddlywiki"], var contentTypeInfo = $tw.config.contentTypeInfo[tiddler.fields.type] || $tw.config.contentTypeInfo["text/vnd.tiddlywiki"],
match; match;
for(var t=0; t<searchTermsRegExps.length; t++) { for(var t=0; t<searchTermsRegExps.length; t++) {
// Search title, tags and body
match = false; match = false;
if(contentTypeInfo.encoding === "utf8") { if(options.field) {
match = match || searchTermsRegExps[t].test(tiddler.fields.text); match = searchTermsRegExps[t].test(tiddler.getFieldString(options.field));
} else {
// Search title, tags and body
if(contentTypeInfo.encoding === "utf8") {
match = match || searchTermsRegExps[t].test(tiddler.fields.text);
}
var tags = tiddler.fields.tags ? tiddler.fields.tags.join("\0") : "";
match = match || searchTermsRegExps[t].test(tags) || searchTermsRegExps[t].test(tiddler.fields.title);
} }
var tags = tiddler.fields.tags ? tiddler.fields.tags.join("\0") : "";
match = match || searchTermsRegExps[t].test(tags) || searchTermsRegExps[t].test(tiddler.fields.title);
if(!match) { if(!match) {
return false; return false;
} }

View File

@ -7,8 +7,11 @@ type: text/vnd.tiddlywiki
The ''search'' filter operator filters the current list to leave only those tiddlers that include the operand text in their title, body or tags. Preceding the operator with `!` returns all tiddlers that do not include the specified text. The search is case-insenstive. The ''search'' filter operator filters the current list to leave only those tiddlers that include the operand text in their title, body or tags. Preceding the operator with `!` returns all tiddlers that do not include the specified text. The search is case-insenstive.
Optionally, a field can be specified to restrict the search.
For example: For example:
|!Filter String |!Description | |!Filter String |!Description |
|`[search[alsatian]]` |Returns a list of the tiddlers containing the text "alsatian" | |`[search[alsatian]]` |Returns a list of the tiddlers containing the text "alsatian" |
|`[all[shadows]search[alsatian]]` |Returns a list of the shadow tiddlers containing the text "alsatian" | |`[all[shadows]search[alsatian]]` |Returns a list of the shadow tiddlers containing the text "alsatian" |
|`[search:caption[spaniel]]` |Returns a list of the tiddlers containing the text "spaniel" in their caption field |