How to parse ordered list of wikitext to HTML usin

2019-04-17 17:15发布

问题:

I need to parse ordered list of wikitext to html using wiky.js. This javascript is mainly using regex to do that.

E.g.

# Item1
# Item2
# Item3
# Item4
## Sub-item 1
### Sub-sub-item

is displayed as

1.Item1
2.Item2
3.Item3
4.Item4
    1.Sub-item 1
       1. Sub-sub-item

I need to get the HTML version of the code. Currently wiky.js uses the old version of parsing ordered list which is not supported by Wiki Editor now.

回答1:

Add the following:

{
    rex: /^((#*)[^#].*(\n))(?=#\2)/gm,
    tmplt: "$1<ol>$3"
},
{
    rex: /^((#+).*(\n))(?!\2|<ol)/gm,
    tmplt: "$1</ol>$2.$2$3"
},
{
    rex: /#(?=(#+)\.#+\n(?!\1))/gm,
    tmplt: "</ol>"
},
{
    rex: /(<\/ol>)[#.]+/gm,
    tmplt: "$1"
},
{
    rex: /^((#+).*(\n))(?=\2[^#]|<\/ol)/gm,
    tmplt: "$1</li>$3"
},
{
    rex: /^(<\/ol>(\n)*)#+/gm,
    tmplt: "$1</li>$2<li>"
},
{
    rex: /^#+/gm,
    tmplt: "<li>"
}

Hoping they are executed in this order. This will cover a potentially infinite recursion level of <ol><li></li></ol> tags.

I don't explain you the code 'cause I've used some dirty expedients whose logic it's hard to unfold.