@@ -20,8 +20,9 @@ import rehypeReact from 'rehype-react'
20
20
import CodeFence from '../components/atoms/markdown/CodeFence'
21
21
import { getGlobalCss , selectTheme } from './styled/styleUtil'
22
22
import yaml from 'yaml'
23
+ import { convertHtmlStringToPdfBlob } from './electronOnly'
23
24
24
- const filenamifyNoteTitle = function ( noteTitle : string ) : string {
25
+ export function filenamifyNoteTitle ( noteTitle : string ) : string {
25
26
return filenamify ( noteTitle . toLowerCase ( ) . replace ( / \s + / g, '-' ) )
26
27
}
27
28
@@ -192,99 +193,59 @@ export const exportNoteAsPdfFile = async (
192
193
note : NoteDoc ,
193
194
preferences : Preferences ,
194
195
pushMessage : ( context : any ) => any ,
195
- { includeFrontMatter } : { includeFrontMatter : boolean } ,
196
196
previewStyle ?: string
197
197
) : Promise < void > => {
198
- await unified ( )
199
- . use ( remarkParse )
200
- . use ( remarkEmoji , { emoticon : false } )
201
- . use ( [ remarkRehype , { allowDangerousHTML : true } ] )
202
- . use ( rehypeRaw )
203
- . use ( rehypeSanitize , schema )
204
- . use ( remarkMath )
205
- . use ( rehypeCodeMirror , {
206
- ignoreMissing : true ,
207
- theme : preferences [ 'markdown.codeBlockTheme' ] ,
208
- } )
209
- . use ( rehypeKatex , { output : 'htmlAndMathml' } )
210
- . use ( rehypeReact , {
211
- createElement : React . createElement ,
212
- components : {
213
- pre : CodeFence ,
214
- } ,
215
- } )
216
- . use ( rehypeStringify )
217
- . process ( note . content , ( err , file ) => {
218
- if ( err != null ) {
219
- pushMessage ( {
220
- title : 'Note processing failed' ,
221
- description : 'Please check markdown syntax and try again later.' ,
222
- } )
223
- return
224
- }
225
-
226
- const stringifiedMdContent = file . toString ( ) . trim ( ) + '\n'
227
- const { BrowserWindow } = window . require ( 'electron' ) . remote
228
- const windowOptions = {
229
- webPreferences : {
230
- nodeIntegration : true ,
231
- webSecurity : false ,
232
- javascript : false ,
198
+ try {
199
+ const output = await unified ( )
200
+ . use ( remarkParse )
201
+ . use ( remarkEmoji , { emoticon : false } )
202
+ . use ( [ remarkRehype , { allowDangerousHTML : true } ] )
203
+ . use ( rehypeRaw )
204
+ . use ( rehypeSanitize , schema )
205
+ . use ( remarkMath )
206
+ . use ( rehypeCodeMirror , {
207
+ ignoreMissing : true ,
208
+ theme : preferences [ 'markdown.codeBlockTheme' ] ,
209
+ } )
210
+ . use ( rehypeKatex , { output : 'htmlAndMathml' } )
211
+ . use ( rehypeReact , {
212
+ createElement : React . createElement ,
213
+ components : {
214
+ pre : CodeFence ,
233
215
} ,
234
- show : false ,
235
- }
236
- const win = new BrowserWindow ( windowOptions )
237
- const htmlStr = generatePrintToPdfHTML (
238
- stringifiedMdContent ,
239
- preferences ,
240
- previewStyle
241
- )
242
- const encodedStr = encodeURIComponent ( htmlStr )
243
- win . loadURL ( 'data:text/html;charset=UTF-8,' + encodedStr )
244
- win . webContents . on ( 'did-finish-load' , function ( ) {
245
- // Enable when newer version of electron is available
246
- const tagsStr =
247
- note . tags . length > 0 ? `, tags: [${ note . tags . join ( ' ' ) } ]` : ''
248
- const headerFooter : Record < string , string > = {
249
- title : `${ note . title } ${ tagsStr } ` ,
250
- url : `file://${ filenamifyNoteTitle ( note . title ) } .pdf` ,
251
- }
252
- const printOpts = {
253
- // Needed for codemirorr themes (backgrounds)
254
- printBackground : true ,
255
- // Enable margins if header footer is printed
256
- // No margins 1, default margins 0, 2 - minimum margins
257
- marginsType : includeFrontMatter ? 0 : 1 ,
258
- pageSize : 'A4' , // This could be chosen by user,
259
- headerFooter : includeFrontMatter ? headerFooter : undefined ,
260
- }
261
- win . webContents
262
- . printToPDF ( printOpts )
263
- . then ( ( data ) => {
264
- if ( data ) {
265
- // We got the PDF - offer the user to save it
266
- const pdfName = `${ filenamifyNoteTitle ( note . title ) } .pdf`
267
- const pdfBlob = new Blob ( [ data ] , {
268
- type : 'application/pdf' , // application/octet-stream
269
- } )
270
- downloadBlob ( pdfBlob , pdfName )
271
- } else {
272
- pushMessage ( {
273
- title : 'PDF export failed' ,
274
- description : 'Please try again later. Reason: Unknown' ,
275
- } )
276
- }
277
- // Destroy window (not shown but disposes it)
278
- win . destroy ( )
279
- } )
280
- . catch ( ( err ) => {
281
- pushMessage ( {
282
- title : 'PDF export failed' ,
283
- description : 'Please try again later.' + ( err ? err : 'Unknown' ) ,
284
- } )
285
- } )
286
216
} )
287
- return
217
+ . use ( rehypeStringify )
218
+ . process ( note . content )
219
+ const markdownContent = output . toString ( 'utf-8' ) . trim ( ) + '\n'
220
+ const htmlString = generatePrintToPdfHTML (
221
+ markdownContent ,
222
+ preferences ,
223
+ previewStyle
224
+ )
225
+
226
+ // Enable when newer version of electron is available
227
+ // const tagsStr =
228
+ // note.tags.length > 0 ? `, tags: [${note.tags.join(' ')}]` : ''
229
+ // const headerFooter: Record<string, string> = {
230
+ // title: `${note.title}${tagsStr}`,
231
+ // url: `file://${filenamifyNoteTitle(note.title)}.pdf`,
232
+ // }
233
+ const printOpts = {
234
+ // Needed for codemirorr themes (backgrounds)
235
+ printBackground : true ,
236
+ // Enable margins if header footer is printed
237
+ // No margins 1, default margins 0, 2 - minimum margins
238
+ // marginsType: includeFrontMatter ? 0 : 1,
239
+ pageSize : 'A4' , // This could be chosen by user,
240
+ // headerFooter: includeFrontMatter ? headerFooter : undefined,
241
+ }
242
+ const pdfBlob = await convertHtmlStringToPdfBlob ( htmlString , printOpts )
243
+ const pdfName = `${ filenamifyNoteTitle ( note . title ) } .pdf`
244
+ downloadBlob ( pdfBlob , pdfName )
245
+ } catch ( error ) {
246
+ pushMessage ( {
247
+ title : 'PDF export failed' ,
248
+ description : error . message ,
288
249
} )
289
- return
250
+ }
290
251
}
0 commit comments