Skip to content

Commit

Permalink
refactor: merge backlinks and backtranscludes indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Sep 15, 2023
1 parent 624fdfc commit 27ca62a
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 182 deletions.
119 changes: 119 additions & 0 deletions core/modules/indexers/back-indexer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*\
title: $:/core/modules/indexers/back-indexer.js
type: application/javascript
module-type: indexer
By parsing the tiddler text, indexes the tiddlers' back links, back transclusions, block level back links.
\*/
function BackIndexer(wiki) {
this.wiki = wiki;
}

BackIndexer.prototype.init = function() {
this.subIndexers = {
link: new BackSubIndexer(this,"extractLinks"),
transclude: new BackSubIndexer(this,"extractTranscludes"),
};
};

BackIndexer.prototype.rebuild = function() {
$tw.utils.each(this.subIndexers,function(subIndexer) {
subIndexer.rebuild();
});
};

BackIndexer.prototype.update = function(updateDescriptor) {
$tw.utils.each(this.subIndexers,function(subIndexer) {
subIndexer.update(updateDescriptor);
});
};
function BackSubIndexer(indexer,extractor) {
this.wiki = indexer.wiki;
this.indexer = indexer;
this.extractor = extractor;
this.index = null; // Hashmap of tag title to {isSorted: bool, titles: [array]} or null if not yet initialised
}

BackSubIndexer.prototype.init = function() {
// lazy init until first lookup
this.index = null;
}

BackSubIndexer.prototype._init = function() {
/**
* {
* [target title, e.g. tiddler title being linked to]:
* {
* [source title, e.g. tiddler title that has link syntax in its text]: true
* }
* }
*/
this.index = Object.create(null);
var self = this;
this.wiki.forEachTiddler(function(sourceTitle,tiddler) {
var newTargets = self._getTarget(tiddler);
$tw.utils.each(newTargets, function(target) {
if(!self.index[target]) {
self.index[target] = Object.create(null);
}
self.index[target][sourceTitle] = true;
});
});
}

BackSubIndexer.prototype.rebuild = function() {
this.index = null;
}

/*
* Get things that is being referenced in the text, e.g. tiddler names in the link syntax.
*/
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 [];
}

BackSubIndexer.prototype.update = function(updateDescriptor) {
// lazy init/update until first lookup
if(!this.index) {
return;
}
var newTargets = [],
oldTargets = [],
self = this;
if(updateDescriptor.old.exists) {
oldTargets = this._getTarget(updateDescriptor.old.tiddler);
}
if(updateDescriptor.new.exists) {
newTargets = this._getTarget(updateDescriptor.new.tiddler);
}

$tw.utils.each(oldTargets,function(target) {
if(self.index[target]) {
delete self.index[target][updateDescriptor.old.tiddler.fields.title];
}
});
$tw.utils.each(newTargets,function(target) {
if(!self.index[target]) {
self.index[target] = Object.create(null);
}
self.index[target][updateDescriptor.new.tiddler.fields.title] = true;
});
}

BackSubIndexer.prototype.lookup = function(title) {
if(!this.index) {
this._init();
}
if(this.index[title]) {
return Object.keys(this.index[title]);
} else {
return [];
}
}

exports.BackIndexer = BackIndexer;
86 changes: 0 additions & 86 deletions core/modules/indexers/backlinks-index.js

This file was deleted.

86 changes: 0 additions & 86 deletions core/modules/indexers/backtranscludes-index.js

This file was deleted.

14 changes: 4 additions & 10 deletions core/modules/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ Return an array of tiddler titles that link to the specified tiddler
*/
exports.getTiddlerBacklinks = function(targetTitle) {
var self = this,
backlinksIndexer = this.getIndexer("BacklinksIndexer"),
backlinks = backlinksIndexer && backlinksIndexer.lookup(targetTitle);
backIndexer = this.getIndexer("BackIndexer"),
backlinks = backIndexer && backIndexer.subIndexers.link.lookup(targetTitle);

if(!backlinks) {
backlinks = [];
Expand Down Expand Up @@ -596,17 +596,11 @@ Return an array of tiddler titles that transclude to the specified tiddler
*/
exports.getTiddlerBacktranscludes = function(targetTitle) {
var self = this,
backtranscludesIndexer = this.getIndexer("BacktranscludesIndexer"),
backtranscludes = backtranscludesIndexer && backtranscludesIndexer.lookup(targetTitle);
backIndexer = this.getIndexer("BackIndexer"),
backtranscludes = backIndexer && backIndexer.subIndexers.transclude.lookup(targetTitle);

if(!backtranscludes) {
backtranscludes = [];
this.forEachTiddler(function(title,tiddler) {
var transcludes = self.getTiddlertranscludes(title);
if(transcludes.indexOf(targetTitle) !== -1) {
backtranscludes.push(title);
}
});
}
return backtranscludes;
};
Expand Down
5 changes: 5 additions & 0 deletions editions/tw5.com/tiddlers/filters/examples/backtransclude.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tags: [[backtranscludes Operator]] [[Operator Examples]]
title: backtranscludes Operator (Examples)
type: text/vnd.tiddlywiki

<<.operator-example 1 "[[Motovun Jack.jpg]backtranscludes[]]">>
5 changes: 5 additions & 0 deletions editions/tw5.com/tiddlers/filters/examples/transclude.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tags: [[transcludes Operator]] [[Operator Examples]]
title: transcludes Operator (Examples)
type: text/vnd.tiddlywiki

<<.operator-example 1 "[[Images in WikiText]transcludes[]]">>

0 comments on commit 27ca62a

Please sign in to comment.