Node.js - 将 markdown 字符串(仅粗体和斜体)解析为文本 + 样式 json 数组

nir*_*sky 4 javascript regex markdown

尝试解析 JavaScript 中包含粗体注释(标记为*multi word bold*(星号))和斜体注释(标记为_multi word italic_(下划线))的字符串。
我希望解析器函数支持多词注释、粗体中斜体、斜体中粗体和混合(参见示例)。

以下是一些具有所需输出的输入示例:

const simpleInput = "The *quick brown fox* jumps _over the lazy dog_";
const simpleOutput =[
    {text: 'The '},
    {text: 'quick brown fox', bold: true},
    {text: ' jumps '},
    {text: 'over the lazy dog', italic: true}
];

const italicWithinBoldInput = "The *quick brown _fox jumps_ over the* lazy dog";
const italicWithinBoldOutput =[
    {text: 'The '},
    {text: 'quick brown ', bold: true},
    {text: 'fox jumps', bold: true, italic: true},
    {text: ' over the', bold: true},
    {text: ' lazy dog'}
];

const mixedInput = "The *quick brown _fox jumps* over the_ lazy dog";
const mixedOutput =[
    {text: 'The '},
    {text: 'quick brown ', bold: true},
    {text: 'fox jumps', bold: true, italic: true},
    {text: ' over the', italic: true},
    {text: ' lazy dog'}
];
Run Code Online (Sandbox Code Playgroud)

我在 npm 上尝试了一些解析器,但一切都有点矫枉过正,没有什么是我所需要的。

mar*_*308 5

您可以通过以下方式进行

function getObject(str){
    let bold = false, italics = false;
    let output = [];
    let text = str.split('').reduce((a, b) => {
        if(b == '*'){
            if(bold){
                if(a != ''){
                    if(italics)
                        output.push({text: a, bold: true, italics:true});
                    else
                      output.push({text: a, bold: true});
                }
                bold = false;
            }
            else{
                if(italics)
                    output.push({text: a, italics: true})
                else
                    output.push({text: a})
                bold = true;
            }
            return '';
        }
        else if(b == '_'){
            if(italics){
                if(a != ''){
                    if(bold)
                        output.push({text: a, bold: true, italics:true});
                    else
                      output.push({text: a, italics: true});
                }
                italics = false;
            }
            else{
                if(bold)
                    output.push({text: a, bold: true})
                else
                    output.push({text: a})
                italics = true;
            }
            return '';
        }
        else{
            return a+b;
        }
    }, '');
    if(text != '')
        output.push({text : text});
    console.log(output);
    return output;
}


const simpleInput = "The *quick brown fox* jumps _over the lazy dog_";

getObject(simpleInput);

const italicWithinBoldInput = "The *quick brown _fox jumps_ over the* lazy dog";

getObject(italicWithinBoldInput);

const mixedInput = "The *quick brown _fox jumps* over the_ lazy dog";

getObject(mixedInput);
Run Code Online (Sandbox Code Playgroud)

  • 我想知道这是否有最坏情况的二次行为?看起来,如果您传入一个不带星号或下划线的字符串,该函数会将输入连接到一个空字符串,一次一个字符。我不知道 JS 字符串连接在幕后是否做了一些聪明的事情,避免了这个用例的 O(n^2) 复杂性。 (2认同)