-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: merge backlinks and backtranscludes indexer
- Loading branch information
Showing
6 changed files
with
133 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
editions/tw5.com/tiddlers/filters/examples/backtransclude.tid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]]">> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]]">> |