From 92ca17a08bbcfbbc15388b7147613f379f4005a8 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Sun, 9 Jun 2024 23:15:41 +0800 Subject: [PATCH] fix: correct anchor start end --- core/modules/parsers/wikiparser/rules/anchor.js | 15 +++++++++------ .../parsers/wikiparser/rules/prettylink.js | 6 ++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/anchor.js b/core/modules/parsers/wikiparser/rules/anchor.js index cede453cb..831ec105c 100644 --- a/core/modules/parsers/wikiparser/rules/anchor.js +++ b/core/modules/parsers/wikiparser/rules/anchor.js @@ -18,8 +18,8 @@ Instantiate parse rule exports.init = function(parser) { this.parser = parser; // 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. - // 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. + // 1. inlineId: located on the end of the line, with a space before it, means it's the id of the current block. + // 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; }; @@ -30,16 +30,19 @@ exports.parse = function() { // Move past the match this.parser.pos = this.matchRegExp.lastIndex; // will be one of following case, another will be undefined - var anchorId = this.match[1]; - var anchorBeforeId = this.match[2]; + var inlineId = this.match[1]; + var blockId = this.match[2]; + var id = inlineId || blockId || ''; + var anchorStart = this.parser.pos; + var anchorEnd = anchorStart + id.length; // Parse tree nodes to return return [{ type: "anchor", 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. // 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: [] }]; diff --git a/core/modules/parsers/wikiparser/rules/prettylink.js b/core/modules/parsers/wikiparser/rules/prettylink.js index b1172ac4a..bed0365d3 100644 --- a/core/modules/parsers/wikiparser/rules/prettylink.js +++ b/core/modules/parsers/wikiparser/rules/prettylink.js @@ -42,7 +42,7 @@ exports.parse = function() { var linkStart = this.match[2] ? (start + this.match[1].length + 1) : start; var linkEnd = linkStart + link.length; 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) { link = link + "^" + anchor; } @@ -60,11 +60,13 @@ exports.parse = function() { }] }]; } else { + var anchorStart = anchor ? (linkEnd + 1) : linkEnd; + var anchorEnd = anchorStart + anchor.length; return [{ type: "link", attributes: { to: {type: "string", value: link, start: linkStart, end: linkEnd}, - toAnchor: {type: "string", value: anchor}, + toAnchor: {type: "string", value: anchor, start: anchorStart, end: anchorEnd}, }, children: [{ type: "text", text: text, start: start, end: textEndPos