Giter Club home page Giter Club logo

Comments (1)

terwer avatar terwer commented on May 29, 2024
function replaceNonCodeBlocks(text, sign, className, style) {
    const regex = new RegExp("``[^`]*``|" + sign + "[^" + sign + "]*?" + sign + "|`[^`]*`", 'g');
    let inCodeBlock = false;
    let result = '';
    let lastIndex = 0;

    text.replace(regex, (match, index) => {
        if (match.startsWith("``")) {
            inCodeBlock = !inCodeBlock;
        } else if (!inCodeBlock && (match.startsWith(sign) || match.startsWith("`"))) {
            result += text.slice(lastIndex, index) + `<span class="${className}" style="${style}">${match.slice(2, -2)}</span>`;
            lastIndex = index + match.length;
        }
    });

    result += text.slice(lastIndex);
    return result;
}

const text = `
这是一个==忽略代码块==。里面有==另一个==忽略代码块==。==最里面的==忽略代码块==。
这个是\`\`\`代码块==不应该被匹配==。

\`哈哈哈哈==这个是单行代码不需要匹配==对对对\`

这是一个包含代码块的段落:
\`\`\`
function test() {
    console.log("这个是代码块内容");
} 
==这也也不应该匹配==
\`\`\`
这个是一个包含==内联代码==的例子。
`;

const replacedText = replaceNonCodeBlocks(text, "=", "mark", "color: red;");
console.log(replacedText);

上面是新的测试用例,已修复,需更新 1.21.0

from siyuan-plugin-publisher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.