From af709e266208507c550fc7300ea942e7ca016fc6 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Mon, 17 Oct 2022 12:22:24 +0100 Subject: [PATCH] First commit --- core/modules/indexers/backlinks-index.js | 22 +++++++++++---- core/modules/wiki.js | 35 ++++++++++++++++++++---- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/core/modules/indexers/backlinks-index.js b/core/modules/indexers/backlinks-index.js index 5902e2829..b02298c08 100644 --- a/core/modules/indexers/backlinks-index.js +++ b/core/modules/indexers/backlinks-index.js @@ -26,11 +26,23 @@ BacklinksIndexer.prototype.rebuild = function() { } BacklinksIndexer.prototype._getLinks = function(tiddler) { - var parser = this.wiki.parseText(tiddler.fields.type, tiddler.fields.text, {}); - if(parser) { - return this.wiki.extractLinks(parser.tree); - } - return []; + var parser = this.wiki.parseText(tiddler.fields.type,tiddler.fields.text,{}); + parser.tree = [{ + type: "importvariables", + attributes: { + filter: { + name: "filter", + type: "string", + value: "[[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]" + } + }, + isBlock: false, + children: parser.tree + }]; + var widget = this.wiki.makeWidget(parser,{document: $tw.fakeDocument, parseAsInline: false, variables: {currentTiddler: tiddler.fields.title}}); + var container = $tw.fakeDocument.createElement("div"); + widget.render(container,null); + return this.wiki.extractLinksFromWidgetTree(widget); } BacklinksIndexer.prototype.update = function(updateDescriptor) { diff --git a/core/modules/wiki.js b/core/modules/wiki.js index c3f272348..f8877bbc0 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -22,7 +22,8 @@ Adds the following properties to the wiki object: /*global $tw: false */ "use strict"; -var widget = require("$:/core/modules/widgets/widget.js"); +var widget = require("$:/core/modules/widgets/widget.js"), + LinkWidget = require("$:/core/modules/widgets/link.js").link; var USER_NAME_TITLE = "$:/status/UserName", TIMESTAMP_DISABLE_TITLE = "$:/config/TimestampDisable"; @@ -513,6 +514,29 @@ exports.extractLinks = function(parseTreeRoot) { return links; }; +/* +Return an array of tiddelr titles that are linked within the given widget tree + */ +exports.extractLinksFromWidgetTree = function(widget) { + // Count up the links + var links = [], + checkWidget = function(widget) { + if(widget instanceof LinkWidget) { + var value = widget.to; + if(links.indexOf(value) === -1) { + links.push(value); + } + } + if(widget.children) { + $tw.utils.each(widget.children,function(widget) { + checkWidget(widget); + }); + } + }; + checkWidget(widget); + return links; +}; + /* Return an array of tiddler titles that are directly linked from the specified tiddler */ @@ -521,11 +545,10 @@ exports.getTiddlerLinks = function(title) { // We'll cache the links so they only get computed if the tiddler changes return this.getCacheForTiddler(title,"links",function() { // Parse the tiddler - var parser = self.parseTiddler(title); - if(parser) { - return self.extractLinks(parser.tree); - } - return []; + var container = $tw.fakeDocument.createElement("div"); + var widget = self.makeTranscludeWidget(title,{document: $tw.fakeDocument, parseAsInline: false,variables: {currentTiddler: title},importPageMacros: true}); + widget.render(container,null); + return self.extractLinksFromWidgetTree(widget); }); };