如何重用 Javascript 宏之间的代码并最大限度地减少在宏中完成的工作?
How can I reuse code between Javascript macros and minimize work done within the macros?
我目前有两个宏是我正在开发的(非常有限的受众)插件的一部分,它们基本上看起来像:
(function(){
exports.name = "name";
exports.params = [
{name: "value"}
];
function get(tiddler) {
// return some contents of some tiddler fields according to some rule
}
function parse(data) {
// convert string to some kind of useful object
}
function logic(x, y) {
// determine whether the two objects correspond in some way
};
function format(data, title) {
// produce WikiText for a link with some additional decoration
};
exports.run = function(value) {
value = parse(value);
var result = [];
this.wiki.each(function(tiddler, title) {
var data = get(tiddler);
if (data !== undefined && logic(value, parse(data))) {
result.push(format(data, title));
}
});
return result.join(" | ");
};
})();
因此,当单独考虑时,它们已经相当巧妙地考虑在内;问题是两个宏之间只有核心 logic
真正不同。如何在宏之间共享函数 get
、logic
和 format
?我试过将它们放在一个单独的 tiddler 中,但这不起作用;当宏 运行 时,TW 引发错误,声称函数是 "not defined"。将每个函数作为自己的 javascript 宏包装在单独的 tiddler 中,例如
(function(){
exports.name = "get";
exports.params = [
{name: "tiddler"}
];
exports.run = function(tiddler) {
// return some contents of some tiddler fields according to some rule
}
})();
也没有帮助。
我还想通过转动主 get
/parse
/logic
/format
将其设置得更 modular/flexible处理成自定义过滤器,然后让普通过滤器表达式负责迭代并使用例如小部件或 <> 宏来显示项目。我究竟该如何设置?文档告诉我
If the provided filter operators are not enough, a developer can add
new filters by adding a module with the filteroperator
type
但我找不到 API 的任何文档,也找不到任何示例。
How can I share the functions get, logic and format between the macros?
您可以使用 Common/JS 标准 require('<tiddler title>')
语法来访问另一个 tiddler 的导出。目标 tiddler 应设置为 JS 模块(即,type
字段设置为 application/javascript
,module-type
字段设置为 library
)。你可以在这里看到一个例子:
https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/widgets/count.js#L15
I'd also like to set this up to be more modular/flexible, by turning the main get/parse/logic/format process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the widget or <> macro to display the items. How exactly do I set this up?
用于编写过滤器运算符的 API 目前没有记录,但有很多示例可供查看:
https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/filters
我目前有两个宏是我正在开发的(非常有限的受众)插件的一部分,它们基本上看起来像:
(function(){
exports.name = "name";
exports.params = [
{name: "value"}
];
function get(tiddler) {
// return some contents of some tiddler fields according to some rule
}
function parse(data) {
// convert string to some kind of useful object
}
function logic(x, y) {
// determine whether the two objects correspond in some way
};
function format(data, title) {
// produce WikiText for a link with some additional decoration
};
exports.run = function(value) {
value = parse(value);
var result = [];
this.wiki.each(function(tiddler, title) {
var data = get(tiddler);
if (data !== undefined && logic(value, parse(data))) {
result.push(format(data, title));
}
});
return result.join(" | ");
};
})();
因此,当单独考虑时,它们已经相当巧妙地考虑在内;问题是两个宏之间只有核心 logic
真正不同。如何在宏之间共享函数 get
、logic
和 format
?我试过将它们放在一个单独的 tiddler 中,但这不起作用;当宏 运行 时,TW 引发错误,声称函数是 "not defined"。将每个函数作为自己的 javascript 宏包装在单独的 tiddler 中,例如
(function(){
exports.name = "get";
exports.params = [
{name: "tiddler"}
];
exports.run = function(tiddler) {
// return some contents of some tiddler fields according to some rule
}
})();
也没有帮助。
我还想通过转动主 get
/parse
/logic
/format
将其设置得更 modular/flexible处理成自定义过滤器,然后让普通过滤器表达式负责迭代并使用例如小部件或 <> 宏来显示项目。我究竟该如何设置?文档告诉我
If the provided filter operators are not enough, a developer can add new filters by adding a module with the
filteroperator
type
但我找不到 API 的任何文档,也找不到任何示例。
How can I share the functions get, logic and format between the macros?
您可以使用 Common/JS 标准 require('<tiddler title>')
语法来访问另一个 tiddler 的导出。目标 tiddler 应设置为 JS 模块(即,type
字段设置为 application/javascript
,module-type
字段设置为 library
)。你可以在这里看到一个例子:
https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/widgets/count.js#L15
I'd also like to set this up to be more modular/flexible, by turning the main get/parse/logic/format process into a custom filter, then letting a normal filter expression take care of the iteration and using e.g. the widget or <> macro to display the items. How exactly do I set this up?
用于编写过滤器运算符的 API 目前没有记录,但有很多示例可供查看:
https://github.com/Jermolene/TiddlyWiki5/tree/master/core/modules/filters