Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

发布到知乎加粗消失 #1144

Closed
Achuan-2 opened this issue Apr 7, 2024 · 1 comment
Closed

发布到知乎加粗消失 #1144

Achuan-2 opened this issue Apr 7, 2024 · 1 comment

Comments

@Achuan-2
Copy link

Achuan-2 commented Apr 7, 2024

Clip_2024-04-07_20-09-38

Clip_2024-04-07_20-09-44

@terwer
Copy link
Owner

terwer commented Apr 9, 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants