Skip to content

Commit 8276d95

Browse files
committed
fix(windows): Fix windows path compatibility issues.
1 parent 64ae9e9 commit 8276d95

File tree

7 files changed

+73
-18
lines changed

7 files changed

+73
-18
lines changed

src/html-generation/asset-handler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ export class AssetHandler
3030
private static vaultPluginsPath: Path;
3131
private static thisPluginPath: Path;
3232

33+
3334
// this path is used to generate the relative path to the images folder, likewise for the other paths
34-
public static readonly mediaFolderName: Path = new Path("lib/media");
35-
public static readonly jsFolderName: Path = new Path("lib/scripts");
36-
public static readonly cssFolderName: Path = new Path("lib/styles");
35+
public static readonly mediaFolderName: Path = Path.getAssetsPath('media');
36+
public static readonly jsFolderName: Path = Path.getAssetsPath('scripts');
37+
public static readonly cssFolderName: Path = Path.getAssetsPath('styles');
3738

3839
public static appStyles: string = "";
3940
public static mathStyles: string = "";

src/html-generation/html-generator.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ export class HTMLGenerator {
110110
// inject file tree
111111
if (InvioSettingTab.settings.includeFileTree) {
112112
let tree = GlobalDataGenerator.getFileTree();
113-
const rootDir = new Path(file.exportPath.asString.split('/')[0])
113+
const rootDir = file.exportPath.getRootDirFromString();
114+
console.log('file.exportPath: ', file.exportPath, file.exportPath.asString, rootDir);
115+
114116
if (rootDir !== rootPath) {
115117
const remoteRoot = rootPath.asString;
116118
const prefix = remoteRoot.startsWith('/') ? '' : '/';
@@ -243,9 +245,9 @@ export class HTMLGenerator {
243245
if (addSelfToDownloads) file.downloads.push(file.getSelfDownloadable());
244246
file.downloads.push(...outlinedImages);
245247

246-
const downloadDir = new Path(file.exportPath.asString.split('/')[0]);
248+
const downloadDir = file.exportPath.getRootDirFromString();
247249
await AssetHandler.reparseAppStyles(downloadDir, rootPath.asString, remoteDomain);
248-
log.info('assets download root path: ', rootPath, downloadDir)
250+
log.info('assets download root path and dir: ', rootPath, downloadDir)
249251
file.downloads.push(...await AssetHandler.getDownloads(downloadDir));
250252

251253
if (InvioSettingTab.settings.makeNamesWebStyle) {
@@ -881,7 +883,7 @@ export class HTMLGenerator {
881883
}
882884

883885
private static generateRootDirNode(file: ExportFile, rootPath: string, usingDocument: Document) {
884-
const rootDir = rootPath || file.exportPath.directory.asString?.split('/')[0];
886+
const rootDir = rootPath || file.exportPath.directory.getRootDirFromString().asString;
885887
const container = usingDocument.createElement('div');
886888
container.id = 'invio-hidden-data-node'
887889
container.style.display = 'none';

src/html-generation/markdown-renderer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AssetHandler } from "./asset-handler";
88
import { RenderLog } from "./render-log";
99
import { StatsView } from "src/statsView";
1010
import type InvioPlugin from "../main"; // unavoidable
11-
import { Path } from "src/utils/path.js";
11+
import { Path, parseOSFromUA } from "src/utils/path.js";
1212
export namespace MarkdownRenderer
1313
{
1414
export let problemLog = "";
@@ -193,13 +193,16 @@ export namespace MarkdownRenderer
193193
// @ts-ignore
194194
// renderLeaf.view.containerEl.win.resizeTo(1, 1);
195195
// @ts-ignore
196-
// renderLeaf.view.containerEl.win.moveTo(window.screen.width + 450, window.screen.height + 450);
196+
renderLeaf.view.containerEl.win.moveTo(window.screen.width + 450, window.screen.height + 450);
197197

198198
// @ts-ignore
199199
const renderBrowserWindow = window.electron.remote.BrowserWindow.getFocusedWindow();
200200
if (renderBrowserWindow) {
201-
renderBrowserWindow.hide();
202-
// renderBrowserWindow.setAlwaysOnTop(false, "floating", 1);
201+
if (parseOSFromUA(navigator.userAgent) !== 'win32') {
202+
console.log('is windows platform');
203+
renderBrowserWindow.hide();
204+
renderBrowserWindow.setAlwaysOnTop(false, "floating", 1);
205+
}
203206
renderBrowserWindow.webContents.setFrameRate(120);
204207
renderBrowserWindow.on("close", () =>
205208
{

src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ export default class InvioPlugin extends Plugin {
283283
// this.app.vault.getAllLoadedFiles
284284
// TODO: List only concerned files, only source of truth
285285
// *.conflict.md files is for data backup when conflicts happened
286-
const local = this.app.vault.getMarkdownFiles().filter(file => file.path.startsWith(this.settings.localWatchDir) && !file.path.endsWith('.conflict.md'))
286+
// const local = this.app.vault.getMarkdownFiles().filter(file => file.path.startsWith(this.settings.localWatchDir + '/') && !file.path.endsWith('.conflict.md'))
287+
const local = this.app.vault.getMarkdownFiles().filter(file => new Path(file.path).isInsideDir(this.settings.localWatchDir) && !file.path.endsWith('.conflict.md'))
287288
log.info('local file path list: ', local);
288289
// const local = this.app.vault.getAllLoadedFiles();
289290
const localHistory = await loadFileHistoryTableByVault(
@@ -378,9 +379,8 @@ export default class InvioPlugin extends Plugin {
378379
// })
379380
// );
380381
let allFiles = this.app.vault.getMarkdownFiles();
381-
const basePath = new Path(this.settings.localWatchDir);
382382
// if we are at the root path export all files, otherwise only export files in the folder we are exporting
383-
allFiles = allFiles.filter((file: TFile) => new Path(file.path).directory.asString.startsWith(basePath.asString) && (file.extension === "md") && (!file.name.endsWith('.conflict.md')));
383+
allFiles = allFiles.filter((file: TFile) => new Path(file.path).isInsideDir(this.settings.localWatchDir) && (file.extension === "md") && (!file.name.endsWith('.conflict.md')));
384384
// Make functions of StatsView static
385385
view = await HTMLGenerator.beginBatch(this, allFiles);
386386
log.info('init stats view: ', view);

src/remoteForS3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export const listFromRemote = async (
420420
Bucket: s3Config.s3BucketName,
421421
} as ListObjectsV2CommandInput;
422422
if (prefix !== undefined) {
423-
confCmd.Prefix = prefix;
423+
confCmd.Prefix = prefix + (prefix.endsWith('/') ? '' : '/');
424424
}
425425

426426
const contents = [] as _Object[];

src/utils/path.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
const pathTools = require('upath');
22
import { existsSync } from 'fs';
33
import { FileSystemAdapter, Notice } from 'obsidian';
4-
import { Utils } from './utils';
4+
import { Utils, NodeJSPlatform } from './utils';
55
import { promises as fs } from 'fs';
66
import internal from 'stream';
77

8+
export function parseOSFromUA(userAgent: string): NodeJSPlatform | undefined {
9+
const osRegex = /(Windows NT|Mac OS X|Linux|Android|iOS|CrOS)[/ ]([\d._]+)/;
10+
const match = userAgent.match(osRegex);
11+
12+
if (match && match.length >= 3) {
13+
const osName = match[1].toLowerCase();
14+
switch (osName) {
15+
case 'windows nt':
16+
return 'win32';
17+
case 'mac os x':
18+
return 'darwin';
19+
case 'linux':
20+
return 'linux';
21+
case 'android':
22+
return 'linux';
23+
case 'ios':
24+
return 'darwin';
25+
case 'cros':
26+
return 'linux';
27+
default:
28+
return undefined;
29+
}
30+
}
31+
32+
return undefined;
33+
}
34+
35+
const isWindows: boolean = (typeof process.platform === 'string' ?
36+
process.platform :
37+
parseOSFromUA(navigator.userAgent)) === "win32";
38+
39+
const PATH_SPLITER = isWindows ? '\\' : '/';
40+
841
export class Path
942
{
1043
private static logQueue: { title: string, message: string, type: "info" | "warn" | "error" | "fatal" }[] = [];
@@ -30,8 +63,9 @@ export class Path
3063
private _isFile: boolean = false;
3164
private _exists: boolean | undefined = undefined;
3265
private _workingDirectory: string;
66+
private _spliter: string = PATH_SPLITER;
3367

34-
private _isWindows: boolean = process.platform === "win32";
68+
private _isWindows: boolean = isWindows;
3569

3670
constructor(path: string, workingDirectory: string = Path.vaultPath.asString)
3771
{
@@ -42,6 +76,12 @@ export class Path
4276
if (this.isAbsolute) this._workingDirectory = "";
4377
}
4478

79+
// @params
80+
// type - media | scripts | styles
81+
static getAssetsPath(type: string) {
82+
return new Path(['lib', type].join(PATH_SPLITER));
83+
}
84+
4585
reparse(path: string): Path
4686
{
4787
let parsed = Path.parsePath(path);
@@ -189,6 +229,15 @@ export class Path
189229
return new Path("/", "");
190230
}
191231

232+
getRootDirFromString() {
233+
return new Path(this.asString.split(this._spliter)[0]);
234+
}
235+
236+
isInsideDir(dirname: string) {
237+
const withSpliter = dirname?.endsWith(this._spliter);
238+
return this.asString.startsWith(dirname + (withSpliter ? '' : this._spliter));
239+
}
240+
192241
static toWebStyle(path: string): string
193242
{
194243
return path.replaceAll(" ", "-").replaceAll(/-{2,}/g, "-").replace(".-", "-").toLowerCase();

src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { StatsView } from 'src/statsView';
88

99
/* @ts-ignore */
1010
const dialog: Electron.Dialog = require('electron').remote.dialog;
11-
11+
export type NodeJSPlatform = 'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32';
1212
export class Utils
1313
{
1414
static padStringBeggining(str: string, length: number, char: string)

0 commit comments

Comments
 (0)