mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-08-20 14:45:59 +00:00
![Jermolene](/assets/img/avatar_default.png)
See here for discussion: https://groups.google.com/d/msg/TiddlyWiki/D3MBNhjaT3E/ph0ZuAhuPFMJ
70 lines
1.6 KiB
JavaScript
70 lines
1.6 KiB
JavaScript
/*\
|
|
title: $:/core/modules/parsers/wikiparser/rules/transcludeblock.js
|
|
type: application/javascript
|
|
module-type: wikirule
|
|
|
|
Wiki text rule for block-level transclusion. For example:
|
|
|
|
```
|
|
{{MyTiddler}}
|
|
{{MyTiddler||TemplateTitle}}
|
|
```
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
exports.name = "transcludeblock";
|
|
exports.types = {block: true};
|
|
|
|
exports.init = function(parser) {
|
|
this.parser = parser;
|
|
// Regexp to match
|
|
this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|\|([^\|\{\}]+))?\}\}(?:\r?\n|$)/mg;
|
|
};
|
|
|
|
exports.parse = function() {
|
|
// Move past the match
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
|
// Get the match details
|
|
var textRef = $tw.utils.trim(this.match[1]),
|
|
tr = $tw.utils.parseTextReference(textRef),
|
|
targetTitle = tr.title,
|
|
targetField = tr.field,
|
|
targetIndex = tr.index,
|
|
template = $tw.utils.trim(this.match[2]);
|
|
// Prepare the transclude widget
|
|
var transcludeNode = {
|
|
type: "element",
|
|
tag: "$transclude",
|
|
attributes: {},
|
|
isBlock: true
|
|
};
|
|
var tiddlerNode = {
|
|
type: "element",
|
|
tag: "$tiddler",
|
|
attributes: {
|
|
tiddler: {type: "string", value: targetTitle}
|
|
},
|
|
isBlock: true,
|
|
children: [transcludeNode]
|
|
};
|
|
if(template) {
|
|
transcludeNode.attributes.tiddler = {type: "string", value: template};
|
|
} else {
|
|
transcludeNode.attributes.tiddler = {type: "string", value: targetTitle};
|
|
if(targetField) {
|
|
transcludeNode.attributes.field = {type: "string", value: targetField};
|
|
}
|
|
if(targetIndex) {
|
|
transcludeNode.attributes.index = {type: "string", value: targetIndex};
|
|
}
|
|
}
|
|
return [tiddlerNode];
|
|
};
|
|
|
|
})();
|