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

前端 未结 6 1246
难免孤独
难免孤独 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:31

    I had a look at the various options, and in the end found libsoldout, a very small C implementation that's quite easy to integrate. You just need to include array.[ch], buffer.[ch], markdown.[ch], and renderers.[ch] in your Xcode project, then you can convert an NSString from markdown to HTML like so:

    NSString *rawMarkdown;
    const char * prose = [rawMarkdown UTF8String];  
    struct buf *ib, *ob;       
    
    int length = rawMarkdown.length + 1;
    
    ib = bufnew(length);
    bufgrow(ib, length);
    memcpy(ib->data, prose, length);
    ib->size = length;
    
    ob = bufnew(64);
    markdown(ob, ib, &mkd_xhtml);
    
    NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];
    NSLog(@"%@", shinyNewHTML);
    
    bufrelease(ib);
    bufrelease(ob);
    

提交回复
热议问题