Fixes path handling and adds remote image support

Addresses issues with path construction on Windows by using `path.isAbsolute()` to correctly handle absolute paths.  Improves cross-platform compatibility in temp directory management and binary location.

Adds support for downloading and embedding remote images (http/https URLs) in markdown, enhancing content integration. Provides fallback mechanism with placeholder text upon download failure.
This commit is contained in:
AlexKucera committed 2025-11-04 10:55:24 +01:00
1 parent 536351cbbf
commit 8329c9dd48
6 files changed
+263 -37

No files matched your search

+58 -2
View File
@@ -9,6 +9,7 @@ import { EmbedProcessor, EmbedProcessorConfig } from './preprocessors/EmbedProce
import { CalloutProcessor } from './preprocessors/CalloutProcessor';
import { MetadataExtractor } from './preprocessors/MetadataExtractor';
import { HorizontalRuleProcessor } from './preprocessors/HorizontalRuleProcessor';
import * as path from 'path';
export interface PreprocessorOptions {
/** Preserve existing frontmatter */
@@ -141,7 +142,10 @@ export class MarkdownPreprocessor {
// Step 5: Convert embeds FIRST (before wikilinks to avoid .md extension being added)
result.content = this.embedProcessor.processEmbeds(result.content, result);
// Step 5.5: Handle standard markdown images with remote URLs
result.content = this.processStandardMarkdownImages(result.content, result);
// Step 6: Convert wikilinks (after embeds are processed)
result.content = this.wikilinkProcessor.processWikilinks(result.content, result);
@@ -159,7 +163,59 @@ export class MarkdownPreprocessor {
return result;
}
/**
* Process standard markdown images and track remote URLs for download
* Remote images are marked for later download and embedding
*/
private processStandardMarkdownImages(content: string, result: PreprocessingResult): string {
// Pattern to match standard markdown images: ![alt](url)
const markdownImagePattern = /!\[([^\]]*)\]\(([^)]+)\)/g;
return content.replace(markdownImagePattern, (match, alt, url) => {
// Check if this is a remote URL (http:// or https://)
const isRemoteUrl = /^https?:\/\//i.test(url.trim());
if (isRemoteUrl) {
const cleanUrl = url.trim();
// Extract filename from URL for tracking
let fileName = 'remote-image.png';
try {
const urlObj = new URL(cleanUrl);
fileName = path.basename(urlObj.pathname) || fileName;
} catch {
// Use default if URL parsing fails
}
const baseName = fileName.replace(/\.[^/.]+$/, ""); // Remove extension
// Create a marker that the export process can detect and replace
const marker = `IMAGE_EMBED_MARKER:${cleanUrl}:${baseName}:${alt || ''}`;
// Add to processing queue for later download and embedding
if (!result.metadata.imageEmbeds) {
result.metadata.imageEmbeds = [];
}
result.metadata.imageEmbeds.push({
originalPath: cleanUrl,
sanitizedPath: cleanUrl, // For remote URLs, sanitized = original
fileName: fileName,
baseName: baseName,
sizeOrAlt: alt,
marker: marker
});
result.warnings.push(`Remote URL image queued for download: ${cleanUrl}`);
return marker;
}
// Keep local images as-is for normal processing
return match;
});
}
/**
* Update configuration
*/
+4 -1
View File
@@ -131,13 +131,16 @@ export class BinaryLocator {
* @returns Array of possible plugin directory paths
*/
private static getPossiblePluginDirs(plugin: obsidianTypstPDFExport | undefined, pluginDirName: string, configDir: string): string[] {
const pathUtils = plugin ? new PathUtils(plugin.app) : null;
return [
// Strategy 1: From process.cwd() if it's in the vault
path.join(process.cwd(), configDir, 'plugins', pluginDirName),
// Strategy 2: Assuming we're running from vault root
path.join(configDir, 'plugins', pluginDirName),
// Strategy 3: From vault base path if plugin is available
...(plugin ? [path.join(new PathUtils(plugin.app).getVaultPath(), plugin.manifest.dir!)] : [])
// Use PathUtils.joinPath to handle absolute paths correctly on Windows
...(pathUtils ? [pathUtils.joinPath(pathUtils.getVaultPath(), plugin!.manifest.dir!)] : [])
];
}