@@ -28,31 +28,11 @@ __export(main_exports, {
2828} ) ;
2929module . exports = __toCommonJS ( main_exports ) ;
3030var import_obsidian = require ( "obsidian" ) ;
31- var DEFAULT_SETTINGS = {
32- imageFolderPath : "zAttachments"
33- // Default folder
34- } ;
35- var ImageCollectorSettingTab = class extends import_obsidian . PluginSettingTab {
36- constructor ( app , plugin ) {
37- super ( app , plugin ) ;
38- this . plugin = plugin ;
39- }
40- display ( ) {
41- const { containerEl } = this ;
42- containerEl . empty ( ) ;
43- containerEl . createEl ( "h2" , { text : "Image Collector Settings" } ) ;
44- new import_obsidian . Setting ( containerEl ) . setName ( "Image Folder Path" ) . setDesc ( "The folder where images are stored" ) . addText ( ( text ) => text . setPlaceholder ( "Enter your image folder path" ) . setValue ( this . plugin . settings . imageFolderPath ) . onChange ( async ( value ) => {
45- this . plugin . settings . imageFolderPath = value ;
46- await this . plugin . saveSettings ( ) ;
47- } ) ) ;
48- }
49- } ;
5031var ImageCollectorPlugin = class extends import_obsidian . Plugin {
5132 async onload ( ) {
52- await this . loadSettings ( ) ;
5333 this . addCommand ( {
54- id : "export-markdown-images-command " ,
55- name : "Export Markdown Images " ,
34+ id : "export-markdown-images" ,
35+ name : "Export markdown images " ,
5636 callback : ( ) => {
5737 const activeFile = this . app . workspace . getActiveFile ( ) ;
5838 if ( activeFile && activeFile instanceof import_obsidian . TFile && activeFile . extension === "md" ) {
@@ -62,47 +42,37 @@ var ImageCollectorPlugin = class extends import_obsidian.Plugin {
6242 }
6343 }
6444 } ) ;
65- this . addSettingTab ( new ImageCollectorSettingTab ( this . app , this ) ) ;
6645 this . registerEvent ( this . app . workspace . on ( "file-menu" , ( menu , file ) => {
6746 if ( file instanceof import_obsidian . TFile && file . extension === "md" ) {
6847 menu . addItem ( ( item ) => {
69- item . setTitle ( "Export Images with My Plugin " ) . setIcon ( "document-export" ) . onClick ( ( ) => {
48+ item . setTitle ( "Export images " ) . setIcon ( "document-export" ) . onClick ( ( ) => {
7049 this . exportMarkdownImages ( file ) ;
7150 } ) ;
7251 } ) ;
7352 }
7453 } ) ) ;
7554 }
76- async loadSettings ( ) {
77- this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
78- }
79- async saveSettings ( ) {
80- await this . saveData ( this . settings ) ;
81- }
8255 async exportMarkdownImages ( file ) {
83- const activeFile = this . app . workspace . getActiveFile ( ) ;
84- if ( ! activeFile || activeFile . extension !== "md" ) {
85- new import_obsidian . Notice ( "No active markdown file." ) ;
86- return ;
87- }
88- const fileContent = await this . app . vault . read ( activeFile ) ;
89- const imageRegex = / ! \[ \[ ? ( .* ?) \] ? \] / g;
56+ const fileContent = await this . app . vault . read ( file ) ;
57+ const imageRegex = / ! \[ \[ ( .* ?) \] \] | ! \[ (?: .* ?) \] \( ( .* ?) \s * ( " .* ?" ) ? \) / g;
9058 let match ;
9159 const images = [ ] ;
9260 while ( ( match = imageRegex . exec ( fileContent ) ) !== null ) {
93- const imagePath = match [ 1 ] . includes ( "|" ) ? match [ 1 ] . split ( "|" ) [ 0 ] : match [ 1 ] ;
94- images . push ( imagePath ) ;
61+ let imagePath = match [ 1 ] || match [ 2 ] ;
62+ if ( imagePath ) {
63+ imagePath = decodeURIComponent ( imagePath . trim ( ) ) ;
64+ images . push ( imagePath ) ;
65+ }
9566 }
9667 if ( images . length === 0 ) {
9768 new import_obsidian . Notice ( "No images found in the markdown file." ) ;
9869 return ;
9970 }
100- const targetFolderName = `${ activeFile . basename } images` ;
71+ const targetFolderName = `${ file . basename } images` ;
10172 await this . app . vault . createFolder ( targetFolderName ) . catch ( ( ) => {
10273 } ) ;
10374 for ( const imagePath of images ) {
104- const resolvedImagePath = this . resolveImagePath ( activeFile , imagePath ) ;
105- const imageFile = this . app . vault . getAbstractFileByPath ( resolvedImagePath ) ;
75+ const imageFile = this . app . metadataCache . getFirstLinkpathDest ( imagePath , file . path ) ;
10676 if ( imageFile instanceof import_obsidian . TFile ) {
10777 try {
10878 const imageContent = await this . app . vault . readBinary ( imageFile ) ;
@@ -114,25 +84,9 @@ var ImageCollectorPlugin = class extends import_obsidian.Plugin {
11484 console . error ( `Failed to export image ${ imageFile . name } :` , error ) ;
11585 }
11686 } else {
117- new import_obsidian . Notice ( `Image not found: ${ resolvedImagePath } ` ) ;
118- console . error ( `Image not found: ${ resolvedImagePath } ` ) ;
119- }
120- }
121- }
122- resolveImagePath ( activeFile , imagePath ) {
123- let normalizedPath = imagePath . replace ( / \[ \[ | \] \] / g, "" ) ;
124- let basePath = this . settings . imageFolderPath ;
125- if ( normalizedPath . startsWith ( "/" ) || normalizedPath . startsWith ( `${ basePath } /` ) ) {
126- return normalizedPath ;
127- }
128- if ( activeFile . parent ) {
129- const folderPath = activeFile . parent . path ;
130- const fullPath = `${ folderPath } /${ normalizedPath } ` ;
131- const fileExists = this . app . vault . getAbstractFileByPath ( fullPath ) ;
132- if ( fileExists ) {
133- return fullPath ;
87+ new import_obsidian . Notice ( `Image not found: ${ imagePath } ` ) ;
88+ console . error ( `Image not found: ${ imagePath } ` ) ;
13489 }
13590 }
136- return `${ basePath } /${ normalizedPath } ` ;
13791 }
13892} ;
0 commit comments