Update build system and path handling
- Use Node filesystem APIs instead of shell commands for temp cleanup - Add absolute path helpers for cross-platform compatibility - Generate both main.js and dist/main.js artifacts - Improve error handling in document downloader - Track additional document metadata in sync tracker - Replace Unix-specific `find` with recursive directory traversal The build system now generates two output files (main.js and dist/main.js) for better compatibility with Obsidian's plugin loading. Path handling has been centralized in new utilities to ensure cross-platform behavior, replacing shell commands that had Windows compatibility issues.
This commit is contained in:
+34
-15
@@ -1,11 +1,16 @@
|
||||
import { RmapiNode } from "../types";
|
||||
import RemarkablePlugin from "../main";
|
||||
import { Notice } from "obsidian";
|
||||
import { vaultPathToAbsolute, vaultRelativePath } from "../utils/paths";
|
||||
|
||||
function sanitizeFileName(name: string): string {
|
||||
return name.replace(/[\\/:*?"<>|]/g, "_");
|
||||
}
|
||||
|
||||
function sanitizeRemotePath(remotePath: string): string[] {
|
||||
return remotePath.split("/").filter(Boolean).map(sanitizeFileName);
|
||||
}
|
||||
|
||||
interface DocumentEntry {
|
||||
node: RmapiNode;
|
||||
remotePath: string;
|
||||
@@ -31,6 +36,7 @@ export class DocumentDownloader {
|
||||
} catch (e) {
|
||||
new Notice(`Sync failed: ${e}`);
|
||||
console.error("Sync error:", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,36 +62,49 @@ export class DocumentDownloader {
|
||||
const { node: doc, remotePath } = entry;
|
||||
|
||||
const synced = await this.plugin.tracker.getSyncedDocument(doc.id);
|
||||
if (synced && synced.lastSynced >= doc.modifiedClient) {
|
||||
if (synced && synced.remoteVersion === doc.version && synced.remoteModified === doc.modifiedClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
const safeName = sanitizeFileName(doc.name);
|
||||
const localDir = this.plugin.settings.downloadPath;
|
||||
const localPath = `${localDir}/${safeName}`;
|
||||
const pathSegments = sanitizeRemotePath(remotePath);
|
||||
const safeName = pathSegments.pop() || sanitizeFileName(doc.name);
|
||||
const localDir = vaultRelativePath(this.plugin.settings.downloadPath, ...pathSegments);
|
||||
const localPath = vaultRelativePath(localDir, `${safeName}.rm`);
|
||||
const absoluteLocalPath = vaultPathToAbsolute(this.plugin, localPath);
|
||||
|
||||
try {
|
||||
await this.plugin.app.vault.adapter.mkdir(localDir);
|
||||
} catch (err) {
|
||||
console.warn("mkdir failed (may already exist):", err);
|
||||
}
|
||||
await this.ensureVaultFolder(localDir);
|
||||
|
||||
await this.plugin.rmapi.downloadFile(remotePath, localPath);
|
||||
await this.plugin.rmapi.downloadFile(remotePath, absoluteLocalPath);
|
||||
|
||||
let hasPdf = false;
|
||||
if (this.plugin.settings.convertToPdf) {
|
||||
const pdfPath = `${localDir}/${safeName}.pdf`;
|
||||
await this.plugin.rmapi.downloadAnnotatedPdf(remotePath, pdfPath);
|
||||
const pdfPath = vaultRelativePath(localDir, `${safeName}.pdf`);
|
||||
await this.plugin.rmapi.downloadAnnotatedPdf(remotePath, vaultPathToAbsolute(this.plugin, pdfPath));
|
||||
hasPdf = true;
|
||||
}
|
||||
|
||||
await this.plugin.tracker.trackDocument(doc.id, doc.name, localPath, hasPdf, false);
|
||||
await this.plugin.tracker.trackDocument(doc, localPath, hasPdf, false);
|
||||
|
||||
if (this.plugin.settings.enableHandwritingMd) {
|
||||
const mdPath = `${localDir}/${safeName}.md`;
|
||||
const mdPath = vaultRelativePath(localDir, `${safeName}.md`);
|
||||
const mdResult = await this.plugin.ocrPipeline.processDocument(localPath, mdPath);
|
||||
if (mdResult) {
|
||||
await this.plugin.tracker.trackDocument(doc.id, doc.name, localPath, hasPdf, true);
|
||||
await this.plugin.tracker.trackDocument(doc, localPath, hasPdf, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureVaultFolder(path: string): Promise<void> {
|
||||
const segments = path.split("/").filter(Boolean);
|
||||
let current = "";
|
||||
for (const segment of segments) {
|
||||
current = current ? vaultRelativePath(current, segment) : segment;
|
||||
try {
|
||||
await this.plugin.app.vault.adapter.mkdir(current);
|
||||
} catch (err) {
|
||||
if (!(await this.plugin.app.vault.adapter.exists(current))) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user