Skip to content

Commit f7b286c

Browse files
shumadridVinzent03
andauthored
fix: improve the "is file openable in obsidian" check (#884)
* fix: improve the "is file openable in obsidian" check * add @ts-expect-error * fix eslint errors * use any * attempt * stop trying to bypass checks * refactor: add types to allow private api --------- Co-authored-by: Vinzent <vinzent03@proton.me>
1 parent 953dbd4 commit f7b286c

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,20 @@ declare module "obsidian" {
321321
saveLocalStorage(key: string, value: string | undefined): void;
322322
openWithDefaultApp(path: string): void;
323323
getTheme(): "obsidian" | "moonstone";
324+
viewRegistry: ViewRegistry;
324325
}
325326
interface View {
326327
titleEl: HTMLElement;
327328
inlineTitleEl: HTMLElement;
328329
}
330+
interface ViewRegistry {
331+
/**
332+
* PRIVATE API
333+
*
334+
* Returns the view type for the given extension if available.
335+
*/
336+
getTypeByExtension(extension: string): string;
337+
}
329338
interface Workspace {
330339
/**
331340
* Emitted when some git action has been completed and plugin has been refreshed

src/ui/history/components/logFileComponent.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type { DiffFile } from "src/types";
44
import {
55
fileIsBinary,
6+
fileOpenableInObsidian,
67
getDisplayPath,
78
getNewLeaf,
89
mayTriggerFileMenu,
@@ -87,7 +88,7 @@
8788
</div>
8889
<div class="git-tools">
8990
<div class="buttons">
90-
{#if view.app.vault.getAbstractFileByPath(diff.vaultPath) instanceof TFile}
91+
{#if fileOpenableInObsidian(diff.vaultPath, view.app)}
9192
<div
9293
data-icon="go-to-file"
9394
aria-label="Open File"

src/ui/sourceControl/components/fileComponent.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { DiscardModal } from "src/ui/modals/discardModal";
77
import {
88
fileIsBinary,
9+
fileOpenableInObsidian,
910
getDisplayPath,
1011
getNewLeaf,
1112
mayTriggerFileMenu,
@@ -141,7 +142,7 @@
141142
</div>
142143
<div class="git-tools">
143144
<div class="buttons">
144-
{#if view.app.vault.getAbstractFileByPath(change.vaultPath) instanceof TFile}
145+
{#if fileOpenableInObsidian(change.vaultPath, view.app)}
145146
<div
146147
data-icon="go-to-file"
147148
aria-label="Open File"

src/ui/sourceControl/components/stagedFileComponent.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import type { FileStatusResult } from "src/types";
66
import {
77
fileIsBinary,
8+
fileOpenableInObsidian,
89
getDisplayPath,
910
getNewLeaf,
1011
mayTriggerFileMenu,
@@ -108,7 +109,7 @@
108109
</div>
109110
<div class="git-tools">
110111
<div class="buttons">
111-
{#if view.app.vault.getAbstractFileByPath(change.vaultPath) instanceof TFile}
112+
{#if fileOpenableInObsidian(change.vaultPath, view.app)}
112113
<div
113114
data-icon="go-to-file"
114115
aria-label="Open File"

src/utils.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cssColorConverter from "css-color-converter";
22
import deepEqual from "deep-equal";
33
import type { App, RGB, WorkspaceLeaf } from "obsidian";
4-
import { Keymap, Menu, moment } from "obsidian";
4+
import { Keymap, Menu, moment, TFile } from "obsidian";
55
import { BINARY_EXTENSIONS } from "./constants";
66

77
export const worthWalking = (filepath: string, root?: string) => {
@@ -186,3 +186,23 @@ export function formatRemoteUrl(url: string): string {
186186
}
187187
return url;
188188
}
189+
190+
export function fileOpenableInObsidian(
191+
relativeVaultPath: string,
192+
app: App
193+
): boolean {
194+
const file = app.vault.getAbstractFileByPath(relativeVaultPath);
195+
if (!(file instanceof TFile)) {
196+
return false;
197+
}
198+
try {
199+
// Internal Obsidian API function
200+
// If a view type is registired for the file extension, it can be opened in Obsidian.
201+
// Just checking if Obsidian tracks the file is not enough,
202+
// because it can also track files, it can only open externally.
203+
return !!app.viewRegistry.getTypeByExtension(file.extension);
204+
} catch {
205+
// If the function doesn't exist anymore, it will throw an error. In that case, just skip the check.
206+
return true;
207+
}
208+
}

0 commit comments

Comments
 (0)