From dee71bda4022c18430fbe8bd66b884ec0170c282 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Fri, 25 Oct 2024 15:32:27 +0800 Subject: [PATCH] fix: allow to omit region name in endregion tag in code snippets --- .../slidev/node/syntax/transform/snippet.ts | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/packages/slidev/node/syntax/transform/snippet.ts b/packages/slidev/node/syntax/transform/snippet.ts index c832f42522..2e10d9fb61 100644 --- a/packages/slidev/node/syntax/transform/snippet.ts +++ b/packages/slidev/node/syntax/transform/snippet.ts @@ -24,48 +24,44 @@ function dedent(text: string): string { return text } -function testLine( - line: string, - regexp: RegExp, - regionName: string, - end: boolean = false, -) { - const [full, tag, name] = regexp.exec(line.trim()) || [] - - return ( - full - && tag - && name === regionName - && tag.match(end ? /^[Ee]nd ?[rR]egion$/ : /^[rR]egion$/) - ) -} - function findRegion(lines: Array, regionName: string) { const regionRegexps = [ - /^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java - /^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss - /^#pragma ((?:end)?region) ([\w*-]+)$/, // C, C++ - /^$/, // HTML, markdown - /^#(End Region) ([\w*-]+)$/, // Visual Basic - /^::#(endregion) ([\w*-]+)$/, // Bat - /^# ?((?:end)?region) ([\w*-]+)$/, // C#, PHP, Powershell, Python, perl & misc + // javascript, typescript, java + [/^\/\/ ?#?region ([\w*-]+)$/, /^\/\/ ?#?endregion/], + // css, less, scss + [/^\/\* ?#region ([\w*-]+) ?\*\/$/, /^\/\* ?#endregion[\s\w*-]*\*\/$/], + // C, C++ + [/^#pragma region ([\w*-]+)$/, /^#pragma endregion/], + // HTML, markdown + [/^$/, /^$/], + // Visual Basic + [/^#Region ([\w*-]+)$/, /^#End Region/], + // Bat + [/^::#region ([\w*-]+)$/, /^::#endregion/], + // C#, PHP, Powershell, Python, perl & misc + [/^# ?region ([\w*-]+)$/, /^# ?endregion/], ] - let regexp = null + let endReg = null let start = -1 for (const [lineId, line] of lines.entries()) { - if (regexp === null) { - for (const reg of regionRegexps) { - if (testLine(line, reg, regionName)) { + if (endReg === null) { + for (const [startReg, end] of regionRegexps) { + const match = line.trim().match(startReg) + if (match && match[1] === regionName) { start = lineId + 1 - regexp = reg + endReg = end break } } } - else if (testLine(line, regexp, regionName, true)) { - return { start, end: lineId, regexp } + else if (endReg.test(line.trim())) { + return { + start, + end: lineId, + regexp: endReg, + } } }