This commit is contained in:
Zef Hemel
2022-11-27 08:48:01 +01:00
parent 2603fdb939
commit 3b1802399a
4 changed files with 72 additions and 22 deletions
+30 -14
View File
@@ -1,3 +1,4 @@
import { pageLinkRegex } from "../../common/parser.ts";
import {
Decoration,
DecorationSet,
@@ -29,39 +30,54 @@ class CleanWikiLinkPlugin {
// let parentRange: [number, number];
iterateTreeInVisibleRanges(view, {
enter: ({ type, from, to }) => {
if (type.name === "WikiLinkPage") {
if (type.name === "WikiLink") {
// Adding 2 on each side due to [[ and ]] that are outside the WikiLinkPage node
if (isCursorInRange(view.state, [from - 2, to + 2])) {
if (isCursorInRange(view.state, [from, to])) {
return;
}
// Add decoration to hide the prefix [[
widgets.push(
invisibleDecoration.range(
from - 2,
from,
from + 2,
),
);
// Add decoration to hide the postfix [[
widgets.push(
invisibleDecoration.range(
to - 2,
to,
to + 2,
),
);
// Now check if there's a "/" inside
// Now check if this page has an alias
const text = view.state.sliceDoc(from, to);
if (text.indexOf("/") === -1) {
return;
const match = pageLinkRegex.exec(text);
if (!match) return;
const [_fullMatch, page, pipePart] = match;
if (!pipePart) {
// No alias, let's check if there's a slash in the page name
if (text.indexOf("/") === -1) {
return;
}
// Add a inivisible decoration to hide the path prefix
widgets.push(
invisibleDecoration.range(
from + 2, // +2 to skip the [[
from + text.lastIndexOf("/") + 1,
),
);
} else {
// Alias is present, so we hide the part before the pipe
widgets.push(
invisibleDecoration.range(
from + 2,
from + page.length + 3, // 3 is for the [[ and the |
),
);
}
// Add a inivisible decoration to hide the path prefix
widgets.push(
invisibleDecoration.range(
from,
from + text.lastIndexOf("/") + 1,
),
);
}
},
});