-
Notifications
You must be signed in to change notification settings - Fork 7
ENG-2250 Implement Discourse context overlay in Reading view #1435
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
Open
trangdoan982
wants to merge
6
commits into
main
from
eng-2250-implement-discourse-context-overlay-in-reading-view
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf9d305
ENG-1249 Add the discourse context overlay in Reading view
trangdoan982 d2f7384
ENG-1249 Leave embedded links to the post processor on refresh
trangdoan982 fab454c
ENG-1249 Trim comments that restated their function names
trangdoan982 efee97b
ENG-2250 Let the popover count relations for Reading view badges too
trangdoan982 404fef8
ENG-2250 Resolve badge links against the file they were written in
trangdoan982 97db6ed
ENG-2250 Keep context badges out of the plugin's own modal previews
trangdoan982 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
apps/obsidian/src/utils/discourseContextOverlayPostProcessor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { parseLinktext, type MarkdownPostProcessorContext } from "obsidian"; | ||
| import type DiscourseGraphPlugin from "~/index"; | ||
| import { | ||
| badgeTargetPath, | ||
| createDiscourseContextBadge, | ||
| updateDiscourseContextBadge, | ||
| DISCOURSE_CONTEXT_BADGE_CLASS, | ||
| } from "~/components/discourseContextBadge"; | ||
| import { openDiscourseContextPopover } from "~/components/DiscourseContextPopover"; | ||
| import { resolveDiscourseLinkTarget } from "./discourseLinkUtils"; | ||
|
|
||
| /** | ||
| * A link inside a transclusion resolves against the embedded file, so walk the | ||
| * `.internal-embed` chain outwards to find the file it was actually written in. | ||
| */ | ||
| const resolveLinkSourcePath = ({ | ||
| plugin, | ||
| link, | ||
| sourcePath, | ||
| }: { | ||
| plugin: DiscourseGraphPlugin; | ||
| link: HTMLElement; | ||
| sourcePath: string; | ||
| }): string | null => { | ||
| const embeds: HTMLElement[] = []; | ||
| let embed = link.parentElement?.closest<HTMLElement>(".internal-embed"); | ||
| while (embed) { | ||
| embeds.unshift(embed); | ||
| embed = embed.parentElement?.closest<HTMLElement>(".internal-embed"); | ||
| } | ||
|
|
||
| let path = sourcePath; | ||
| for (const ancestor of embeds) { | ||
| const src = ancestor.getAttribute("src"); | ||
| if (!src) return null; | ||
| const file = plugin.app.metadataCache.getFirstLinkpathDest( | ||
| parseLinktext(src).path, | ||
| path, | ||
| ); | ||
| if (!file) return null; | ||
| path = file.path; | ||
| } | ||
| return path; | ||
| }; | ||
|
|
||
| /** Idempotent: Obsidian reuses rendered sections and re-runs post processors. */ | ||
| export const applyDiscourseContextBadges = ({ | ||
| plugin, | ||
| el, | ||
| sourcePath, | ||
| }: { | ||
| plugin: DiscourseGraphPlugin; | ||
| el: HTMLElement; | ||
| sourcePath: string; | ||
| }): void => { | ||
| const links = el.querySelectorAll<HTMLAnchorElement>("a.internal-link"); | ||
|
|
||
| for (const link of Array.from(links)) { | ||
| const linkSourcePath = resolveLinkSourcePath({ plugin, link, sourcePath }); | ||
| if (!linkSourcePath) continue; | ||
| const existing = link.nextElementSibling?.hasClass( | ||
| DISCOURSE_CONTEXT_BADGE_CLASS, | ||
| ) | ||
| ? (link.nextElementSibling as HTMLElement) | ||
| : null; | ||
|
|
||
| // data-href holds the link as written; href is resolved and URL-encoded. | ||
| const linktext = | ||
| link.getAttribute("data-href") ?? link.getAttribute("href"); | ||
| if (!linktext) { | ||
| existing?.remove(); | ||
| continue; | ||
| } | ||
|
|
||
| const target = resolveDiscourseLinkTarget({ | ||
| plugin, | ||
| linktext, | ||
| sourcePath: linkSourcePath, | ||
| }); | ||
| if (!target) { | ||
| existing?.remove(); | ||
| continue; | ||
| } | ||
|
|
||
| // Updated rather than replaced when the target is unchanged: an open | ||
| // popover anchored to this badge would otherwise hold a detached element. | ||
| if (existing && badgeTargetPath(existing) === target.file.path) { | ||
| updateDiscourseContextBadge({ | ||
| badge: existing, | ||
| nodeType: target.nodeType, | ||
| relationCount: target.relationCount, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const badge = createDiscourseContextBadge({ | ||
| file: target.file, | ||
| nodeType: target.nodeType, | ||
| relationCount: target.relationCount, | ||
| onActivate: ({ file, anchor }) => | ||
| openDiscourseContextPopover({ | ||
| plugin, | ||
| file, | ||
| anchor, | ||
| }), | ||
| }); | ||
|
|
||
| existing?.remove(); | ||
| link.insertAdjacentElement("afterend", badge); | ||
| } | ||
| }; | ||
|
|
||
| export const removeDiscourseContextBadges = (el: HTMLElement): void => { | ||
| el.querySelectorAll(`.${DISCOURSE_CONTEXT_BADGE_CLASS}`).forEach((badge) => | ||
| badge.remove(), | ||
| ); | ||
| }; | ||
|
|
||
| export const createDiscourseContextOverlayPostProcessor = | ||
| (plugin: DiscourseGraphPlugin) => | ||
| (el: HTMLElement, ctx: MarkdownPostProcessorContext): void => { | ||
| if (!plugin.settings.showDiscourseContextOverlay) return; | ||
| if (!ctx.sourcePath) return; | ||
| // Rendered sections arrive detached, so an attached one is the plugin's own | ||
| // MarkdownRenderer.render inside a modal, where the popover cannot dismiss. | ||
| if (el.closest(".modal-container")) return; | ||
| applyDiscourseContextBadges({ plugin, el, sourcePath: ctx.sourcePath }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.