Fix: addParentPointers re-attaches parents even if a sub-tree already

has some

+ some cleanup

Properly addresses #1776
This commit is contained in:
Zef Hemel
2026-01-14 08:33:29 +01:00
parent 27c67878e5
commit 062c1ea44d
4 changed files with 5 additions and 7 deletions
@@ -43,10 +43,15 @@ function cleanTags(values: (Tag | null)[], cleanWhitespace = false): Tag[] {
return result;
}
/**
* Cleans up a markdown tree. Side effect: adds parent pointers
*/
function preprocess(t: ParseTree) {
addParentPointers(t);
traverseTree(t, (node) => {
// If this node i
if (!node.type) {
// Remove redundant newlines in table
if (node.text?.startsWith("\n")) {
const prevNodeIdx = node.parent!.children!.indexOf(node) - 1;
const prevNodeType = node.parent!.children![prevNodeIdx]?.type;
-4
View File
@@ -15,10 +15,6 @@ export function addParentPointers(tree: ParseTree) {
return;
}
for (const child of tree.children) {
if (child.parent) {
// Already added parent pointers before
return;
}
child.parent = tree;
addParentPointers(child);
}
-1
View File
@@ -49,7 +49,6 @@ export async function renamePageLinkCommand() {
console.error("No link found at cursor position...");
return;
}
console.log("Link node", mdTree);
addParentPointers(mdTree);
let node: ParseTree | null = link;
if (node.type !== "WikiLink") {
-2
View File
@@ -1,7 +1,6 @@
import type { FrontMatter } from "./frontmatter.ts";
import { queryLuaObjects } from "./api.ts";
import {
addParentPointers,
collectNodesOfType,
findParentMatching,
type ParseTree,
@@ -31,7 +30,6 @@ export function indexTags(
tree: ParseTree,
) {
const tags = new Set<string>(); // name:parent
addParentPointers(tree);
const pageTags: string[] = frontmatter.tags || [];
for (const pageTag of pageTags) {
tags.add(`${pageTag}:page`);