Added annotatedbacklinks filter

This commit is contained in:
Matthias Bilger 2023-08-30 21:21:49 +02:00
parent cb83f260f7
commit 1a8a62aa44
2 changed files with 61 additions and 28 deletions

View 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);
};
})();

View File

@ -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;
};