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

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

    I found problems with processing large amounts of markdown with these C-based libraries.

    There's a very simple Obj-C library that worked for me here:

    https://github.com/mdiep/MMMarkdown


    Steps to use MMMarkdown:

    1. Build the OS X or iOS target

    2. Copy include/MMMarkdown.h and either lib/libMMMarkdown-Mac.a or lib/libMMMarkdown-iOS.a into your project

    3. Then the code is:

    #import "MMMarkdown.h"
    
    NSError  *error;
    NSString *markdown   = @"# Example\nWhat a library!";
    NSString *htmlString = [MMMarkdown HTMLStringWithMarkdown:markdown error:&error];
    // Returns @"<h1>Example</h1>\n<p>What a library!</p>"
    
    0 讨论(0)
  • 2020-11-30 19:23
    1. Oliver Letterer's GHMarkdownParser translate markdown to HTML.
    2. Phil Toland's QLMarkdown QuickLook generator for markdown files.
    0 讨论(0)
  • 2020-11-30 19:30

    I've used peg-markdown, it's much faster than the original perl and can handle a few syntax extensions if you enable them.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-30 19:34

    You may want to check out the open-source app Macdown which I wrote (or alternatively rentzsch's Markdownlive), which incorporate this functionality as the sole purpose of the two apps.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题