Convert between Markdown elements

后端 未结 5 500
借酒劲吻你
借酒劲吻你 2021-01-11 19:52

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

Let\'s say it

```
# unaffected #
``         


        
5条回答
  •  再見小時候
    2021-01-11 20:40

    You must use regexps. marked itself use Regexp for parsing the document. Why don't you?

    This is some of the regexp you need, from marked.js source code on github:

    var block = {
      newline: /^\n+/,
      code: /^( {4}[^\n]+\n*)+/,
      fences: noop,
      hr: /^( *[-*_]){3,} *(?:\n+|$)/,
      heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
      nptable: noop,
      lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
      blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
      list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
      html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
      def: /^ *\[([^\]]+)\]: *]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
      table: noop,
      paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
      text: /^[^\n]+/
    };
    

    If you really really don't want to use regexp, you can fork the marked object. and overide the Renderer object.

    Marked on github is splited to two components. One for parsing and one for render. You can eaisly change the render to your own render. (compiler)

    Example of one function in Render.js:

    Renderer.prototype.blockquote = function(quote) {
      return '
    \n' + quote + '
    \n'; };)

提交回复
热议问题