如何使用Node.js标记化标记?

即时通讯构build一个iOS应用程序,有一个视图,将有源自降价。

我的想法是能够将存储在MongoDB中的降价分析成如下所示的JSON对象:

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

在服务器上,我正在运行Node.js,并且正在查看标记为最受欢迎的模块。 它使我可以访问Lexer,这是标记化一些自定义对象的降价。 但是当我看着这个对象的时候,它并没有标记出这个链接。 如果我继续parsingHTML的标记,则会检测到该链接,并且HTML看起来正确。

看了一些更多的模块,失败后,我想也许我可以在客户端上做到这一点,发现MMMarkdown看起来很有前途,但是再次..当直接parsing到HTML时,工作正常,但是当介入之间,只是parsing所谓的MMDocument降价,它不包含任何types的链接的MMElement。

那么,有什么关于降价parsing的基础知识,我失踪了? 内联链接的Lexing应该是在第二轮,或什么的? 我不能让我的头在附近。

如果没有其他的工作,我可能会使用一个UIWebView充满了从parsing的降价HTML的HTML,但是我们必须再次devise整个事情,但与CSS,我们没有时间,所以我们不能重新负担得起双重工作。

你看过https://github.com/evilstreak/markdown-js吗?

它似乎让你访问语法树。

例如:

 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 ), refs = tree[ 1 ].references; 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." ] ] 

这里是我最终使用的代码。

 var nodes = markdownText.split('\r\n'); var content = []; nodes.forEach(function(node) { // Heading 2 if (node.indexOf('##') == 0) { content.push({ h2: node.replace('##','') }) } // Heading 1 else if (node.indexOf('#') == 0) { content.push({ h1: node.replace('#','') }) } // Link (Text + URL) else if (node.indexOf('[') == 0) { var matches = node.match(/\[(.*)\]\((.*)\)/); content.push({ link: { text: matches[1], url: matches[2] } }) } // Paragraph else if (node.length > 0) { content.push({ p: node }) } }); 

我知道这个匹配是非常不宽容的,但在我们的情况下,它工作正常。