@@ -9,15 +9,21 @@ import type { PaperImporterPluginSettings } from "./setting_tab";
99
1010export class ImportModal extends Modal {
1111 settings : PaperImporterPluginSettings ;
12+ downloadPdf : boolean ;
1213 importDialog : ReturnType < typeof ImportDialog > | null = null ;
1314 states : Record < string , any > = $state ( {
1415 logs : [ ] ,
1516 downloadProgress : 0 ,
1617 } ) ;
1718
18- constructor ( app : App , settings : PaperImporterPluginSettings ) {
19+ constructor (
20+ app : App ,
21+ settings : PaperImporterPluginSettings ,
22+ downloadPdf : boolean = true
23+ ) {
1924 super ( app ) ;
2025 this . settings = settings ;
26+ this . downloadPdf = downloadPdf ;
2127 }
2228
2329 onOpen ( ) {
@@ -27,12 +33,18 @@ export class ImportModal extends Modal {
2733 target : contentEl ,
2834 props : {
2935 states : this . states ,
36+ downloadPdf : this . downloadPdf ,
3037 onkeypress : async ( e : KeyboardEvent , paperUri : string ) => {
3138 if ( e . key === "Enter" ) {
3239 // Reset progress and messages
3340 this . states . downloadProgress = 0 ;
3441 this . states . logs . length = 0 ;
35- this . states . logs . push ( [ "info" , "Importing paper..." ] ) ;
42+ this . states . logs . push ( [
43+ "info" ,
44+ this . downloadPdf
45+ ? "Importing paper..."
46+ : "Importing metadata..." ,
47+ ] ) ;
3648
3749 let arxivId : string ;
3850 try {
@@ -56,11 +68,15 @@ export class ImportModal extends Modal {
5668 return ;
5769 }
5870
59- this . states . logs . push ( [
60- "success" ,
61- "Paper imported successfully!" ,
62- ] ) ;
63- new Notice ( "Paper imported successfully!" ) ;
71+ // Add a simple success message
72+ this . states . logs . push ( [ "success" , "Import completed!" ] ) ;
73+
74+ // Show a notice based on the last log entry
75+ const lastLog =
76+ this . states . logs [ this . states . logs . length - 1 ] ;
77+ if ( lastLog && lastLog [ 0 ] === "success" ) {
78+ new Notice ( lastLog [ 1 ] ) ;
79+ }
6480
6581 this . close ( ) ;
6682 }
@@ -78,6 +94,19 @@ export class ImportModal extends Modal {
7894 async searchAndImportPaper ( arxivId : string ) : Promise < [ string , string ] > {
7995 const paper = await searchPaper ( arxivId ) ;
8096
97+ let pdfPath = "" ;
98+
99+ if ( this . downloadPdf ) {
100+ pdfPath = await this . downloadPdfFile ( paper ) ;
101+ }
102+
103+ const notePath = await this . createNoteFromPaper ( paper , pdfPath ) ;
104+
105+ this . states . downloadProgress = 100 ;
106+ return [ notePath , pdfPath ] ;
107+ }
108+
109+ private async downloadPdfFile ( paper : any ) : Promise < string > {
81110 const pdfFolder = normalizePath ( this . settings . pdfFolder ) ;
82111
83112 let pdfFolderPath = this . app . vault . getFolderByPath ( pdfFolder ) ! ;
@@ -93,8 +122,14 @@ export class ImportModal extends Modal {
93122 // Check if PDF already exists
94123 const pdfExists = await this . app . vault . adapter . exists ( pdfPath ) ;
95124 if ( pdfExists ) {
96- this . states . logs . push ( [ "error" , `PDF already exists: ${ pdfPath } ` ] ) ;
97- throw new Error ( `PDF already exists: ${ pdfPath } ` ) ;
125+ this . states . logs . push ( [
126+ "warn" ,
127+ `PDF already exists: ${ pdfPath } . Skipping download.` ,
128+ ] ) ;
129+ new Notice (
130+ `PDF already exists: ${ pdfFilename } . Using existing file.`
131+ ) ;
132+ return pdfPath ; // Return the existing PDF path instead of throwing an error
98133 }
99134
100135 // Download PDF with progress tracking
@@ -151,7 +186,6 @@ export class ImportModal extends Modal {
151186 pdfPath ,
152187 arrayBuffer . buffer
153188 ) ;
154- this . states . downloadProgress = 100 ;
155189 } catch ( error ) {
156190 this . states . downloadProgress = 0 ;
157191 this . states . logs . push ( [
@@ -162,7 +196,13 @@ export class ImportModal extends Modal {
162196 }
163197
164198 this . states . logs . push ( [ "info" , `PDF downloaded: ${ pdfPath } ` ] ) ;
199+ return pdfPath ;
200+ }
165201
202+ private async createNoteFromPaper (
203+ paper : any ,
204+ pdfPath : string
205+ ) : Promise < string > {
166206 const noteFolder = normalizePath ( this . settings . noteFolder ) ;
167207
168208 let noteFolderPath = this . app . vault . getFolderByPath ( noteFolder ) ! ;
@@ -180,16 +220,34 @@ export class ImportModal extends Modal {
180220 // Check if note already exists
181221 const noteExists = await this . app . vault . adapter . exists ( notePath ) ;
182222 if ( noteExists ) {
183- this . states . logs . push ( [
184- "error" ,
185- `Note already exists: ${ notePath } ` ,
186- ] ) ;
187- throw new Error ( `Note already exists: ${ notePath } ` ) ;
223+ this . states . logs . push ( [ "warn" , `Note already exists: ${ notePath } ` ] ) ;
224+ new Notice (
225+ `Note already exists: ${ noteFilename } . Opening existing note.`
226+ ) ;
227+ return notePath ; // Return the existing note path instead of creating a new one
188228 }
189229
190- // Load template: external file if specified, otherwise use default
191- let template : string ;
230+ const template = await this . loadTemplate ( ) ;
231+
232+ // Determine PDF link format based on whether we downloaded the PDF
233+ const pdfLink = pdfPath ? `"[[${ pdfPath } ]]"` : `"${ paper . pdfUrl } "` ;
234+
235+ const noteContent = template
236+ . replace ( / { { \s * p a p e r _ i d \s * } } / g, paper . paperId )
237+ . replace ( / { { \s * t i t l e \s * } } / g, `"${ paper . title } "` )
238+ . replace ( / { { \s * a u t h o r s \s * } } / g, paper . authors . join ( ", " ) )
239+ . replace ( / { { \s * d a t e \s * } } / g, paper . date )
240+ . replace ( / { { \s * a b s t r a c t \s * } } / g, `"${ paper . abstract } "` )
241+ . replace ( / { { \s * c o m m e n t s \s * } } / g, `"${ paper . comments } "` )
242+ . replace ( / { { \s * p d f _ l i n k \s * } } / g, pdfLink ) ;
243+
244+ await this . app . vault . adapter . write ( notePath , noteContent ) ;
245+
246+ this . states . logs . push ( [ "info" , `Note created: ${ notePath } ` ] ) ;
247+ return notePath ;
248+ }
192249
250+ private async loadTemplate ( ) : Promise < string > {
193251 if (
194252 this . settings . templateFilePath &&
195253 this . settings . templateFilePath . trim ( )
@@ -202,43 +260,30 @@ export class ImportModal extends Modal {
202260 templatePath
203261 ) ;
204262 if ( exists ) {
205- template = await this . app . vault . adapter . read ( templatePath ) ;
263+ const template = await this . app . vault . adapter . read (
264+ templatePath
265+ ) ;
206266 this . states . logs . push ( [
207267 "info" ,
208268 `Using custom template: ${ templatePath } ` ,
209269 ] ) ;
270+ return template ;
210271 } else {
211272 this . states . logs . push ( [
212273 "warn" ,
213274 `Template file not found: ${ templatePath } , using default template` ,
214275 ] ) ;
215- template = this . getDefaultTemplate ( ) ;
216276 }
217277 } catch ( error ) {
218278 this . states . logs . push ( [
219279 "error" ,
220280 `Failed to read template file: ${ error . message } , using default template` ,
221281 ] ) ;
222- template = this . getDefaultTemplate ( ) ;
223282 }
224- } else {
225- template = this . getDefaultTemplate ( ) ;
226- this . states . logs . push ( [ "info" , "Using default template" ] ) ;
227283 }
228284
229- const noteContent = template
230- . replace ( / { { \s * p a p e r _ i d \s * } } / g, paper . paperId )
231- . replace ( / { { \s * t i t l e \s * } } / g, `"${ paper . title } "` )
232- . replace ( / { { \s * a u t h o r s \s * } } / g, paper . authors . join ( ", " ) )
233- . replace ( / { { \s * d a t e \s * } } / g, paper . date )
234- . replace ( / { { \s * a b s t r a c t \s * } } / g, `"${ paper . abstract } "` )
235- . replace ( / { { \s * c o m m e n t s \s * } } / g, `"${ paper . comments } "` )
236- . replace ( / { { \s * p d f _ l i n k \s * } } / g, `"[[${ pdfPath } ]]"` ) ;
237- await this . app . vault . adapter . write ( notePath , noteContent ) ;
238-
239- this . states . logs . push ( [ "info" , `Note created: ${ notePath } ` ] ) ;
240-
241- return [ notePath , pdfPath ] ;
285+ this . states . logs . push ( [ "info" , "Using default template" ] ) ;
286+ return this . getDefaultTemplate ( ) ;
242287 }
243288
244289 extractArxivId ( text : string ) : string {
0 commit comments