1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-18 13:12:52 +00:00

fix: ignore self-referential transclusion

This commit is contained in:
lin onetwo 2024-06-12 21:12:27 +08:00
parent 4bda8cfee6
commit 78d1bd1570
3 changed files with 24 additions and 6 deletions

View File

@ -75,7 +75,7 @@ BackSubIndexer.prototype._getTarget = function(tiddler) {
}
var parser = this.wiki.parseText(tiddler.fields.type, tiddler.fields.text, {});
if(parser) {
return this.wiki[this.extractor](parser.tree);
return this.wiki[this.extractor](parser.tree, tiddler.fields.title);
}
return [];
}

View File

@ -551,9 +551,9 @@ exports.getTiddlerBacklinks = function(targetTitle) {
/*
Return an array of tiddler titles that are directly transcluded within the given parse tree
Return an array of tiddler titles that are directly transcluded within the given parse tree. `title` is the tiddler being parsed, we will ignore its self-referential transclusions, only return
*/
exports.extractTranscludes = function(parseTreeRoot) {
exports.extractTranscludes = function(parseTreeRoot, title) {
// Count up the transcludes
var transcludes = [],
checkParseTree = function(parseTree, parentNode) {
@ -567,7 +567,8 @@ exports.extractTranscludes = function(parseTreeRoot) {
} else {
value = parseTreeNode.attributes.$tiddler.value;
}
if(transcludes.indexOf(value) === -1 && value !== undefined) {
// ignore empty value (like `{{!!field}}`), and deduplicate
if(value && transcludes.indexOf(value) === -1 && value !== title) {
transcludes.push(value);
}
}
@ -591,7 +592,8 @@ exports.getTiddlerTranscludes = function(title) {
// Parse the tiddler
var parser = self.parseTiddler(title);
if(parser) {
return self.extractTranscludes(parser.tree);
// this will ignore self-referential transclusions from `title`
return self.extractTranscludes(parser.tree, title);
}
return [];
});

View File

@ -187,7 +187,7 @@ describe('Backtranscludes and transclude filter tests', function() {
wiki.addTiddler({
title: 'TestOutgoing',
text: "{{!!created}}\n\nA transclude to {{!!title}}"});
text: "{{!!created}}\n\nAn implicit self-referential transclude to <$transclude $field='created'/> and <$transclude field='created'/>"});
it('should have no transclude', function() {
expect(wiki.filterTiddlers('TestOutgoing +[transcludes[]]').join(',')).toBe('');
@ -198,6 +198,22 @@ describe('Backtranscludes and transclude filter tests', function() {
});
});
describe('Explicit self transclusion', function() {
var wiki = new $tw.Wiki();
wiki.addTiddler({
title: 'TestOutgoing',
text: "{{TestOutgoing!!created}}\n\n<$transclude $tiddler='TestOutgoing' $field='created'/> and <$transclude tiddler='TestOutgoing' field='created'/>"});
it('should have no transclude', function() {
expect(wiki.filterTiddlers('TestOutgoing +[transcludes[]]').join(',')).toBe('');
});
it('should have no back transcludes', function() {
expect(wiki.filterTiddlers('TestOutgoing +[backtranscludes[]]').join(',')).toBe('');
});
});
describe('recognize soft transclusion defined by widget', function() {
var wiki = new $tw.Wiki();