mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-08-04 18:08:55 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c39e7b44e4 | ||
|
|
b0d99f3bd3 | ||
|
|
91e7a62c13 | ||
|
|
c3ca72ebe2 | ||
|
|
1ed3217239 | ||
|
|
e59f97b9c5 |
@@ -1577,6 +1577,8 @@ $tw.Wiki.prototype.processSafeMode = function() {
|
|||||||
this.addTiddler(new $tw.Tiddler({title: titleReportTiddler, text: report.join("\n\n")}));
|
this.addTiddler(new $tw.Tiddler({title: titleReportTiddler, text: report.join("\n\n")}));
|
||||||
// Set $:/DefaultTiddlers to point to our report
|
// Set $:/DefaultTiddlers to point to our report
|
||||||
this.addTiddler(new $tw.Tiddler({title: "$:/DefaultTiddlers", text: "[[" + titleReportTiddler + "]]"}));
|
this.addTiddler(new $tw.Tiddler({title: "$:/DefaultTiddlers", text: "[[" + titleReportTiddler + "]]"}));
|
||||||
|
// Switch to the safe mode layout
|
||||||
|
this.addTiddler({title: "$:/layout", text: "$:/core/ui/SafeLayout"});
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ Manager/Controls/Show/Option/Tiddlers: tiddlers
|
|||||||
Manager/Controls/Show/Prompt: Show:
|
Manager/Controls/Show/Prompt: Show:
|
||||||
Manager/Controls/Sort/Prompt: Sort by:
|
Manager/Controls/Sort/Prompt: Sort by:
|
||||||
Manager/Item/Colour: Colour
|
Manager/Item/Colour: Colour
|
||||||
|
Manager/Item/Editor: Editor
|
||||||
Manager/Item/Fields: Fields
|
Manager/Item/Fields: Fields
|
||||||
Manager/Item/Icon/None: (none)
|
Manager/Item/Icon/None: (none)
|
||||||
Manager/Item/Icon: Icon
|
Manager/Item/Icon: Icon
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ function updateLocationHash(options) {
|
|||||||
$tw.utils.copyToClipboard(url,{successNotification: options.successNotification, failureNotification: options.failureNotification});
|
$tw.utils.copyToClipboard(url,{successNotification: options.successNotification, failureNotification: options.failureNotification});
|
||||||
}
|
}
|
||||||
// Only change the location hash if we must, thus avoiding unnecessary onhashchange events
|
// Only change the location hash if we must, thus avoiding unnecessary onhashchange events
|
||||||
if($tw.utils.getLocationHash() !== $tw.locationHash) {
|
if($tw.utils.getLocationHash() !== $tw.locationHash && !$tw.safeMode) {
|
||||||
if(options.updateHistory === "yes") {
|
if(options.updateHistory === "yes") {
|
||||||
// Assign the location hash so that history is updated
|
// Assign the location hash so that history is updated
|
||||||
window.location.hash = $tw.locationHash;
|
window.location.hash = $tw.locationHash;
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
title: $:/Manager/ItemMain/Editor
|
||||||
|
tags: $:/tags/Manager/ItemMain
|
||||||
|
caption: {{$:/language/Manager/Item/Editor}}
|
||||||
|
|
||||||
|
<$edit-text tag="textarea" class="tc-edit-texteditor tc-edit-texteditor-body tc-max-width"/>
|
||||||
@@ -13,3 +13,8 @@ caption: {{$:/language/Manager/Item/Tools}}
|
|||||||
{{$:/core/images/edit-button}} edit
|
{{$:/core/images/edit-button}} edit
|
||||||
</$button>
|
</$button>
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<$button message="tm-delete-tiddler" param=<<currentTiddler>>>
|
||||||
|
{{$:/core/images/delete-button}} delete
|
||||||
|
</$button>
|
||||||
|
</p>
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
title: $:/core/ui/SafeLayout
|
||||||
|
tags: $:/tags/Layout
|
||||||
|
name: Safe Mode Layout
|
||||||
|
description: Safe Mode Layout
|
||||||
|
icon: $:/core/images/list
|
||||||
|
|
||||||
|
\whitespace trim
|
||||||
|
|
||||||
|
\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]
|
||||||
|
|
||||||
|
\define tv-config-toolbar-class() tc-btn-invisible
|
||||||
|
\define tv-config-toolbar-icons() yes
|
||||||
|
\define tv-config-toolbar-text() no
|
||||||
|
|
||||||
|
<div style="margin: 1em;">
|
||||||
|
|
||||||
|
<$navigator story="$:/StoryList" history="$:/HistoryList">
|
||||||
|
|
||||||
|
<h2>Layout</h2>
|
||||||
|
|
||||||
|
{{$:/snippets/LayoutSwitcher}}
|
||||||
|
|
||||||
|
<h2>Controls</h2>
|
||||||
|
|
||||||
|
<div class="tc-page-controls">
|
||||||
|
<$list filter="[all[shadows+tiddlers]tag[$:/tags/PageControls]!has[draft.of]]">
|
||||||
|
<$transclude mode="inline"/>
|
||||||
|
</$list>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2>Tiddlers</h2>
|
||||||
|
|
||||||
|
<$transclude tiddler="$:/Manager" mode="block"/>
|
||||||
|
|
||||||
|
</$navigator>
|
||||||
|
|
||||||
|
</div>
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
title: $:/state/popup/manager/item/$:/Manager/
|
title: $:/state/popup/manager/item/$:/Manager/
|
||||||
|
|
||||||
ItemMain/RawText: hide
|
ItemMain/RawText: hide
|
||||||
|
ItemMain/Editor: hide
|
||||||
|
|||||||
@@ -12,3 +12,15 @@ eg="""<<tabs "[tag[sampletab]]" "SampleTabTwo" "$:/state/tab2" "tc-vertical">>""
|
|||||||
|
|
||||||
<$macrocall $name=".example" n="3"
|
<$macrocall $name=".example" n="3"
|
||||||
eg="""<<tabs "[tag[sampletab]nsort[order]]" "SampleTabThree" "$:/state/tab3" "tc-vertical">>"""/>
|
eg="""<<tabs "[tag[sampletab]nsort[order]]" "SampleTabThree" "$:/state/tab3" "tc-vertical">>"""/>
|
||||||
|
|
||||||
|
The following example sets the default tab to be the first tiddler selected in the filter and makes the saved state non-persistent (by using "~$:/temp/"):
|
||||||
|
|
||||||
|
<$macrocall $name=".example" n="4"
|
||||||
|
eg="""<$set name=tl filter="[tag[sampletab]nsort[order]]">
|
||||||
|
<$transclude $variable=tabs tabsList=<<tl>> default={{{[enlist<tl>]}}} state="$:/temp/state/tab" class="tc-vertical"/>
|
||||||
|
</$set>"""/>
|
||||||
|
|
||||||
|
<<.from-version "5.4.0">> Dynamic parameters can be used to specify the default tab:
|
||||||
|
|
||||||
|
<$macrocall $name=".example" n="5"
|
||||||
|
eg="""<<tabs "[tag[sampletab]nsort[order]]" default={{{[tag[sampletab]nsort[order]]}}} state="$:/temp/state/tab" class="tc-vertical">>"""/>
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
title: $:/changenotes/5.4.0/#9397
|
|
||||||
description: Fix critical freelinks bugs: first character loss and false positive matches in v5.4.0
|
|
||||||
release: 5.4.0
|
|
||||||
tags: $:/tags/ChangeNote
|
|
||||||
change-type: bugfix
|
|
||||||
change-category: plugin
|
|
||||||
github-links: https://github.com/TiddlyWiki/TiddlyWiki5/pull/9084 https://github.com/TiddlyWiki/TiddlyWiki5/pull/9397
|
|
||||||
github-contributors: s793016
|
|
||||||
|
|
||||||
This note addresses two major bugs introduced in the Freelinks plugin with the v5.4.0 release:
|
|
||||||
|
|
||||||
Fixes:
|
|
||||||
* First Character Loss: The first character of a matched word would incorrectly disappear (e.g., "The" became "he"). This was fixed by correctly timing the filtering of the current tiddler's title during match validation, ensuring proper substring handling.
|
|
||||||
* False Positive Matches: Unrelated words (like "it is" or "Choose") would incorrectly link to a tiddler title. This was resolved by fixing wrong output merging in the Aho-Corasick failure-link handling, eliminating spurious matches from intermediate nodes, and adding cycle detection.
|
|
||||||
|
|
||||||
Impact:
|
|
||||||
* Significantly improved correctness and reliability of automatic linking for all users, especially in multilingual and large wikis.
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
title: $:/changenotes/5.4.0/#9676
|
||||||
|
description: Fix critical freelinks bugs: first character loss and false positive matches in v5.4.0
|
||||||
|
release: 5.4.0
|
||||||
|
tags: $:/tags/ChangeNote
|
||||||
|
change-type: bugfix
|
||||||
|
change-category: plugin
|
||||||
|
github-links: https://github.com/TiddlyWiki/TiddlyWiki5/pull/9084 https://github.com/TiddlyWiki/TiddlyWiki5/pull/9397 https://github.com/TiddlyWiki/TiddlyWiki5/pull/9676
|
||||||
|
github-contributors: s793016
|
||||||
|
|
||||||
|
Fixes and optimizations to the Freelinks plugin's Aho-Corasick implementation following #9397.
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* Failure Links Non-Functional (Critical): The failure link map used a plain object `{}` with trie nodes as keys. Since all JavaScript objects coerce to the same string `[object Object]`, every node resolved to the same map entry. Failure links were silently broken for all overlapping patterns. Fixed by replacing with `WeakMap`.
|
||||||
|
* Cache Rebuilt on Every UI Interaction (Performance): Any `$:/state/...` update (e.g. clicking tabs) would trigger a full Aho-Corasick rebuild, causing severe lag on large wikis. The `refresh` logic now ignores system tiddlers, with an explicit allowlist for plugin config tiddlers.
|
||||||
|
* Short Match Blocking Longer Match: A shorter title appearing earlier (e.g. "The New") could prevent a longer overlapping title (e.g. "New York City") from matching. Replaced left-to-right greedy selection with global length-first sorting and interval occupation tracking.
|
||||||
|
* Unicode Index Desync in ignoreCase Mode: Calling `toLowerCase()` on the full text before searching could change string length (e.g. Turkish "İ" expands), causing `substring()` to split Emoji surrogate pairs and produce garbage output. Case conversion is now done per-character during search.
|
||||||
|
* Removed Vestigial Regex Escaping: `escapeRegExp()` was called during trie construction but Aho-Corasick operates on literal character transitions, not regex. Removed.
|
||||||
|
|
||||||
|
Impact:
|
||||||
|
* Overlapping titles now match correctly for the first time.
|
||||||
|
* No cache rebuilds during normal UI interactions on large wikis.
|
||||||
|
* Correct longest-match behavior for titles sharing substrings.
|
||||||
|
* Safe Emoji and complex Unicode handling in case-insensitive mode.
|
||||||
|
|
||||||
|
|
||||||
|
#9397
|
||||||
|
This note addresses two major bugs introduced in the Freelinks plugin with the v5.4.0 release:
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* First Character Loss: The first character of a matched word would incorrectly disappear (e.g., "The" became "he"). This was fixed by correctly timing the filtering of the current tiddler's title during match validation, ensuring proper substring handling.
|
||||||
|
* False Positive Matches: Unrelated words (like "it is" or "Choose") would incorrectly link to a tiddler title. This was resolved by fixing wrong output merging in the Aho-Corasick failure-link handling, eliminating spurious matches from intermediate nodes, and adding cycle detection.
|
||||||
|
|
||||||
|
Impact:
|
||||||
|
* Significantly improved correctness and reliability of automatic linking for all users, especially in multilingual and large wikis.
|
||||||
|
|
||||||
|
|
||||||
|
#9084
|
||||||
|
This change introduces a fully optimized override of the core text widget, integrating an enhanced Aho-Corasick algorithm for automatic linkification of tiddler titles within text (freelinks). The new implementation prioritizes performance for large wikis and correct support for non-Latin scripts such as Chinese.
|
||||||
|
|
||||||
|
Highlights:
|
||||||
|
- Full switch from regex-based matching to a custom, robust Aho-Corasick engine dedicated to rapid, multi-pattern title detection—drastically decreasing linkification time (tested: 1–5s reduced to 100–500ms on ~12,000 tiddlers).
|
||||||
|
- Handles extremely large title sets gracefully, including a chunked insertion process and use of a persistent cache (`$:/config/Freelinks/PersistAhoCorasickCache`) to further accelerate subsequent linking operations in large/active wikis.
|
||||||
|
- Improvements for CJK and non-Latin text: supports linking using long or full-width symbol titles such as ':' (U+FF1A) with no split or mismatch.
|
||||||
|
- Smart prioritization: longer titles are automatically matched before shorter, more ambiguous ones, preventing partial/incorrect linking.
|
||||||
|
- Actively skips self-linking in the current tiddler and prevents overlapping matches for clean, deterministic linkification.
|
||||||
|
- End users with large or multilingual wikis see massive performance boost and 100% accurate linking for complex, full-width, or multi-language titles.
|
||||||
|
- New options for persistent match cache and word boundary checking (`$:/config/Freelinks/WordBoundary`), both can be tuned based on wiki size and content language needs.
|
||||||
|
- Safe for gradual rollout: legacy behavior is preserved if the new freelinks override is not enabled.
|
||||||
@@ -4,10 +4,10 @@
|
|||||||
"tiddlywiki/browser-sniff",
|
"tiddlywiki/browser-sniff",
|
||||||
"tiddlywiki/confetti",
|
"tiddlywiki/confetti",
|
||||||
"tiddlywiki/dynannotate",
|
"tiddlywiki/dynannotate",
|
||||||
"tiddlywiki/tour",
|
|
||||||
"tiddlywiki/internals",
|
"tiddlywiki/internals",
|
||||||
"tiddlywiki/menubar",
|
"tiddlywiki/menubar",
|
||||||
"tiddlywiki/railroad"
|
"tiddlywiki/railroad",
|
||||||
|
"tiddlywiki/tour"
|
||||||
],
|
],
|
||||||
"themes": [
|
"themes": [
|
||||||
"tiddlywiki/vanilla",
|
"tiddlywiki/vanilla",
|
||||||
|
|||||||
@@ -1,51 +1,23 @@
|
|||||||
/*\
|
/*\
|
||||||
|
|
||||||
title: $:/core/modules/utils/aho-corasick.js
|
title: $:/core/modules/utils/aho-corasick.js
|
||||||
type: application/javascript
|
type: application/javascript
|
||||||
module-type: utils
|
module-type: utils
|
||||||
|
|
||||||
Optimized Aho-Corasick string matching algorithm implementation with enhanced performance and error handling for TiddlyWiki freelinking functionality.
|
Optimized Aho-Corasick string matching algorithm implementation with enhanced performance
|
||||||
|
and error handling for TiddlyWiki freelinking functionality.
|
||||||
|
|
||||||
Useage:
|
- Uses WeakMap for failure links (required; plain object keys would collide).
|
||||||
|
- search() converts case per character to avoid Unicode index desync.
|
||||||
|
- Optional word boundary filtering: CJK always allowed; Latin requires non-word chars around.
|
||||||
|
|
||||||
Initialization:
|
|
||||||
Create an AhoCorasick instance: var ac = new AhoCorasick();
|
|
||||||
After initialization, the trie and failure structures are automatically created to store patterns and failure links.
|
|
||||||
|
|
||||||
Adding Patterns:
|
|
||||||
Call addPattern(pattern, index) to add a pattern, e.g., ac.addPattern("[[Link]]", 0);.
|
|
||||||
pattern is the string to match, and index is an identifier for tracking results.
|
|
||||||
Multiple patterns can be added, stored in the trie structure.
|
|
||||||
|
|
||||||
Building Failure Links:
|
|
||||||
Call buildFailureLinks() to construct failure links for efficient multi-pattern matching.
|
|
||||||
Includes a maximum node limit (default 100,000 or 15 times the pattern count) to prevent excessive computation.
|
|
||||||
|
|
||||||
Performing Search:
|
|
||||||
Use search(text, useWordBoundary) to find pattern matches in the text.
|
|
||||||
text is the input string, and useWordBoundary (boolean) controls whether to enforce word boundary checks.
|
|
||||||
Returns an array of match results, each containing pattern (matched pattern), index (start position), length (pattern length), and titleIndex (pattern identifier).
|
|
||||||
|
|
||||||
Word Boundary Check:
|
|
||||||
If useWordBoundary is true, only matches surrounded by non-word characters (letters, digits, or underscores) are returned.
|
|
||||||
|
|
||||||
Cleanup and Statistics:
|
|
||||||
Use clear() to reset the trie and failure links, freeing memory.
|
|
||||||
Use getStats() to retrieve statistics, including node count (nodeCount), pattern count (patternCount), and failure link count (failureLinks).
|
|
||||||
|
|
||||||
Notes
|
|
||||||
Performance Considerations: The Aho-Corasick trie may consume significant memory with a large number of patterns. Limit the number of patterns (e.g., <10,000) for optimal performance.
|
|
||||||
Error Handling: The module includes maximum node and failure depth limits (maxFailureDepth) to prevent infinite loops or memory overflow.
|
|
||||||
Word Boundary: Enabling useWordBoundary ensures more precise matches, ideal for link detection scenarios.
|
|
||||||
Compatibility: Ensure compatibility with other TiddlyWiki modules (e.g., wikiparser.js) when processing WikiText.
|
|
||||||
Debugging: Use getStats() to inspect the trie structure's size and ensure it does not overload browser memory.
|
|
||||||
|
|
||||||
\*/
|
\*/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function AhoCorasick() {
|
function AhoCorasick() {
|
||||||
this.trie = {};
|
this.trie = {};
|
||||||
this.failure = {};
|
this.failure = new WeakMap();
|
||||||
this.maxFailureDepth = 100;
|
this.maxFailureDepth = 100;
|
||||||
this.patternCount = 0;
|
this.patternCount = 0;
|
||||||
}
|
}
|
||||||
@@ -54,198 +26,164 @@ AhoCorasick.prototype.addPattern = function(pattern, index) {
|
|||||||
if(!pattern || typeof pattern !== "string" || pattern.length === 0) {
|
if(!pattern || typeof pattern !== "string" || pattern.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var node = this.trie;
|
var node = this.trie;
|
||||||
|
|
||||||
for(var i = 0; i < pattern.length; i++) {
|
for(var i = 0; i < pattern.length; i++) {
|
||||||
var char = pattern[i];
|
var ch = pattern[i];
|
||||||
if(!node[char]) {
|
if(!node[ch]) {
|
||||||
node[char] = {};
|
node[ch] = {};
|
||||||
}
|
}
|
||||||
node = node[char];
|
node = node[ch];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!node.$) {
|
if(!node.$) {
|
||||||
node.$ = [];
|
node.$ = [];
|
||||||
}
|
}
|
||||||
node.$.push({
|
node.$.push({
|
||||||
pattern: pattern,
|
pattern: pattern,
|
||||||
index: index,
|
index: index,
|
||||||
length: pattern.length
|
length: pattern.length
|
||||||
});
|
});
|
||||||
|
|
||||||
this.patternCount++;
|
this.patternCount++;
|
||||||
};
|
};
|
||||||
|
|
||||||
AhoCorasick.prototype.buildFailureLinks = function() {
|
AhoCorasick.prototype.buildFailureLinks = function() {
|
||||||
var queue = [];
|
var queue = [];
|
||||||
var root = this.trie;
|
var root = this.trie;
|
||||||
this.failure[root] = root;
|
var self = this;
|
||||||
|
|
||||||
for(var char in root) {
|
this.failure = new WeakMap();
|
||||||
if(root[char] && char !== "$") {
|
this.failure.set(root, root);
|
||||||
this.failure[root[char]] = root;
|
|
||||||
queue.push(root[char]);
|
for(var ch in root) {
|
||||||
|
if(ch === "$") continue;
|
||||||
|
if(root[ch] && typeof root[ch] === "object") {
|
||||||
|
this.failure.set(root[ch], root);
|
||||||
|
queue.push(root[ch]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var processedNodes = 0;
|
var processedNodes = 0;
|
||||||
var maxNodes = Math.max(100000, this.patternCount * 15);
|
var maxNodes = Math.max(100000, this.patternCount * 15);
|
||||||
|
|
||||||
while(queue.length > 0 && processedNodes < maxNodes) {
|
|
||||||
var node = queue.shift();
|
|
||||||
processedNodes++;
|
|
||||||
|
|
||||||
for(var char in node) {
|
|
||||||
if(node[char] && char !== "$") {
|
|
||||||
var child = node[char];
|
|
||||||
var fail = this.failure[node];
|
|
||||||
var failureDepth = 0;
|
|
||||||
|
|
||||||
while(fail && !fail[char] && failureDepth < this.maxFailureDepth) {
|
|
||||||
fail = this.failure[fail];
|
|
||||||
failureDepth++;
|
|
||||||
}
|
|
||||||
|
|
||||||
var failureLink = (fail && fail[char]) ? fail[char] : root;
|
|
||||||
this.failure[child] = failureLink;
|
|
||||||
|
|
||||||
// Do not merge outputs from failure links during build
|
|
||||||
// Instead, collect matches dynamically by traversing failure links during search
|
|
||||||
|
|
||||||
queue.push(child);
|
while(queue.length > 0) {
|
||||||
|
if(processedNodes++ >= maxNodes) {
|
||||||
|
throw new Error("Aho-Corasick: buildFailureLinks exceeded maximum nodes (" + maxNodes + ")");
|
||||||
|
}
|
||||||
|
var node = queue.shift();
|
||||||
|
|
||||||
|
for(var edge in node) {
|
||||||
|
if(edge === "$") continue;
|
||||||
|
var child = node[edge];
|
||||||
|
if(!child || typeof child !== "object") continue;
|
||||||
|
|
||||||
|
var fail = self.failure.get(node) || root;
|
||||||
|
var depth = 0;
|
||||||
|
|
||||||
|
while(fail !== root && !fail[edge] && depth < self.maxFailureDepth) {
|
||||||
|
fail = self.failure.get(fail) || root;
|
||||||
|
depth++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nextFail = (fail[edge] && fail[edge] !== child) ? fail[edge] : root;
|
||||||
|
self.failure.set(child, nextFail);
|
||||||
|
|
||||||
|
if(nextFail.$) {
|
||||||
|
if(!child.$) child.$ = [];
|
||||||
|
child.$ = child.$.concat(nextFail.$);
|
||||||
|
}
|
||||||
|
|
||||||
|
queue.push(child);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(processedNodes >= maxNodes) {
|
|
||||||
throw new Error("Aho-Corasick: buildFailureLinks exceeded maximum nodes (" + maxNodes + ")");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
AhoCorasick.prototype.search = function(text, useWordBoundary) {
|
AhoCorasick.prototype.search = function(text, useWordBoundary, ignoreCase) {
|
||||||
if(!text || typeof text !== "string" || text.length === 0) {
|
if(!text || typeof text !== "string" || text.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var matches = [];
|
var matches = [];
|
||||||
var node = this.trie;
|
var node = this.trie;
|
||||||
|
var root = this.trie;
|
||||||
var textLength = text.length;
|
var textLength = text.length;
|
||||||
|
|
||||||
var maxMatches = Math.min(textLength * 2, 10000);
|
var maxMatches = Math.min(textLength * 2, 10000);
|
||||||
|
|
||||||
for(var i = 0; i < textLength; i++) {
|
for(var i = 0; i < textLength; i++) {
|
||||||
var char = text[i];
|
var ch = ignoreCase ? text[i].toLowerCase() : text[i];
|
||||||
var transitionCount = 0;
|
|
||||||
|
while(node !== root && !node[ch]) {
|
||||||
// Follow failure links to find a valid transition
|
node = this.failure.get(node) || root;
|
||||||
while(node && !node[char] && node !== this.trie && transitionCount < this.maxFailureDepth) {
|
|
||||||
node = this.failure[node] || this.trie;
|
|
||||||
transitionCount++;
|
|
||||||
}
|
}
|
||||||
|
if(node[ch]) {
|
||||||
if(node && node[char]) {
|
node = node[ch];
|
||||||
node = node[char];
|
|
||||||
} else {
|
|
||||||
node = this.trie;
|
|
||||||
if(this.trie[char]) {
|
|
||||||
node = this.trie[char];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traverse the current node and its failure link chain to gather all patterns
|
if(node.$) {
|
||||||
var currentNode = node;
|
var outputs = node.$;
|
||||||
var collectCount = 0;
|
for(var j = 0; j < outputs.length && matches.length < maxMatches; j++) {
|
||||||
var visitedNodes = new Set();
|
var out = outputs[j];
|
||||||
|
var matchStart = i - out.length + 1;
|
||||||
while(currentNode && collectCount < 10) {
|
var matchEnd = i + 1;
|
||||||
// Prevent infinite loops
|
if(matchStart < 0) continue;
|
||||||
if(visitedNodes.has(currentNode)) {
|
|
||||||
break;
|
if(useWordBoundary && !this.isWordBoundaryMatch(text, matchStart, matchEnd)) {
|
||||||
}
|
continue;
|
||||||
visitedNodes.add(currentNode);
|
|
||||||
|
|
||||||
// Only collect outputs from the current node (not merged ones)
|
|
||||||
if(currentNode.$) {
|
|
||||||
var outputs = currentNode.$;
|
|
||||||
for(var j = 0; j < outputs.length && matches.length < maxMatches; j++) {
|
|
||||||
var output = outputs[j];
|
|
||||||
var matchStart = i - output.length + 1;
|
|
||||||
var matchEnd = i + 1;
|
|
||||||
|
|
||||||
var matchedText = text.substring(matchStart, matchEnd);
|
|
||||||
if(matchedText !== output.pattern) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(useWordBoundary && !this.isWordBoundaryMatch(text, matchStart, matchEnd)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
matches.push({
|
|
||||||
pattern: output.pattern,
|
|
||||||
index: matchStart,
|
|
||||||
length: output.length,
|
|
||||||
titleIndex: output.index
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matches.push({
|
||||||
|
pattern: out.pattern,
|
||||||
|
index: matchStart,
|
||||||
|
length: out.length,
|
||||||
|
titleIndex: out.index
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
currentNode = this.failure[currentNode];
|
|
||||||
if(currentNode === this.trie) break;
|
|
||||||
collectCount++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches;
|
return matches;
|
||||||
};
|
};
|
||||||
|
|
||||||
AhoCorasick.prototype.isWordBoundaryMatch = function(text, start, end) {
|
AhoCorasick.prototype.isWordBoundaryMatch = function(text, start, end) {
|
||||||
|
var matchedText = text.substring(start, end);
|
||||||
|
|
||||||
|
if(/[\u3400-\u9FFF\uF900-\uFAFF]/.test(matchedText)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
var beforeChar = start > 0 ? text[start - 1] : "";
|
var beforeChar = start > 0 ? text[start - 1] : "";
|
||||||
var afterChar = end < text.length ? text[end] : "";
|
var afterChar = end < text.length ? text[end] : "";
|
||||||
|
|
||||||
var isWordChar = function(char) {
|
var isLatinWordChar = function(char) {
|
||||||
return /[a-zA-Z0-9_\u00C0-\u00FF]/.test(char);
|
return /[a-zA-Z0-9_\u00C0-\u00FF]/.test(char);
|
||||||
};
|
};
|
||||||
|
|
||||||
var beforeIsWord = beforeChar && isWordChar(beforeChar);
|
return !isLatinWordChar(beforeChar) && !isLatinWordChar(afterChar);
|
||||||
var afterIsWord = afterChar && isWordChar(afterChar);
|
|
||||||
|
|
||||||
return !beforeIsWord && !afterIsWord;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
AhoCorasick.prototype.clear = function() {
|
AhoCorasick.prototype.clear = function() {
|
||||||
this.trie = {};
|
this.trie = {};
|
||||||
this.failure = {};
|
this.failure = new WeakMap();
|
||||||
this.patternCount = 0;
|
this.patternCount = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
AhoCorasick.prototype.getStats = function() {
|
AhoCorasick.prototype.getStats = function() {
|
||||||
var nodeCount = 0;
|
var nodeCount = 0;
|
||||||
// eslint-disable-next-line no-unused-vars
|
|
||||||
var patternCount = 0;
|
|
||||||
var failureCount = 0;
|
|
||||||
|
|
||||||
function countNodes(node) {
|
function countNodes(node) {
|
||||||
if(!node) return;
|
if(!node) return;
|
||||||
nodeCount++;
|
nodeCount++;
|
||||||
if(node.$) {
|
|
||||||
patternCount += node.$.length;
|
|
||||||
}
|
|
||||||
for(var key in node) {
|
for(var key in node) {
|
||||||
if(node[key] && typeof node[key] === "object" && key !== "$") {
|
if(key === "$") continue;
|
||||||
|
if(node[key] && typeof node[key] === "object") {
|
||||||
countNodes(node[key]);
|
countNodes(node[key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
countNodes(this.trie);
|
countNodes(this.trie);
|
||||||
|
|
||||||
failureCount += Object.keys(this.failure).length;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
nodeCount: nodeCount,
|
nodeCount: nodeCount,
|
||||||
patternCount: this.patternCount,
|
patternCount: this.patternCount,
|
||||||
failureLinks: failureCount
|
failureLinks: this.patternCount
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
/*\
|
/*\
|
||||||
|
|
||||||
title: $:/core/modules/widgets/text.js
|
title: $:/core/modules/widgets/text.js
|
||||||
type: application/javascript
|
type: application/javascript
|
||||||
module-type: widget
|
module-type: widget
|
||||||
|
|
||||||
An optimized override of the core text widget that automatically linkifies the text, with support for non-Latin languages like Chinese, prioritizing longer titles, skipping processed matches, excluding the current tiddler title from linking, and handling large title sets with enhanced Aho-Corasick algorithm.
|
Optimized override of the core text widget that automatically linkifies text.
|
||||||
|
- Supports non-Latin languages like Chinese.
|
||||||
|
- Global longest-match priority, then removes overlaps.
|
||||||
|
- Excludes current tiddler title from linking.
|
||||||
|
- Uses Aho-Corasick for performance.
|
||||||
|
|
||||||
\*/
|
\*/
|
||||||
|
|
||||||
@@ -18,28 +23,6 @@ var Widget = require("$:/core/modules/widgets/widget.js").widget,
|
|||||||
ElementWidget = require("$:/core/modules/widgets/element.js").element,
|
ElementWidget = require("$:/core/modules/widgets/element.js").element,
|
||||||
AhoCorasick = require("$:/core/modules/utils/aho-corasick.js").AhoCorasick;
|
AhoCorasick = require("$:/core/modules/utils/aho-corasick.js").AhoCorasick;
|
||||||
|
|
||||||
var ESCAPE_REGEX = /[\\^$*+?.()|[\]{}]/g;
|
|
||||||
|
|
||||||
function escapeRegExp(str) {
|
|
||||||
try {
|
|
||||||
return str.replace(ESCAPE_REGEX, "\\$&");
|
|
||||||
} catch(e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function FastPositionSet() {
|
|
||||||
this.set = new Set();
|
|
||||||
}
|
|
||||||
|
|
||||||
FastPositionSet.prototype.add = function(pos) {
|
|
||||||
this.set.add(pos);
|
|
||||||
};
|
|
||||||
|
|
||||||
FastPositionSet.prototype.has = function(pos) {
|
|
||||||
return this.set.has(pos);
|
|
||||||
};
|
|
||||||
|
|
||||||
var TextNodeWidget = function(parseTreeNode,options) {
|
var TextNodeWidget = function(parseTreeNode,options) {
|
||||||
this.initialise(parseTreeNode,options);
|
this.initialise(parseTreeNode,options);
|
||||||
};
|
};
|
||||||
@@ -54,138 +37,121 @@ TextNodeWidget.prototype.render = function(parent,nextSibling) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TextNodeWidget.prototype.execute = function() {
|
TextNodeWidget.prototype.execute = function() {
|
||||||
var self = this,
|
var self = this;
|
||||||
ignoreCase = self.getVariable("tv-freelinks-ignore-case",{defaultValue:"no"}).trim() === "yes";
|
var ignoreCase = self.getVariable("tv-freelinks-ignore-case",{defaultValue:"no"}).trim() === "yes";
|
||||||
|
|
||||||
var childParseTree = [{
|
var childParseTree = [{
|
||||||
type: "plain-text",
|
type: "plain-text",
|
||||||
text: this.getAttribute("text",this.parseTreeNode.text || "")
|
text: this.getAttribute("text",this.parseTreeNode.text || "")
|
||||||
}];
|
}];
|
||||||
|
|
||||||
var text = childParseTree[0].text;
|
var text = childParseTree[0].text;
|
||||||
|
|
||||||
if(!text || text.length < 2) {
|
if(!text || text.length < 2) {
|
||||||
this.makeChildWidgets(childParseTree);
|
this.makeChildWidgets(childParseTree);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.getVariable("tv-wikilinks",{defaultValue:"yes"}) !== "no" &&
|
if(this.getVariable("tv-wikilinks",{defaultValue:"yes"}) !== "no" &&
|
||||||
this.getVariable("tv-freelinks",{defaultValue:"no"}) === "yes" &&
|
this.getVariable("tv-freelinks",{defaultValue:"no"}) === "yes" &&
|
||||||
!this.isWithinButtonOrLink()) {
|
!this.isWithinButtonOrLink()) {
|
||||||
|
|
||||||
var currentTiddlerTitle = this.getVariable("currentTiddler") || "";
|
var currentTiddlerTitle = this.getVariable("currentTiddler") || "";
|
||||||
var useWordBoundary = self.wiki.getTiddlerText(WORD_BOUNDARY_TIDDLER, "no") === "yes";
|
var useWordBoundary = self.wiki.getTiddlerText(WORD_BOUNDARY_TIDDLER,"no") === "yes";
|
||||||
|
|
||||||
var cacheKey = "tiddler-title-info-" + (ignoreCase ? "insensitive" : "sensitive");
|
var cacheKey = "tiddler-title-info-" + (ignoreCase ? "insensitive" : "sensitive");
|
||||||
|
this.tiddlerTitleInfo = this.wiki.getGlobalCache(cacheKey,function() {
|
||||||
this.tiddlerTitleInfo = this.wiki.getGlobalCache(cacheKey, function() {
|
return computeTiddlerTitleInfo(self,ignoreCase);
|
||||||
return computeTiddlerTitleInfo(self, ignoreCase);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if(this.tiddlerTitleInfo.titles.length > 0) {
|
if(this.tiddlerTitleInfo && this.tiddlerTitleInfo.titles && this.tiddlerTitleInfo.titles.length > 0 && this.tiddlerTitleInfo.ac) {
|
||||||
var newParseTree = this.processTextWithMatches(text, currentTiddlerTitle, ignoreCase, useWordBoundary);
|
var newParseTree = this.processTextWithMatches(text,currentTiddlerTitle,ignoreCase,useWordBoundary);
|
||||||
if(newParseTree && newParseTree.length > 0 &&
|
if(newParseTree && newParseTree.length > 0 &&
|
||||||
(newParseTree.length > 1 || newParseTree[0].type !== "plain-text")) {
|
(newParseTree.length > 1 || newParseTree[0].type !== "plain-text")) {
|
||||||
childParseTree = newParseTree;
|
childParseTree = newParseTree;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.makeChildWidgets(childParseTree);
|
this.makeChildWidgets(childParseTree);
|
||||||
};
|
};
|
||||||
|
|
||||||
TextNodeWidget.prototype.processTextWithMatches = function(text, currentTiddlerTitle, ignoreCase, useWordBoundary) {
|
TextNodeWidget.prototype.processTextWithMatches = function(text,currentTiddlerTitle,ignoreCase,useWordBoundary) {
|
||||||
if(!text || text.length === 0) {
|
if(!text || text.length === 0) {
|
||||||
return [{type: "plain-text", text: text}];
|
return [{type: "plain-text", text: text}];
|
||||||
}
|
}
|
||||||
|
|
||||||
var searchText = ignoreCase ? text.toLowerCase() : text;
|
|
||||||
var matches;
|
var matches;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
matches = this.tiddlerTitleInfo.ac.search(searchText, useWordBoundary);
|
matches = this.tiddlerTitleInfo.ac.search(text, useWordBoundary, ignoreCase);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
return [{type: "plain-text", text: text}];
|
return [{type: "plain-text", text: text}];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!matches || matches.length === 0) {
|
if(!matches || matches.length === 0) {
|
||||||
return [{type: "plain-text", text: text}];
|
return [{type: "plain-text", text: text}];
|
||||||
}
|
}
|
||||||
|
|
||||||
matches.sort(function(a, b) {
|
var titleToCompare = ignoreCase ?
|
||||||
if(a.index !== b.index) {
|
(currentTiddlerTitle ? currentTiddlerTitle.toLowerCase() : "") :
|
||||||
return a.index - b.index;
|
currentTiddlerTitle;
|
||||||
}
|
|
||||||
return b.length - a.length;
|
matches.sort(function(a,b) {
|
||||||
|
if(b.length !== a.length) return b.length - a.length;
|
||||||
|
return a.index - b.index;
|
||||||
});
|
});
|
||||||
|
|
||||||
var processedPositions = new FastPositionSet();
|
var occupied = new Uint8Array(text.length);
|
||||||
var validMatches = [];
|
var validMatches = [];
|
||||||
|
|
||||||
for(var i = 0; i < matches.length; i++) {
|
for(var i = 0; i < matches.length; i++) {
|
||||||
var match = matches[i];
|
var m = matches[i];
|
||||||
var matchStart = match.index;
|
var start = m.index;
|
||||||
var matchEnd = matchStart + match.length;
|
var end = start + m.length;
|
||||||
|
if(start < 0 || end > text.length) continue;
|
||||||
if(matchStart < 0 || matchEnd > text.length) {
|
|
||||||
continue;
|
var matchedTitle = this.tiddlerTitleInfo.titles[m.titleIndex];
|
||||||
|
if(!matchedTitle) continue;
|
||||||
|
|
||||||
|
var matchedTitleToCompare = ignoreCase ? matchedTitle.toLowerCase() : matchedTitle;
|
||||||
|
if(titleToCompare && matchedTitleToCompare === titleToCompare) continue;
|
||||||
|
|
||||||
|
var overlapping = false;
|
||||||
|
for(var j = start; j < end; j++) {
|
||||||
|
if(occupied[j]) { overlapping = true; break; }
|
||||||
}
|
}
|
||||||
|
if(overlapping) continue;
|
||||||
var matchedTitle = this.tiddlerTitleInfo.titles[match.titleIndex];
|
|
||||||
|
validMatches.push(m);
|
||||||
var titleToCompare = ignoreCase ?
|
for(var k = start; k < end; k++) {
|
||||||
(currentTiddlerTitle ? currentTiddlerTitle.toLowerCase() : "") :
|
occupied[k] = 1;
|
||||||
currentTiddlerTitle;
|
|
||||||
var matchedTitleToCompare = ignoreCase ?
|
|
||||||
(matchedTitle ? matchedTitle.toLowerCase() : "") :
|
|
||||||
matchedTitle;
|
|
||||||
|
|
||||||
if(titleToCompare && matchedTitleToCompare === titleToCompare) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var hasOverlap = false;
|
|
||||||
for(var pos = matchStart; pos < matchEnd && !hasOverlap; pos++) {
|
|
||||||
if(processedPositions.has(pos)) {
|
|
||||||
hasOverlap = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!hasOverlap) {
|
|
||||||
for(var pos = matchStart; pos < matchEnd; pos++) {
|
|
||||||
processedPositions.add(pos);
|
|
||||||
}
|
|
||||||
validMatches.push(match);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(validMatches.length === 0) {
|
if(validMatches.length === 0) {
|
||||||
return [{type: "plain-text", text: text}];
|
return [{type: "plain-text", text: text}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validMatches.sort(function(a,b){ return a.index - b.index; });
|
||||||
|
|
||||||
var newParseTree = [];
|
var newParseTree = [];
|
||||||
var currentPos = 0;
|
var curPos = 0;
|
||||||
|
|
||||||
for(var i = 0; i < validMatches.length; i++) {
|
for(var x = 0; x < validMatches.length; x++) {
|
||||||
var match = validMatches[i];
|
var mm = validMatches[x];
|
||||||
var matchStart = match.index;
|
var s = mm.index;
|
||||||
var matchEnd = matchStart + match.length;
|
var e = s + mm.length;
|
||||||
|
|
||||||
if(matchStart > currentPos) {
|
if(s > curPos) {
|
||||||
var beforeText = text.substring(currentPos, matchStart);
|
newParseTree.push({ type: "plain-text", text: text.substring(curPos,s) });
|
||||||
newParseTree.push({
|
|
||||||
type: "plain-text",
|
|
||||||
text: beforeText
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var matchedTitle = this.tiddlerTitleInfo.titles[match.titleIndex];
|
var toTitle = this.tiddlerTitleInfo.titles[mm.titleIndex];
|
||||||
var matchedText = text.substring(matchStart, matchEnd);
|
var matchedText = text.substring(s,e);
|
||||||
|
|
||||||
newParseTree.push({
|
newParseTree.push({
|
||||||
type: "link",
|
type: "link",
|
||||||
attributes: {
|
attributes: {
|
||||||
to: {type: "string", value: matchedTitle},
|
to: {type: "string", value: toTitle},
|
||||||
"class": {type: "string", value: "tc-freelink"}
|
"class": {type: "string", value: "tc-freelink"}
|
||||||
},
|
},
|
||||||
children: [{
|
children: [{
|
||||||
@@ -193,80 +159,63 @@ TextNodeWidget.prototype.processTextWithMatches = function(text, currentTiddlerT
|
|||||||
text: matchedText
|
text: matchedText
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
|
|
||||||
currentPos = matchEnd;
|
curPos = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(currentPos < text.length) {
|
if(curPos < text.length) {
|
||||||
var remainingText = text.substring(currentPos);
|
newParseTree.push({ type: "plain-text", text: text.substring(curPos) });
|
||||||
newParseTree.push({
|
|
||||||
type: "plain-text",
|
|
||||||
text: remainingText
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return newParseTree;
|
return newParseTree;
|
||||||
};
|
};
|
||||||
|
|
||||||
function computeTiddlerTitleInfo(self, ignoreCase) {
|
function computeTiddlerTitleInfo(self,ignoreCase) {
|
||||||
var targetFilterText = self.wiki.getTiddlerText(TITLE_TARGET_FILTER),
|
var targetFilterText = self.wiki.getTiddlerText(TITLE_TARGET_FILTER),
|
||||||
titles = !!targetFilterText ?
|
titles = targetFilterText ?
|
||||||
self.wiki.filterTiddlers(targetFilterText,$tw.rootWidget) :
|
self.wiki.filterTiddlers(targetFilterText,$tw.rootWidget) :
|
||||||
self.wiki.allTitles();
|
self.wiki.allTitles();
|
||||||
|
|
||||||
if(!titles || titles.length === 0) {
|
if(!titles || titles.length === 0) {
|
||||||
return {
|
return { titles: [], ac: new AhoCorasick() };
|
||||||
titles: [],
|
|
||||||
ac: new AhoCorasick()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var validTitles = [];
|
var validTitles = [];
|
||||||
var ac = new AhoCorasick();
|
|
||||||
|
|
||||||
for(var i = 0; i < titles.length; i++) {
|
for(var i = 0; i < titles.length; i++) {
|
||||||
var title = titles[i];
|
var t = titles[i];
|
||||||
if(title && title.length > 0 && title.substring(0,3) !== "$:/") {
|
if(t && t.length > 0 && t.substring(0,3) !== "$:/") {
|
||||||
var escapedTitle = escapeRegExp(title);
|
validTitles.push(t);
|
||||||
if(escapedTitle) {
|
|
||||||
validTitles.push(title);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sortedTitles = validTitles.sort(function(a,b) {
|
validTitles.sort(function(a,b) {
|
||||||
var lenDiff = b.length - a.length;
|
var d = b.length - a.length;
|
||||||
if(lenDiff !== 0) return lenDiff;
|
if(d !== 0) return d;
|
||||||
return a < b ? -1 : a > b ? 1 : 0;
|
return a < b ? -1 : a > b ? 1 : 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
for(var i = 0; i < sortedTitles.length; i++) {
|
var ac = new AhoCorasick();
|
||||||
var title = sortedTitles[i];
|
for(var j = 0; j < validTitles.length; j++) {
|
||||||
|
var title = validTitles[j];
|
||||||
var pattern = ignoreCase ? title.toLowerCase() : title;
|
var pattern = ignoreCase ? title.toLowerCase() : title;
|
||||||
ac.addPattern(pattern, i);
|
ac.addPattern(pattern,j);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ac.buildFailureLinks();
|
ac.buildFailureLinks();
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
return {
|
return { titles: [], ac: new AhoCorasick() };
|
||||||
titles: [],
|
|
||||||
ac: new AhoCorasick()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return { titles: validTitles, ac: ac };
|
||||||
titles: sortedTitles,
|
|
||||||
ac: ac
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TextNodeWidget.prototype.isWithinButtonOrLink = function() {
|
TextNodeWidget.prototype.isWithinButtonOrLink = function() {
|
||||||
var widget = this.parentWidget;
|
var widget = this.parentWidget;
|
||||||
while(widget) {
|
while(widget) {
|
||||||
if(widget instanceof ButtonWidget ||
|
if(widget instanceof ButtonWidget ||
|
||||||
widget instanceof LinkWidget ||
|
widget instanceof LinkWidget ||
|
||||||
((widget instanceof ElementWidget) && widget.parseTreeNode.tag === "a")) {
|
((widget instanceof ElementWidget) && widget.parseTreeNode.tag === "a")) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
widget = widget.parentWidget;
|
widget = widget.parentWidget;
|
||||||
@@ -275,35 +224,56 @@ TextNodeWidget.prototype.isWithinButtonOrLink = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TextNodeWidget.prototype.refresh = function(changedTiddlers) {
|
TextNodeWidget.prototype.refresh = function(changedTiddlers) {
|
||||||
var self = this,
|
var self = this;
|
||||||
changedAttributes = this.computeAttributes(),
|
var changedAttributes = this.computeAttributes();
|
||||||
titlesHaveChanged = false;
|
var titlesHaveChanged = false;
|
||||||
|
|
||||||
if(changedTiddlers) {
|
if(changedTiddlers) {
|
||||||
$tw.utils.each(changedTiddlers,function(change,title) {
|
$tw.utils.each(changedTiddlers,function(change,title) {
|
||||||
if(change.isDeleted) {
|
if(titlesHaveChanged) return;
|
||||||
|
|
||||||
|
if(title === WORD_BOUNDARY_TIDDLER || title === TITLE_TARGET_FILTER) {
|
||||||
|
titlesHaveChanged = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(title.substring(0,3) === "$:/") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(change && change.isDeleted) {
|
||||||
|
if(self.tiddlerTitleInfo && self.tiddlerTitleInfo.titles && self.tiddlerTitleInfo.titles.indexOf(title) !== -1) {
|
||||||
|
titlesHaveChanged = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tiddler = self.wiki.getTiddler(title);
|
||||||
|
if(tiddler && tiddler.hasField("draft.of")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!self.tiddlerTitleInfo || !self.tiddlerTitleInfo.titles || self.tiddlerTitleInfo.titles.indexOf(title) === -1) {
|
||||||
titlesHaveChanged = true;
|
titlesHaveChanged = true;
|
||||||
} else {
|
|
||||||
titlesHaveChanged = titlesHaveChanged ||
|
|
||||||
!self.tiddlerTitleInfo ||
|
|
||||||
self.tiddlerTitleInfo.titles.indexOf(title) === -1;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(changedAttributes.text || titlesHaveChanged ||
|
var wordBoundaryChanged = !!(changedTiddlers && changedTiddlers[WORD_BOUNDARY_TIDDLER]);
|
||||||
(changedTiddlers && changedTiddlers[WORD_BOUNDARY_TIDDLER])) {
|
|
||||||
|
if(changedAttributes.text || titlesHaveChanged || wordBoundaryChanged) {
|
||||||
if(titlesHaveChanged) {
|
if(titlesHaveChanged) {
|
||||||
var ignoreCase = self.getVariable("tv-freelinks-ignore-case",{defaultValue:"no"}).trim() === "yes";
|
self.wiki.clearCache("tiddler-title-info-insensitive");
|
||||||
var cacheKey = "tiddler-title-info-" + (ignoreCase ? "insensitive" : "sensitive");
|
self.wiki.clearCache("tiddler-title-info-sensitive");
|
||||||
self.wiki.clearCache(cacheKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.refreshSelf();
|
this.refreshSelf();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if(changedTiddlers) {
|
||||||
return this.refreshChildren(changedTiddlers);
|
return this.refreshChildren(changedTiddlers);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.text = TextNodeWidget;
|
exports.text = TextNodeWidget;
|
||||||
|
|||||||
Reference in New Issue
Block a user