Skip to content

Commit d5aa687

Browse files
authoredOct 26, 2024··
fix: allow to omit region name in endregion tag in code snippets (#1908)
1 parent 06e0f0d commit d5aa687

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed
 

Diff for: ‎packages/slidev/node/syntax/transform/snippet.ts

+26-30
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,44 @@ function dedent(text: string): string {
2424
return text
2525
}
2626

27-
function testLine(
28-
line: string,
29-
regexp: RegExp,
30-
regionName: string,
31-
end: boolean = false,
32-
) {
33-
const [full, tag, name] = regexp.exec(line.trim()) || []
34-
35-
return (
36-
full
37-
&& tag
38-
&& name === regionName
39-
&& tag.match(end ? /^[Ee]nd ?[rR]egion$/ : /^[rR]egion$/)
40-
)
41-
}
42-
4327
function findRegion(lines: Array<string>, regionName: string) {
4428
const regionRegexps = [
45-
/^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java
46-
/^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss
47-
/^#pragma ((?:end)?region) ([\w*-]+)$/, // C, C++
48-
/^<!-- #?((?:end)?region) ([\w*-]+) -->$/, // HTML, markdown
49-
/^#(End Region) ([\w*-]+)$/, // Visual Basic
50-
/^::#(endregion) ([\w*-]+)$/, // Bat
51-
/^# ?((?:end)?region) ([\w*-]+)$/, // C#, PHP, Powershell, Python, perl & misc
29+
// javascript, typescript, java
30+
[/^\/\/ ?#?region ([\w*-]+)$/, /^\/\/ ?#?endregion/],
31+
// css, less, scss
32+
[/^\/\* ?#region ([\w*-]+) ?\*\/$/, /^\/\* ?#endregion[\s\w*-]*\*\/$/],
33+
// C, C++
34+
[/^#pragma region ([\w*-]+)$/, /^#pragma endregion/],
35+
// HTML, markdown
36+
[/^<!-- #?region ([\w*-]+) -->$/, /^<!-- #?region[\s\w*-]*-->$/],
37+
// Visual Basic
38+
[/^#Region ([\w*-]+)$/, /^#End Region/],
39+
// Bat
40+
[/^::#region ([\w*-]+)$/, /^::#endregion/],
41+
// C#, PHP, Powershell, Python, perl & misc
42+
[/^# ?region ([\w*-]+)$/, /^# ?endregion/],
5243
]
5344

54-
let regexp = null
45+
let endReg = null
5546
let start = -1
5647

5748
for (const [lineId, line] of lines.entries()) {
58-
if (regexp === null) {
59-
for (const reg of regionRegexps) {
60-
if (testLine(line, reg, regionName)) {
49+
if (endReg === null) {
50+
for (const [startReg, end] of regionRegexps) {
51+
const match = line.trim().match(startReg)
52+
if (match && match[1] === regionName) {
6153
start = lineId + 1
62-
regexp = reg
54+
endReg = end
6355
break
6456
}
6557
}
6658
}
67-
else if (testLine(line, regexp, regionName, true)) {
68-
return { start, end: lineId, regexp }
59+
else if (endReg.test(line.trim())) {
60+
return {
61+
start,
62+
end: lineId,
63+
regexp: endReg,
64+
}
6965
}
7066
}
7167

0 commit comments

Comments
 (0)
Please sign in to comment.