What is the simplest implementation of Markdown for a Cocoa application?

前端 未结 6 1245
难免孤独
难免孤独 2020-11-30 18:45

I\'m writing a Cocoa application in Objective-C, and I would like to be able to incorporate Markdown. The user will enter text in Markdown syntax, click an \"export\" button

6条回答
  •  无人及你
    2020-11-30 19:39

    I just used the Sundown implementation which includes SmartyPants support, in an iPad app with great success. Took about 15 minutes to build a test app.

    Assume you have a UITextView *textView (which you setDelegate:self) and also a UIWebView *webView in which to display the results:

    - (void) textViewDidEndEditing:(UITextView *)textView
    {
        NSString *rawMarkdown = [textView text];
        const char * prose = [rawMarkdown UTF8String];  
        struct buf *ib, *ob;       
    
        int length = [rawMarkdown lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
    
        ib = bufnew(length);
        bufgrow(ib, length);
        memcpy(ib->data, prose, length);
        ib->size = length;
    
        ob = bufnew(64);
    
        struct sd_callbacks callbacks;
        struct html_renderopt options;
        struct sd_markdown *markdown;
    
    
        sdhtml_renderer(&callbacks, &options, 0);
        markdown = sd_markdown_new(0, 16, &callbacks, &options);
    
        sd_markdown_render(ob, ib->data, ib->size, markdown);
        sd_markdown_free(markdown);
    
    
        NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];
        [webView loadHTMLString:shinyNewHTML baseURL:[[NSURL alloc] initWithString:@""]];
    
        bufrelease(ib);
        bufrelease(ob);
    }
    

提交回复
热议问题