mirror of
https://github.com/TiddlyWiki/TiddlyWiki5.git
synced 2026-04-25 07:24:40 +00:00
Add macro operator (#9520)
* Add macro operator * Replace unnecessary usage of wikify widget * Simplify multiple let widget * fixup! Replace unnecessary usage of wikify widget * Update tests * Update change note * fixup! Replace unnecessary usage of wikify widget * Update docs * fixup! Update tests
This commit is contained in:
27
core/modules/filters/macro.js
Normal file
27
core/modules/filters/macro.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/*\
|
||||||
|
title: $:/core/modules/filters/macro.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: filteroperator
|
||||||
|
|
||||||
|
Filter operator returning those input titles that are returned from a javascript macro
|
||||||
|
|
||||||
|
\*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/*
|
||||||
|
Export our filter function
|
||||||
|
*/
|
||||||
|
exports.macro = function(source,operator,options) {
|
||||||
|
const macroName = operator.operands[0],
|
||||||
|
results = [];
|
||||||
|
if($tw.macros[macroName] !== undefined) {
|
||||||
|
results.push($tw.macros[macroName].run(...operator.operands.slice(1)));
|
||||||
|
} else {
|
||||||
|
// Return the input list if the macro wasn't found
|
||||||
|
source((tiddler,title) => {
|
||||||
|
results.push(title);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
};
|
||||||
@@ -4,13 +4,11 @@ description: create a new journal tiddler
|
|||||||
|
|
||||||
\whitespace trim
|
\whitespace trim
|
||||||
\function get-tags() [<textFieldTags>] [<tagsFieldTags>] +[join[ ]]
|
\function get-tags() [<textFieldTags>] [<tagsFieldTags>] +[join[ ]]
|
||||||
<$let journalTitleTemplate={{$:/config/NewJournal/Title}} textFieldTags={{$:/config/NewJournal/Tags}} tagsFieldTags={{$:/config/NewJournal/Tags!!tags}} journalText={{$:/config/NewJournal/Text}}>
|
<$let journalTitleTemplate={{$:/config/NewJournal/Title}} textFieldTags={{$:/config/NewJournal/Tags}} tagsFieldTags={{$:/config/NewJournal/Tags!!tags}} journalText={{$:/config/NewJournal/Text}} journalTitle={{{ [macro[now],<journalTitleTemplate>] }}}>
|
||||||
<$wikify name="journalTitle" text="<$transclude $variable='now' format=<<journalTitleTemplate>>/>">
|
|
||||||
<$reveal type="nomatch" state=<<journalTitle>> text="">
|
<$reveal type="nomatch" state=<<journalTitle>> text="">
|
||||||
<$action-sendmessage $message="tm-new-tiddler" title=<<journalTitle>> tags=<<get-tags>> text={{{ [<journalTitle>get[]] }}}/>
|
<$action-sendmessage $message="tm-new-tiddler" title=<<journalTitle>> tags=<<get-tags>> text={{{ [<journalTitle>get[]] }}}/>
|
||||||
</$reveal>
|
</$reveal>
|
||||||
<$reveal type="match" state=<<journalTitle>> text="">
|
<$reveal type="match" state=<<journalTitle>> text="">
|
||||||
<$action-sendmessage $message="tm-new-tiddler" title=<<journalTitle>> tags=<<get-tags>> text=<<journalText>>/>
|
<$action-sendmessage $message="tm-new-tiddler" title=<<journalTitle>> tags=<<get-tags>> text=<<journalText>>/>
|
||||||
</$reveal>
|
</$reveal>
|
||||||
</$wikify>
|
|
||||||
</$let>
|
</$let>
|
||||||
|
|||||||
@@ -6,28 +6,19 @@ tags: $:/tags/EditTemplate
|
|||||||
\procedure lingo-base() $:/language/EditTemplate/
|
\procedure lingo-base() $:/language/EditTemplate/
|
||||||
|
|
||||||
\procedure tag-body-inner(colour,fallbackTarget,colourA,colourB,icon,tagField:"tags")
|
\procedure tag-body-inner(colour,fallbackTarget,colourA,colourB,icon,tagField:"tags")
|
||||||
<$wikify name="foregroundColor"
|
<$let backgroundColor=<<colour>> foregroundColor={{{ [macro[contrastcolour],<colour>,<fallbackTarget>,<colourA>,<colourB>] }}}>
|
||||||
text="""<$macrocall $name="contrastcolour"
|
<span class="tc-tag-label tc-tag-list-item tc-small-gap-right"
|
||||||
target=<<colour>>
|
data-tag-title=<<currentTiddler>>
|
||||||
fallbackTarget=<<fallbackTarget>>
|
style=`color:$(foregroundColor)$; background-color:$(backgroundColor)$;`
|
||||||
colourA=<<colourA>>
|
>
|
||||||
colourB=<<colourB>>/>
|
<$transclude tiddler=<<icon>>/>
|
||||||
"""
|
<$view field="title" format="text"/>
|
||||||
>
|
<$button class="tc-btn-invisible tc-remove-tag-button">
|
||||||
<$let backgroundColor=<<colour>> >
|
<$action-listops $tiddler=<<saveTiddler>> $field=<<tagField>> $subfilter="-[{!!title}]"/>
|
||||||
<span class="tc-tag-label tc-tag-list-item tc-small-gap-right"
|
{{$:/core/images/close-button}}
|
||||||
data-tag-title=<<currentTiddler>>
|
</$button>
|
||||||
style=`color:$(foregroundColor)$; background-color:$(backgroundColor)$;`
|
</span>
|
||||||
>
|
</$let>
|
||||||
<$transclude tiddler=<<icon>>/>
|
|
||||||
<$view field="title" format="text"/>
|
|
||||||
<$button class="tc-btn-invisible tc-remove-tag-button">
|
|
||||||
<$action-listops $tiddler=<<saveTiddler>> $field=<<tagField>> $subfilter="-[{!!title}]"/>
|
|
||||||
{{$:/core/images/close-button}}
|
|
||||||
</$button>
|
|
||||||
</span>
|
|
||||||
</$let>
|
|
||||||
</$wikify>
|
|
||||||
\end
|
\end
|
||||||
|
|
||||||
\procedure tag-body(colour,palette,icon,tagField:"tags")
|
\procedure tag-body(colour,palette,icon,tagField:"tags")
|
||||||
|
|||||||
@@ -16,15 +16,13 @@ title: $:/core/ui/TagPickerTagTemplate
|
|||||||
<$set name="backgroundColor"
|
<$set name="backgroundColor"
|
||||||
value={{{ [<currentTiddler>] :cascade[all[shadows+tiddlers]tag[$:/tags/TiddlerColourFilter]!is[draft]get[text]] }}}
|
value={{{ [<currentTiddler>] :cascade[all[shadows+tiddlers]tag[$:/tags/TiddlerColourFilter]!is[draft]get[text]] }}}
|
||||||
>
|
>
|
||||||
<$wikify name="foregroundColor"
|
<$let foregroundColor={{{ [macro[contrastcolour],<backgroundColor>,<fallbackTarget>,<colourA>,<colourB>] }}}>
|
||||||
text="""<$macrocall $name="contrastcolour" target=<<backgroundColor>> fallbackTarget=<<fallbackTarget>> colourA=<<colourA>> colourB=<<colourB>>/>"""
|
|
||||||
>
|
|
||||||
<span class="tc-tag-label tc-btn-invisible"
|
<span class="tc-tag-label tc-btn-invisible"
|
||||||
style=<<tag-pill-styles>>
|
style=<<tag-pill-styles>>
|
||||||
data-tag-title=<<currentTiddler>>
|
data-tag-title=<<currentTiddler>>
|
||||||
>
|
>
|
||||||
{{||$:/core/ui/TiddlerIcon}}<$view field="title" format="text"/>
|
{{||$:/core/ui/TiddlerIcon}}<$view field="title" format="text"/>
|
||||||
</span>
|
</span>
|
||||||
</$wikify>
|
</$let>
|
||||||
</$set>
|
</$set>
|
||||||
</$button>
|
</$button>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ description: {{$:/language/Buttons/NewJournalHere/Hint}}
|
|||||||
\whitespace trim
|
\whitespace trim
|
||||||
\procedure journalButton()
|
\procedure journalButton()
|
||||||
<$button tooltip={{$:/language/Buttons/NewJournalHere/Hint}} aria-label={{$:/language/Buttons/NewJournalHere/Caption}} class=<<tv-config-toolbar-class>>>
|
<$button tooltip={{$:/language/Buttons/NewJournalHere/Hint}} aria-label={{$:/language/Buttons/NewJournalHere/Caption}} class=<<tv-config-toolbar-class>>>
|
||||||
<$wikify name="journalTitle" text="""<$transclude $variable="now" format=<<journalTitleTemplate>>/>""">
|
<$let journalTitle={{{ [macro[now],<journalTitleTemplate>] }}}>
|
||||||
<$action-sendmessage $message="tm-new-tiddler" title=<<journalTitle>> tags=`[[$(currentTiddlerTag)$]] $(journalTags)$`/>
|
<$action-sendmessage $message="tm-new-tiddler" title=<<journalTitle>> tags=`[[$(currentTiddlerTag)$]] $(journalTags)$`/>
|
||||||
<%if [<tv-config-toolbar-icons>match[yes]] %>
|
<%if [<tv-config-toolbar-icons>match[yes]] %>
|
||||||
{{$:/core/images/new-journal-button}}
|
{{$:/core/images/new-journal-button}}
|
||||||
@@ -16,7 +16,7 @@ description: {{$:/language/Buttons/NewJournalHere/Hint}}
|
|||||||
<$text text={{$:/language/Buttons/NewJournalHere/Caption}}/>
|
<$text text={{$:/language/Buttons/NewJournalHere/Caption}}/>
|
||||||
</span>
|
</span>
|
||||||
<%endif%>
|
<%endif%>
|
||||||
</$wikify>
|
</$let>
|
||||||
</$button>
|
</$button>
|
||||||
\end
|
\end
|
||||||
<$let journalTitleTemplate={{$:/config/NewJournal/Title}} journalTags={{$:/config/NewJournal/Tags}} currentTiddlerTag=<<currentTiddler>>>
|
<$let journalTitleTemplate={{$:/config/NewJournal/Title}} journalTags={{$:/config/NewJournal/Tags}} currentTiddlerTag=<<currentTiddler>>>
|
||||||
|
|||||||
@@ -1088,6 +1088,10 @@ Tests the filtering mechanism.
|
|||||||
expect(wiki.filterTiddlers("[[representation]makepatches[misreprehensionisation]]").join(" ")).toBe("@@ -1,13 +1,21 @@\n+mis\n repre\n-sent\n+hensionis\n atio\n");
|
expect(wiki.filterTiddlers("[[representation]makepatches[misreprehensionisation]]").join(" ")).toBe("@@ -1,13 +1,21 @@\n+mis\n repre\n-sent\n+hensionis\n atio\n");
|
||||||
expect(wiki.filterTiddlers("[[the cat sat on the mat]makepatches[the hat saw in every category]]").join(" ")).toBe("@@ -1,22 +1,29 @@\n the \n-c\n+h\n at sa\n-t on the mat\n+w in every category\n");
|
expect(wiki.filterTiddlers("[[the cat sat on the mat]makepatches[the hat saw in every category]]").join(" ")).toBe("@@ -1,22 +1,29 @@\n the \n-c\n+h\n at sa\n-t on the mat\n+w in every category\n");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle the macro operator", () => {
|
||||||
|
expect(wiki.filterTiddlers("[macro[makedatauri],[some example text],[text/plain]]").join("")).toBe("data:text/plain,some%20example%20text");
|
||||||
|
});
|
||||||
|
|
||||||
it("should parse filter variable parameters", function(){
|
it("should parse filter variable parameters", function(){
|
||||||
expect($tw.utils.parseFilterVariable("currentTiddler")).toEqual(
|
expect($tw.utils.parseFilterVariable("currentTiddler")).toEqual(
|
||||||
|
|||||||
7
editions/tw5.com/tiddlers/macro Operator (Examples).tid
Normal file
7
editions/tw5.com/tiddlers/macro Operator (Examples).tid
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
created: 20260104045032090
|
||||||
|
modified: 20260104045240842
|
||||||
|
tags: [[macro Operator]]
|
||||||
|
title: macro Operator (Examples)
|
||||||
|
type: text/vnd.tiddlywiki
|
||||||
|
|
||||||
|
<<.operator-example 1 "[macro[now],{$:/config/NewJournal/Title}]">>
|
||||||
14
editions/tw5.com/tiddlers/macro Operator.tid
Normal file
14
editions/tw5.com/tiddlers/macro Operator.tid
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
caption: macro
|
||||||
|
created: 20260104043647022
|
||||||
|
modified: 20260104045136808
|
||||||
|
op-input: ignored
|
||||||
|
op-output: the result of the javascript macro
|
||||||
|
op-parameter: first parameter is the javascript macro, subsequent parameters are passed to the macro by position
|
||||||
|
op-purpose: return the result from a javascript macro (with indirect parameters support)
|
||||||
|
tags: [[Filter Operators]]
|
||||||
|
title: macro Operator
|
||||||
|
type: text/vnd.tiddlywiki
|
||||||
|
|
||||||
|
<<.from-version "5.4.0">> The <<.op macro>> operator applies a javascript macro to the input titles, and returns the result from the macro. The function is invoked once with all of the input titles.
|
||||||
|
|
||||||
|
<<.operator-examples "macro">>
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
title: $:/changenotes/5.4.0/#9520
|
||||||
|
description: Add macro operator
|
||||||
|
release: 5.4.0
|
||||||
|
tags: $:/tags/ChangeNote
|
||||||
|
change-type: feature
|
||||||
|
change-category: filters
|
||||||
|
github-links: https://github.com/TiddlyWiki/TiddlyWiki5/pull/9520
|
||||||
|
github-contributors: Leilei332
|
||||||
|
|
||||||
|
Added [[macro Operator]] for evaluating javascript macros in filters.
|
||||||
Reference in New Issue
Block a user