Skip to content

Commit a961e74

Browse files
author
Ozan Tellioglu
committed
Address app://local change
1 parent a4b0a52 commit a961e74

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/cm5/checkLine.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { normalizePath, TFile } from 'obsidian';
1+
import { normalizePath, TFile, Platform } from 'obsidian';
22
import OzanImagePlugin from '../main';
33
import * as PDFHandler from 'src/util/pdfHandler';
44
import * as ExcalidrawHandler from 'src/util/excalidrawHandler';
@@ -66,7 +66,8 @@ export const checkLine: any = async (cm: CodeMirror.Editor, lineNumber: number,
6666
// Prepare the src for the Image
6767
if (linkInLine.result) {
6868
// Local File URL Correction (Outside of Vault)
69-
if (filename.startsWith('file:///')) filename = filename.replace('file:///', 'app://local/');
69+
let resourcePathPrefix = ObsidianHelper.getObsidianResourcePathPrefix();
70+
if (filename.startsWith('file:///')) filename = filename.replace('file:///', resourcePathPrefix);
7071
img.src = decodeURI(filename);
7172
} else {
7273
// Get Image File

src/cm6/linkDetector.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TFile } from 'obsidian';
22
import OzanImagePlugin from 'src/main';
33
import * as ExcalidrawHandler from 'src/util/excalidrawHandler';
4+
import * as ObsidianHelper from 'src/util/obsidianHelper';
45

56
export const transclusionTypes = ['file-transclusion', 'header-transclusion', 'blockid-transclusion'];
67

@@ -292,14 +293,17 @@ export const detectLink = (params: { lineText: string; sourceFile: TFile; plugin
292293
}
293294

294295
// --> F: Local File Wiki
296+
/* @deprecated Local File Wiki is not supported anymore
297+
https://discord.com/channels/686053708261228577/1103015564055691324/1103025035498758154
298+
***************************************************************************************************
295299
const localFileWikiRegex = /!\[\[(file\:\/\/\/|app\:\/\/local\/).*(.pdf|.jpe?g|.png|.gif|.svg|.bmp)(.*)?\]\]/;
296-
const localFileNameRegex = /(file\:\/\/\/|app\:\/\/local\/).*(.pdf|.jpe?g|.png|.gif|.svg|.bmp)/;
297300
const localFileMatchWiki = lineText.match(localFileWikiRegex);
298301
299302
if (localFileMatchWiki) {
300303
const localFileNameWikiMatch = localFileMatchWiki[0].match(localFileNameRegex);
301304
if (localFileNameWikiMatch) {
302-
const fileLink = localFileNameWikiMatch[0].replace('file:///', 'app://local/');
305+
let resourcePathPrefix = ObsidianHelper.getObsidianResourcePathPrefix();
306+
const fileLink = localFileNameWikiMatch[0].replace('file:///', resourcePathPrefix);
303307
const localPDFPageNumberRegex = /#page=[0-9]+\]\]/;
304308
const localPDFPageNumberMatch = localFileMatchWiki[0].match(localPDFPageNumberRegex);
305309
const wikiAltRegex = /\|.*(?=]])/;
@@ -314,15 +318,19 @@ export const detectLink = (params: { lineText: string; sourceFile: TFile; plugin
314318
};
315319
}
316320
}
321+
***************************************************************************************************
322+
*/
317323

318324
// --> G: Local File Markdown
319325
const localFileMdRegex = /!\[(^$|.*)\]\((file\:\/\/\/|app\:\/\/local\/).*(.pdf|.jpe?g|.png|.gif|.svg|.bmp)(.*)?\)/;
326+
const localFileNameRegex = /(file\:\/\/\/|app\:\/\/local\/).*(.pdf|.jpe?g|.png|.gif|.svg|.bmp)/;
320327
const localFileMatchMd = lineText.match(localFileMdRegex);
321328

322329
if (localFileMatchMd) {
323330
const localFileNameMdMatch = localFileMatchMd[0].match(localFileNameRegex);
324331
if (localFileNameMdMatch) {
325-
const fileLink = localFileNameMdMatch[0].replace('file:///', 'app://local/');
332+
let resourcePathPrefix = ObsidianHelper.getObsidianResourcePathPrefix();
333+
const fileLink = localFileNameMdMatch[0].replace('file:///', resourcePathPrefix);
326334
const localPDFPageNumberRegex = /#page=[0-9]+\)/;
327335
const localPDFPageNumberMatch = localFileMatchMd[0].match(localPDFPageNumberRegex);
328336
const mdAltRegex = /\[(^$|.*)(?=\])/;

src/util/obsidianHelper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-ignore
2-
import { Workspace, Vault, TFile, App, Keymap } from 'obsidian';
2+
import { Workspace, Vault, TFile, App, Keymap, Platform } from 'obsidian';
33

44
// Getting Active Markdown File
55
export const getActiveNoteFile = (workspace: Workspace) => {
@@ -27,3 +27,9 @@ export const pluginIsLoaded = (app: App, pluginId: string) => {
2727
export const livePreviewActive = (app: App): boolean => {
2828
return (app.vault as any).config?.livePreview;
2929
};
30+
31+
export const getObsidianResourcePathPrefix = () => {
32+
// Check https://discord.com/channels/686053708261228577/1103015564055691324/1103035015404728320
33+
//@ts-ignore
34+
return Platform?.resourcePathPrefix || 'app://local/';
35+
};

0 commit comments

Comments
 (0)