如何使用Node.js标记降价? [英] How to tokenize markdown using Node.js?

查看:50
本文介绍了如何使用Node.js标记降价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个iOS应用,该应用的视图将来自markdown.

Im building an iOS app that have a view that is going to have its source from markdown.

我的想法是能够将存储在MongoDB中的markdown解析为一个类似于以下内容的JSON对象:

My idea is to be able to parse markdown stored in MongoDB into a JSON-object that looks something like:

{
    "h1": "This is the heading",
    "p" : "Heres the first paragraph",
    "link": {
        "text": "Text for link",
        "url": "http://exampledomain.com",
    }
}

在服务器上,我正在运行Node.js,并正在查看模块已标记,该模块似乎是最受欢迎的模块在那里.它使我可以访问Lexer,后者将标记降价标记为某些自定义对象.但是当我查看对象时,它不会标记链接.如果我继续将markdown解析为HTML,则会检测到链接,并且HTML看起来正确.

On the server I am running Node.js, and was looking at the module marked which seem to be the most popular one out there. It gives me access to the Lexer, which is tokenizing the markdown to some custom object. But when I look at the object, it doesnt tokenize the link. If I go ahead and parse the markdown to HTML, the link is detected and the HTML looks correct.

在研究了更多模块之后,失败了,我想也许我可以在客户端上执行此操作,然后发现 MMMarkdown 似乎很有希望,但随后..在直接解析为HTML时仍然可以正常工作,但是当介入并仅解析标记为MMDocument的markdown时,它不包含任何Link类型的MMElement.

After looking into some more modules, and failing I thought that maybe I could do this on the client instead and found MMMarkdown which seemed promising, but then again .. that worked fine when parsing directly to HTML, but when stepping in between and just parsing the markdown to the so called MMDocument, it did not consist of any MMElement of type Link.

那么,我缺少Markdown解析的基础知识吗?内联链接的词汇化应该在第二轮完成吗?我无法解决这个问题.

So, is there anything fundamental about markdown parsing that I am missing? Is the lexing of the inline links supposed to be done in a second round, or something? I cant get my head around it.

如果没有其他效果,我可能会使用UIWebView来填充解析后的markdown中的HTML,但是接下来我们不得不重新设计整个过程,但是使用CSS,我们用光了时间,所以我们不能真正承担起双重工作.

If nothing else works, I might just go with using a UIWebView filled withed the HTML from the parsed markdown, but then we have to design the whole thing again, but with CSS, and we are running out of time so we cant reallt afford the double work.

推荐答案

您是否看过 https://github.com/evilstreak/markdown-js 吗?

似乎可以让您访问语法树.

It seems to give you access to the syntax tree.

例如:

var md = require( "markdown" ).markdown,
text = "Header\n---------------\n\n" +
       "This is a paragraph\n\n" +
"This is [an example](http://example.com/ \"Title\") inline link.";

// parse the markdown into a tree and grab the link references
var tree = md.parse( text );

console.log(JSON.stringify(tree));

产生

[
    "markdown",
    [
        "header",
        {
            "level": 2
        },
        "Header"
    ],
    [
        "para",
        "This is a paragraph"
    ],
    [
        "para",
        "This is ",
        [
            "link",
            {
                "href": "http://example.com/",
                "title": "Title"
            },
            "an example"
        ],
        " inline link."
    ]
]

这篇关于如何使用Node.js标记降价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆