From 61d5484f09629ae53d125af2240b657dddcd7a74 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Mon, 17 Jun 2024 17:50:23 +0800 Subject: [PATCH] refactor: move transcludes ast extractor to a file --- core/modules/wiki-extract.js | 90 ++++++++++++++++++++++++++++++++++++ core/modules/wiki.js | 76 ------------------------------ 2 files changed, 90 insertions(+), 76 deletions(-) create mode 100644 core/modules/wiki-extract.js diff --git a/core/modules/wiki-extract.js b/core/modules/wiki-extract.js new file mode 100644 index 000000000..9f4aad588 --- /dev/null +++ b/core/modules/wiki-extract.js @@ -0,0 +1,90 @@ +/*\ +title: $:/core/modules/wiki-extract.js +type: application/javascript +module-type: wikimethod + +AST information extractor for indexers. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Return an array of tiddler titles that are directly transcluded within the given parse tree. `title` is the tiddler being parsed, we will ignore its self-referential transclusions, only return +*/ +exports.extractTranscludes = function(parseTreeRoot, title) { + // Count up the transcludes + var transcludes = [], + checkParseTree = function(parseTree, parentNode) { + for(var t=0; t`) means self-referential transclusion. + value = title; + } else if(parseTreeNode.attributes.field && parseTreeNode.attributes.field.type === "string") { + // Old usage with Empty value (like `<$transclude field='created'/>`) + value = title; + } + // Deduplicate the result. + if(value && transcludes.indexOf(value) === -1) { + transcludes.push(value); + } + } + if(parseTreeNode.children) { + checkParseTree(parseTreeNode.children, parseTreeNode); + } + } + }; + checkParseTree(parseTreeRoot); + return transcludes; +}; + + +/* +Return an array of tiddler titles that are transcluded from the specified tiddler +*/ +exports.getTiddlerTranscludes = function(title) { + var self = this; + // We'll cache the transcludes so they only get computed if the tiddler changes + return this.getCacheForTiddler(title,"transcludes",function() { + // Parse the tiddler + var parser = self.parseTiddler(title); + if(parser) { + // this will ignore self-referential transclusions from `title` + return self.extractTranscludes(parser.tree, title); + } + return []; + }); +}; + +/* +Return an array of tiddler titles that transclude to the specified tiddler +*/ +exports.getTiddlerBacktranscludes = function(targetTitle) { + var self = this, + backIndexer = this.getIndexer("BackIndexer"), + backtranscludes = backIndexer && backIndexer.subIndexers.transclude.lookup(targetTitle); + + if(!backtranscludes) { + backtranscludes = []; + } + return backtranscludes; +}; + +})(); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index d57fd6098..dcb62d7d3 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -549,82 +549,6 @@ exports.getTiddlerBacklinks = function(targetTitle) { return backlinks; }; - -/* -Return an array of tiddler titles that are directly transcluded within the given parse tree. `title` is the tiddler being parsed, we will ignore its self-referential transclusions, only return - */ -exports.extractTranscludes = function(parseTreeRoot, title) { - // Count up the transcludes - var transcludes = [], - checkParseTree = function(parseTree, parentNode) { - for(var t=0; t`) means self-referential transclusion. - value = title; - } else if(parseTreeNode.attributes.field && parseTreeNode.attributes.field.type === "string") { - // Old usage with Empty value (like `<$transclude field='created'/>`) - value = title; - } - // Deduplicate the result. - if(value && transcludes.indexOf(value) === -1) { - transcludes.push(value); - } - } - if(parseTreeNode.children) { - checkParseTree(parseTreeNode.children, parseTreeNode); - } - } - }; - checkParseTree(parseTreeRoot); - return transcludes; -}; - - -/* -Return an array of tiddler titles that are transcluded from the specified tiddler -*/ -exports.getTiddlerTranscludes = function(title) { - var self = this; - // We'll cache the transcludes so they only get computed if the tiddler changes - return this.getCacheForTiddler(title,"transcludes",function() { - // Parse the tiddler - var parser = self.parseTiddler(title); - if(parser) { - // this will ignore self-referential transclusions from `title` - return self.extractTranscludes(parser.tree, title); - } - return []; - }); -}; - -/* -Return an array of tiddler titles that transclude to the specified tiddler -*/ -exports.getTiddlerBacktranscludes = function(targetTitle) { - var self = this, - backIndexer = this.getIndexer("BackIndexer"), - backtranscludes = backIndexer && backIndexer.subIndexers.transclude.lookup(targetTitle); - - if(!backtranscludes) { - backtranscludes = []; - } - return backtranscludes; -}; - /* Return a hashmap of tiddler titles that are referenced but not defined. Each value is the number of times the missing tiddler is referenced */