mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-04-23 03:03:14 +00:00
Added annotatedbacklinks filter
This commit is contained in:
parent
cb83f260f7
commit
1a8a62aa44
46
core/modules/filters/annotatedbacklinks.js
Normal file
46
core/modules/filters/annotatedbacklinks.js
Normal file
@ -0,0 +1,46 @@
|
||||
/*\
|
||||
title: $:/core/modules/filters/annotatedbacklinks.js
|
||||
type: application/javascript
|
||||
module-type: filteroperator
|
||||
|
||||
Filter operator for returning all the back links from a tiddler with a given annotation
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
function parseAnnotations(annotationstring){
|
||||
var annotations = {}
|
||||
if (annotationstring.length > 0){
|
||||
annotationstring.split(',').forEach(function(part){
|
||||
if (part.includes('=')){
|
||||
let subparts = part.split('=', 2);
|
||||
let key = subparts[0],
|
||||
value = subparts[1];
|
||||
annotations[key] = value;
|
||||
}else{
|
||||
annotations[part] = "*";
|
||||
}
|
||||
});
|
||||
}
|
||||
return annotations;
|
||||
}
|
||||
|
||||
/*
|
||||
Export our filter function
|
||||
*/
|
||||
exports.annotatedbacklinks = function(source,operator,options) {
|
||||
var results = new $tw.utils.LinkedList();
|
||||
source(function(tiddler,title) {
|
||||
var annotations = parseAnnotations(operator.operands[0]);
|
||||
console.log(annotations);
|
||||
results.pushTop(options.wiki.getTiddlerAnnotatedBacklinks(title, annotations));
|
||||
});
|
||||
return results.makeTiddlerIterator(options.wiki);
|
||||
};
|
||||
|
||||
|
||||
})();
|
@ -585,6 +585,7 @@ exports.extractAnnotatedLinks = function(parseTreeRoot, annotations) {
|
||||
checkParseTree = function(parseTree) {
|
||||
for(var t=0; t<parseTree.length; t++) {
|
||||
var parseTreeNode = parseTree[t];
|
||||
console.log(parseTreeNode);
|
||||
if(parseTreeNode.type === "link" && parseTreeNode.attributes.to && parseTreeNode.attributes.to.type === "string") {
|
||||
var add = false;
|
||||
for (const [key, value] of Object.entries(annotation_list)) {
|
||||
@ -621,39 +622,25 @@ Return an array of tiddler titles that are directly linked from the specified ti
|
||||
*/
|
||||
exports.getTiddlerAnnotatedLinks = function(title, annotations) {
|
||||
var self = this;
|
||||
var parser = self.parseTiddler(title);
|
||||
if(parser) {
|
||||
return self.extractAnnotatedLinks(parser.tree, annotations);
|
||||
}
|
||||
return [];
|
||||
// We'll cache the links so they only get computed if the tiddler changes
|
||||
return this.getCacheForTiddler(title,"annotatedlinks",function() {
|
||||
// Parse the tiddler
|
||||
var parser = self.parseTiddler(title);
|
||||
if(parser) {
|
||||
return self.extractAnnotatedLinks(parser.tree, annotations);
|
||||
}
|
||||
return [];
|
||||
});
|
||||
var parser = self.parseTiddler(title);
|
||||
if(parser) {
|
||||
return self.extractAnnotatedLinks(parser.tree, annotations);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
/*
|
||||
Return an array of tiddler titles that link to the specified tiddler
|
||||
*/
|
||||
exports.getTiddlerAnnotatedBacklinks = function(targetTitle) {
|
||||
var self = this,
|
||||
backlinksIndexer = this.getIndexer("BacklinksIndexer"),
|
||||
backlinks = backlinksIndexer && backlinksIndexer.lookup(targetTitle);
|
||||
|
||||
if(!backlinks) {
|
||||
backlinks = [];
|
||||
this.forEachTiddler(function(title,tiddler) {
|
||||
var links = self.getTiddlerAnnotatedLinks(title);
|
||||
if(links.indexOf(targetTitle) !== -1) {
|
||||
backlinks.push(title);
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.getTiddlerAnnotatedBacklinks = function(targetTitle, annotations) {
|
||||
var self = this;
|
||||
var backlinks = [];
|
||||
this.forEachTiddler(function(title,tiddler) {
|
||||
var links = self.getTiddlerAnnotatedLinks(title, annotations);
|
||||
if(links.indexOf(targetTitle) !== -1) {
|
||||
backlinks.push(title);
|
||||
}
|
||||
});
|
||||
return backlinks;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user