Skip to content

Commit

Permalink
4.3.0
Browse files Browse the repository at this point in the history
- Added Sync Links to Metadata Cache setting
  • Loading branch information
valentine195 committed May 26, 2021
1 parent e17e695 commit 347aeae
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions @types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ISettingsData {
copyButton: boolean;
autoCollapse: boolean;
defaultCollapseType: string;
syncLinks: boolean;
version: string;
}
export declare class ObsidianAdmonitionPlugin extends Plugin_2 {
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
61 changes: 59 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
MarkdownRenderer,
MarkdownView,
Notice,
Plugin
Plugin,
TFile
} from "obsidian";
import {
Admonition,
Expand Down Expand Up @@ -58,7 +59,8 @@ const DEFAULT_APP_SETTINGS: ISettingsData = {
copyButton: false,
version: "",
autoCollapse: false,
defaultCollapseType: "open"
defaultCollapseType: "open",
syncLinks: true
};

const ADMONITION_MAP: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
}
}
}
}
12 changes: 12 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 347aeae

Please sign in to comment.