1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-09 17:00:27 +00:00

fix: correct anchor start end

This commit is contained in:
lin onetwo 2024-06-09 23:15:41 +08:00
parent 7e9cadf6b0
commit 92ca17a08b
2 changed files with 13 additions and 8 deletions

View File

@ -18,8 +18,8 @@ Instantiate parse rule
exports.init = function(parser) { exports.init = function(parser) {
this.parser = parser; this.parser = parser;
// Regexp to match the anchor. // Regexp to match the anchor.
// 1. located on the end of the line, with a space before it, means it's the id of the current block. // 1. inlineId: located on the end of the line, with a space before it, means it's the id of the current block.
// 2. located at start of the line, no space, means it's the id of the previous block. Because some block can't have id suffix, otherwise id break the block mode parser like codeblock. // 2. blockId: located at start of the line, no space, means it's the id of the previous block. Because some block can't have id suffix, otherwise id break the block mode parser like codeblock.
this.matchRegExp = /[ ]\^(\S+)$|^\^(\S+)$/mg; this.matchRegExp = /[ ]\^(\S+)$|^\^(\S+)$/mg;
}; };
@ -30,16 +30,19 @@ exports.parse = function() {
// Move past the match // Move past the match
this.parser.pos = this.matchRegExp.lastIndex; this.parser.pos = this.matchRegExp.lastIndex;
// will be one of following case, another will be undefined // will be one of following case, another will be undefined
var anchorId = this.match[1]; var inlineId = this.match[1];
var anchorBeforeId = this.match[2]; var blockId = this.match[2];
var id = inlineId || blockId || '';
var anchorStart = this.parser.pos;
var anchorEnd = anchorStart + id.length;
// Parse tree nodes to return // Parse tree nodes to return
return [{ return [{
type: "anchor", type: "anchor",
attributes: { attributes: {
id: {type: "string", value: anchorId || anchorBeforeId}, id: {type: "string", value: id, start: anchorStart, end: anchorEnd},
// `yes` means the block that this anchor pointing to, is before this node, both anchor and the block, is in a same parent node's children list. // `yes` means the block that this anchor pointing to, is before this node, both anchor and the block, is in a same parent node's children list.
// empty means the block is this node's direct parent node. // empty means the block is this node's direct parent node.
previousSibling: {type: "string", value: Boolean(anchorBeforeId) ? "yes" : ""}, previousSibling: {type: "string", value: Boolean(blockId) ? "yes" : ""},
}, },
children: [] children: []
}]; }];

View File

@ -42,7 +42,7 @@ exports.parse = function() {
var linkStart = this.match[2] ? (start + this.match[1].length + 1) : start; var linkStart = this.match[2] ? (start + this.match[1].length + 1) : start;
var linkEnd = linkStart + link.length; var linkEnd = linkStart + link.length;
if($tw.utils.isLinkExternal(link)) { if($tw.utils.isLinkExternal(link)) {
// add back the part after `^` to the ext link, if it happen to has one. // add back the part after `^` to the ext link, if it happen to has one. Here is is not an anchor, but a part of the external URL.
if(anchor) { if(anchor) {
link = link + "^" + anchor; link = link + "^" + anchor;
} }
@ -60,11 +60,13 @@ exports.parse = function() {
}] }]
}]; }];
} else { } else {
var anchorStart = anchor ? (linkEnd + 1) : linkEnd;
var anchorEnd = anchorStart + anchor.length;
return [{ return [{
type: "link", type: "link",
attributes: { attributes: {
to: {type: "string", value: link, start: linkStart, end: linkEnd}, to: {type: "string", value: link, start: linkStart, end: linkEnd},
toAnchor: {type: "string", value: anchor}, toAnchor: {type: "string", value: anchor, start: anchorStart, end: anchorEnd},
}, },
children: [{ children: [{
type: "text", text: text, start: start, end: textEndPos type: "text", text: text, start: start, end: textEndPos