fix: improve regex for code block extraction and streamline match handling

This commit is contained in:
Harvey Zhao committed 2026-03-07 16:03:15 +08:00
1 parent 0f752260a4
commit 9cb7d6dc9f
1 file changed
+5 -9
+5 -9
View File
@@ -10,14 +10,14 @@ const excludeDirs = ['en', '.vitepress', 'public', 'plugin']
// Regex to match code blocks after "Run Code" markers
// Match until we find a closing ``` that's on its own line
const runCodePattern = /<div className="run-code">.*?<\/div>\s*\n\s*```js[^\n]*\n([\s\S]*?)\n```(?=\s*\n|$)/g
const runCodePattern = /<div className="run-code">.*?<\/div>[\t\v\f\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*\n\s*```js[^\n]*\n([\s\S]*?)\n```(?=\s*\n|$)/g
function extractCodeBlocks(content, filePath) {
const blocks = []
let match
let match = runCodePattern.exec(content)
while ((match = runCodePattern.exec(content)) !== null) {
let code = match[1].trim()
while (match !== null) {
const code = match[1].trim()
// Skip if code contains markdown syntax (unclosed code block issue)
if (code.includes('\n## ') || code.includes('\n:::') || code.includes('<div className="run-code">')) {
@@ -29,6 +29,7 @@ function extractCodeBlocks(content, filePath) {
code,
file: filePath,
})
match = runCodePattern.exec(content)
}
return blocks
@@ -98,11 +99,6 @@ function generateTestCode(fileResults) {
blocks.forEach((block, index) => {
const testName = `Example ${index + 1}`
// Escape the code for embedding in a string
const escapedCode = block.code
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\$/g, '\\$')
lines.push(` it('${testName}', function (done) {`)
lines.push(` try {`)