@@ -10,6 +10,8 @@ import { extensionViews } from '../../tree-view-providers/tree-view-registration
10
10
import { postPdfTemplateBuilder } from './post-pdf-template-builder' ;
11
11
import { chromiumPathProvider } from '../../utils/chromium-path-provider' ;
12
12
import { Settings } from '../../services/settings.service' ;
13
+ import { accountService } from '../../services/account.service' ;
14
+ import { AlertService } from '../../services/alert.service' ;
13
15
14
16
const launchBrowser = async (
15
17
chromiumPath : string
@@ -42,7 +44,8 @@ const exportOne = async (
42
44
post : Post ,
43
45
page : puppeteer . Page ,
44
46
targetFileUri : Uri ,
45
- progress : Progress < { message : string ; increment : number } >
47
+ progress : Progress < { message : string ; increment : number } > ,
48
+ blogApp : string
46
49
) => {
47
50
let message = `[${ idx + 1 } /${ total } ]正在导出 - ${ post . title } ` ;
48
51
const report = ( increment : number ) => {
@@ -52,7 +55,7 @@ const exportOne = async (
52
55
} ) ;
53
56
} ;
54
57
report ( 10 ) ;
55
- let html = await postPdfTemplateBuilder . build ( post ) ;
58
+ let html = await postPdfTemplateBuilder . build ( post , blogApp ) ;
56
59
report ( 15 ) ;
57
60
// Wait for code block highlight finished
58
61
await Promise . all ( [
@@ -151,23 +154,20 @@ const handlePostInput = (post: Post): Promise<Post[]> => {
151
154
152
155
const handleUriInput = async ( uri : Uri ) : Promise < Post [ ] > => {
153
156
const posts : Post [ ] = [ ] ;
154
- const postId = PostFileMapManager . getPostId ( uri . fsPath ) ;
155
- let inputPost : Post | undefined ;
156
- if ( postId && postId > 0 ) {
157
- inputPost = ( await postService . fetchPostEditDto ( postId ) ) ?. post ;
158
- } else {
159
- const { fsPath } = uri ;
160
- inputPost = Object . assign ( ( await postService . fetchPostEditDto ( - 1 ) ) ?. post , {
157
+ const { fsPath } = uri ;
158
+ const postId = PostFileMapManager . getPostId ( fsPath ) ;
159
+ const { post : inputPost } = ( await postService . fetchPostEditDto ( postId && postId > 0 ? postId : - 1 ) ) ?? { } ;
160
+
161
+ if ( ! inputPost ) {
162
+ return [ ] ;
163
+ } else if ( inputPost . id <= 0 ) {
164
+ Object . assign ( inputPost , {
161
165
id : - 1 ,
162
166
title : path . basename ( fsPath , path . extname ( fsPath ) ) ,
163
167
postBody : new TextDecoder ( ) . decode ( await workspace . fs . readFile ( uri ) ) ,
164
168
} as Post ) ;
165
169
}
166
170
167
- if ( ! inputPost ) {
168
- return [ ] ;
169
- }
170
-
171
171
posts . push ( inputPost ) ;
172
172
173
173
return posts ;
@@ -187,6 +187,13 @@ const exportPostToPdf = async (input: Post | Uri): Promise<void> => {
187
187
if ( ! chromiumPath ) {
188
188
return ;
189
189
}
190
+ const {
191
+ curUser : { blogApp } ,
192
+ } = accountService ;
193
+
194
+ if ( ! blogApp ) {
195
+ return AlertService . warning ( '无法获取到博客地址, 请检查登录状态' ) ;
196
+ }
190
197
191
198
reportErrors (
192
199
await window . withProgress < string [ ] | undefined > (
@@ -216,7 +223,7 @@ const exportPostToPdf = async (input: Post | Uri): Promise<void> => {
216
223
const { length : total } = selectedPosts ;
217
224
for ( const post of selectedPosts ) {
218
225
try {
219
- await exportOne ( idx ++ , total , post , page , dir ! , progress ) ;
226
+ await exportOne ( idx ++ , total , post , page , dir ! , progress , blogApp ) ;
220
227
} catch ( err ) {
221
228
errors . push ( `导出"${ post . title } 失败", ${ err } ` ) ;
222
229
}
0 commit comments