我尝试使用以下解决方案:
Cocoa 应用程序最简单的 Markdown 实现是什么?
但两种解决方案都没有返回完全有效的 html,因为缺少最后一个标签(或两个?)。例如:
这个降价:
### Markdown Example - Lists
#### Unordered list
* Aenean
* vel
* libero
* eget
* ante
#### Ordered list
1. Aenean
2. vel
3. libero
4. eget
5. ante
输出以下 html:
<h3>Markdown Example - Lists</h3>
<h4>Unordered list</h4>
<ul>
<li>Aenean </li>
<li>vel
<ul>
<li>libero </li>
<li>eget </li>
</ul>
</li>
<li>ante</li>
</ul>
<h4>Ordered list</h4>
<ol>
<li>Aenean </li>
<li>vel </li>
<li>libero </li>
<li>eget </li>
<li>ante
(为清楚起见添加了缩进)。
用于生成此代码的代码取自第二个答案(尽管我也尝试了第一个,结果相同):
NSString *markdownXHTML = @"";
if([ext isEqualToString:@"markdown"] || [ext isEqualToString:@"md"]){
NSString *rawMarkdown = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
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);
markdownXHTML = [NSString stringWithUTF8String: ob->data];
bufrelease(ib);
bufrelease(ob);
}
有人有什么想法吗?:)