Markdown元素之间的转换 [英] Convert between Markdown elements

查看:114
本文介绍了Markdown元素之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有哪些选项可以解析Markdown文档并处理其元素以输出另一个Markdown文档?

What are the options to parse Markdown document and process its elements to output an another Markdown document?

让我们说吧

```
# unaffected #
```

# H1 #

H1
==

## H2 ##

H2
--

### H3 ###

应转换为

```
# unaffected #
```

## H1 ##

H1
--

### H2 ###

### H2 ###

#### H3 ####

。目标元素可能有所不同(例如####可能会转换为**)。

in Node environment. Target element may vary (e.g. #### may be converted to **).

该文档可能包含其他不受影响的标记元素。

The document may contain other markup elements that should remain unaffected.

如何获得?显然,不是使用正则表达式(使用正则表达式而不是完整的词法分析器会影响 #unaffected#)。我希望使用标记,但它似乎只能输出HTML,而不是Markdown。

How it can be obtained? Obviously, not with regexps (using regexp instead of full-blown lexer will affect # unaffected #). I was hoped to use marked but it seems that it is capable only of HTML output, not Markdown.

推荐答案

您是否考虑过将HTML用作中间格式?一旦进入HTML,标题类型之间的差异将无法区分,因此Markdown - > HTML转换将为您有效地规范化它们。有markdown - > HTML转换器,还有很多HTML - > markdown。

Have you considered using HTML as an intermediate format? Once in HTML, the differences between the header types will be indistinguishable, so the Markdown -> HTML conversion will effectively normalize them for you. There are markdown -> HTML converters aplenty, and also a number of HTML -> markdown.

我用这两个包放了一个例子:

I put together an example using these two packages:

  • https://www.npmjs.com/package/markdown-it for Markdown -> HTML
  • https://www.npmjs.com/package/h2m for HTML -> Markdown

我不喜欢不知道你是否有任何性能要求(读:这很慢......)但这是一个非常低的投资解决方案。看看:

I don't know if you have any performance requirements here (read: this is slow...) but this is a very low investment solution. Take a look:

var md = require('markdown-it')(),
    h2m = require('h2m');

var mdContent = `
\`\`\`
# unaffected #
\`\`\`

# H1 #

H1
==

## H2 ##

H2
--

### H3 ###
`;

var htmlContent = md.render(mdContent);
var newMdContent = h2m(htmlContent, {converter: 'MarkdownExtra'});
console.log(newMdContent);

您可能需要使用混合组件才能获得正确的方言支持等等。我尝试了一堆,并不能完全匹配你的输出。我想也许 - 的解释方式不同?这是输出,我会让你决定它是否足够好:

You may have to play with a mix of components to get the correct dialect support and whatnot. I tried a bunch and couldn't quite match your output. I think perhaps the -- is being interpreted differently? Here's the output, I'll let you decide if it is good enough:

```
# unaffected #

```

# H1 #

# H1 #

## H2 ##

## H2 ##

### H3 ###

这篇关于Markdown元素之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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