Skip to content

Commit 81eb7bf

Browse files
committed
md转html - 优先请求服务器,拿不到数据时用本地模板
1 parent c41cca5 commit 81eb7bf

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

Coding_iOS/Controllers/FileViewController.m

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#import "Coding_FileManager.h"
1414
#import "WebContentManager.h"
1515
#import "FunctionTipsManager.h"
16-
#import <MMMarkdown/MMMarkdown.h>
1716
#import "EaseToolBar.h"
1817

1918
#import "FileActivitiesViewController.h"
@@ -178,21 +177,14 @@ - (void)loadWebView:(NSURL *)fileUrl{
178177
}];
179178
}
180179
if ([self.fileType isEqualToString:@"md"]){
181-
NSError *error = nil;
182-
NSString *htmlStr;
183-
@try {
184-
NSString *mdStr = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:&error];
185-
htmlStr = [MMMarkdown HTMLStringWithMarkdown:mdStr error:&error];
186-
}
187-
@catch (NSException *exception) {
188-
htmlStr = @"加载失败!";
189-
}
190-
191-
if (error) {
192-
htmlStr = @"加载失败!";
193-
}
194-
NSString *contentStr = [WebContentManager markdownPatternedWithContent:htmlStr];
195-
[self.contentWebView loadHTMLString:contentStr baseURL:nil];
180+
NSString *mdStr = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil];
181+
[self.activityIndicator startAnimating];
182+
[[Coding_NetAPIManager sharedManager] request_MDHtmlStr_WithMDStr:mdStr inProject:nil andBlock:^(id data, NSError *error) {
183+
NSString *htmlStr;
184+
htmlStr = data? data: @"加载失败";
185+
NSString *contentStr = [WebContentManager markdownPatternedWithContent:htmlStr];
186+
[self.contentWebView loadHTMLString:contentStr baseURL:nil];
187+
}];
196188
}else if ([self.fileType isEqualToString:@"html"]){
197189
NSString* htmlString = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil];
198190
[self.contentWebView loadHTMLString:htmlString baseURL:nil];

Coding_iOS/Util/Manager/Coding_NetAPIManager.m

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "JDStatusBarNotification.h"
1111
#import "UnReadManager.h"
1212
#import <NYXImagesKit/NYXImagesKit.h>
13+
#import <MMMarkdown/MMMarkdown.h>
1314
#import "MBProgressHUD+Add.h"
1415

1516
@implementation Coding_NetAPIManager
@@ -2172,16 +2173,32 @@ - (void)request_MDHtmlStr_WithMDStr:(NSString *)mdStr inProject:(Project *)proje
21722173
path = [NSString stringWithFormat:@"api/user/%@/project/%@/markdownNoAt", project.owner_user_name, project.name];
21732174
}
21742175
[[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:path withParams:@{@"content" : mdStr} withMethodType:Post andBlock:^(id data, NSError *error) {
2175-
if (data) {
2176-
[MobClick event:kUmeng_Event_Request_Get label:@"md-html转化"];
2176+
[MobClick event:kUmeng_Event_Request_Get label:@"md-html转化"];
21772177

2178+
if (data) {
21782179
id resultData = [data valueForKeyPath:@"data"];
21792180
block(resultData, nil);
21802181
}else{
2181-
block(nil, error);
2182+
block([self localMDHtmlStr_WithMDStr:mdStr], error);
21822183
}
21832184
}];
21842185
}
2186+
2187+
- (NSString *)localMDHtmlStr_WithMDStr:(NSString *)mdStr{
2188+
NSError *error = nil;
2189+
NSString *htmlStr;
2190+
@try {
2191+
htmlStr = [MMMarkdown HTMLStringWithMarkdown:mdStr error:&error];
2192+
}
2193+
@catch (NSException *exception) {
2194+
htmlStr = @"加载失败!";
2195+
}
2196+
if (error) {
2197+
htmlStr = @"加载失败!";
2198+
}
2199+
return htmlStr;
2200+
}
2201+
21852202
- (void)request_VerifyTypeWithBlock:(void (^)(VerifyType type, NSError *error))block{
21862203
[[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"api/user/2fa/method" withParams:nil withMethodType:Get andBlock:^(id data, NSError *error) {
21872204
if (data) {

0 commit comments

Comments
 (0)