diff --git a/@types/types.ts b/@types/types.ts index e1d1a36..48efd98 100644 --- a/@types/types.ts +++ b/@types/types.ts @@ -21,6 +21,7 @@ export interface ISettingsData { copyButton: boolean; autoCollapse: boolean; defaultCollapseType: string; + syncLinks: boolean; version: string; } export declare class ObsidianAdmonitionPlugin extends Plugin_2 { diff --git a/README.md b/README.md index bd4cf66..8d38f5c 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,12 @@ An icon without a title will have this CSS: Turns on an experimental mode that uses Obsidian's markdown syntax highlighter inside admonition code blocks. +### Sync Links to Metadata Cache + +This will attempt to sync internal links within admonitions to the metadata cache used by Obsidian. This will allow graph view to display these links. + +This setting is experimental and could have unintended consequences. If you begin to experience odd behavior, try turning it off and reloading Obsidian. + ### Collapsible By Default Admonitions will be automatically rendered as collapsible (open) by default. @@ -301,11 +307,12 @@ Adds a "copy content" button to every admonition block. ## Todo +No additional features are planned at this time. If there is a feature missing that you would like to see, please open an issue. + - [x] Add the ability to collapse the admonition - [x] Custom admonitions - [x] Settings tab to customize icon and color of all admonitions - [x] Ability to render markdown inside an admonition -- [ ] Top-level Python Markdown-style admonitions # Version History diff --git a/manifest.json b/manifest.json index 1909c08..17823d0 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-admonition", "name": "Admonition", - "version": "4.2.1", + "version": "4.3.0", "minAppVersion": "0.11.0", "description": "Admonition block-styled content for Obsidian.md", "author": "Jeremy Valentine", diff --git a/package.json b/package.json index 6f3f9ca..7801beb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-admonition", - "version": "4.2.1", + "version": "4.3.0", "description": "Admonition block-styled content for Obsidian.md", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index 9f25183..10ff2f4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,8 @@ import { MarkdownRenderer, MarkdownView, Notice, - Plugin + Plugin, + TFile } from "obsidian"; import { Admonition, @@ -58,7 +59,8 @@ const DEFAULT_APP_SETTINGS: ISettingsData = { copyButton: false, version: "", autoCollapse: false, - defaultCollapseType: "open" + defaultCollapseType: "open", + syncLinks: true }; const ADMONITION_MAP: { @@ -220,6 +222,24 @@ export default class ObsidianAdmonition } } }); + + this.registerEvent( + this.app.metadataCache.on("resolve", (file) => { + if (this.app.workspace.getActiveFile() != file) return; + + const view = + this.app.workspace.getActiveViewOfType(MarkdownView); + + if (!view || !(view instanceof MarkdownView)) return; + + const admonitionLinks = + view.contentEl.querySelectorAll<HTMLAnchorElement>( + ".admonition a.internal-link" + ); + + this.addLinksToCache(admonitionLinks, file.path); + }) + ); } turnOnSyntaxHighlighting(types: string[] = Object.keys(this.admonitions)) { if (!this.data.syntaxHighlight) return; @@ -402,6 +422,13 @@ export default class ObsidianAdmonition } } + const links = + admonitionContent.querySelectorAll<HTMLAnchorElement>( + "a.internal-link" + ); + + this.addLinksToCache(links, ctx.sourcePath); + /** * Replace the <pre> tag with the new admonition. */ @@ -429,4 +456,34 @@ export default class ObsidianAdmonition this.turnOffSyntaxHighlighting(); } + addLinksToCache( + links: NodeListOf<HTMLAnchorElement>, + sourcePath: string + ): void { + if (!this.data.syncLinks) return; + + for (let i = 0; i < links.length; i++) { + const a = links[i]; + if (a.dataset.href) { + let file = this.app.metadataCache.getFirstLinkpathDest( + a.dataset.href, + "" + ); + if (file && file instanceof TFile) { + if (!this.app.metadataCache.resolvedLinks[sourcePath]) { + this.app.metadataCache.resolvedLinks[sourcePath] = { + [file.path]: 0 + }; + } + let resolved = + this.app.metadataCache.resolvedLinks[sourcePath]; + if (!resolved[file.path]) { + resolved[file.path] = 0; + } + resolved[file.path] += 1; + this.app.metadataCache.resolvedLinks[sourcePath] = resolved; + } + } + } + } } diff --git a/src/settings.ts b/src/settings.ts index 8155823..25bce7c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -65,6 +65,18 @@ export default class AdmonitionSetting extends PluginSettingTab { await this.plugin.saveSettings(); }); }); + new Setting(containerEl) + .setName("Sync Links to Metadata Cache") + .setDesc( + "Try to sync internal links to the metadata cache to display in graph view. This setting could have unintended consequences. Use at your own risk." + ) + .addToggle((t) => { + t.setValue(this.plugin.data.syncLinks).onChange(async (v) => { + this.plugin.data.syncLinks = v; + this.display(); + await this.plugin.saveSettings(); + }); + }); new Setting(containerEl) .setName("Collapsible by Default") .setDesc( diff --git a/versions.json b/versions.json index d79a2e5..62ac765 100644 --- a/versions.json +++ b/versions.json @@ -7,5 +7,6 @@ "3.3.4": "0.11.0", "4.0.1": "0.11.0", "4.1.7": "0.11.0", - "4.2.1": "0.11.0" + "4.2.1": "0.11.0", + "4.3.0": "0.12.0" } \ No newline at end of file