Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: embedded notes are displayed twice, since 1.12.5 #1061

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 4 additions & 44 deletions src/util/RenderMarkdownWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
AUDIO_FORMATS,
IMAGE_FORMATS,
VIDEO_FORMATS,
NON_LETTER_SYMBOLS_REGEX,

Check warning on line 6 in src/util/RenderMarkdownWrapper.ts

View workflow job for this annotation

GitHub Actions / lint_and_test

'NON_LETTER_SYMBOLS_REGEX' is defined but never used. Allowed unused vars must match /^_/u
} from "../constants";
import SRPlugin from "../main";
import { TextDirection } from "./TextDirection";
Expand Down Expand Up @@ -46,8 +46,10 @@
if (link.target.extension !== "md") {
this.embedMediaFile(el, link.target);
} else {
el.innerText = "";
this.renderTransclude(el, link, recursiveDepth);
// We get here if there is a transclusion link, such as "![[Test Embed]]"
// In version 1.12.4 and earlier we used the deprecated Obsidian MarkdownRenderer.renderMarkdown() and we
// needed to have our own method "renderTransclude()" that loaded the referenced file and rendered it.
// In version 1.12.5, we started using MarkdownRenderer.render() instead, which does this automatically.
}
}
});
Expand Down Expand Up @@ -115,46 +117,4 @@
el.innerText = target.path;
}
}

private async renderTransclude(
el: HTMLElement,
link: {
text: string;
file: string;
heading: string;
blockId: string;
target: TFile;
},
recursiveDepth: number,
) {
const cache = this.app.metadataCache.getCache(link.target.path);
const text = await this.app.vault.cachedRead(link.target);
let blockText;
if (link.heading) {
const clean = (s: string) => s.replace(NON_LETTER_SYMBOLS_REGEX, "");
const headingIndex = cache.headings?.findIndex(
(h) => clean(h.heading) === clean(link.heading),
);
const heading = cache.headings[headingIndex];

const startAt = heading.position.start.offset;
const endAt =
cache.headings.slice(headingIndex + 1).find((h) => h.level <= heading.level)
?.position?.start?.offset || text.length;

blockText = text.substring(startAt, endAt);
} else if (link.blockId) {
const block = cache.blocks[link.blockId];
const startAt = block.position.start.offset;
const endAt = block.position.end.offset;
blockText = text.substring(startAt, endAt);
} else {
blockText = text;
}

// We are operating here within the parent container.
// It already has the rtl div if necessary.
// We don't need another rtl div, so we can set direction to Unspecified
this.renderMarkdownWrapper(blockText, el, TextDirection.Unspecified, recursiveDepth + 1);
}
}
Loading