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

fix: allow to omit region name in endregion tag in code snippets #1908

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions packages/slidev/node/syntax/transform/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, regionName: string) {
const regionRegexps = [
/^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java
/^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss
/^#pragma ((?:end)?region) ([\w*-]+)$/, // C, C++
/^<!-- #?((?:end)?region) ([\w*-]+) -->$/, // 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
[/^<!-- #?region ([\w*-]+) -->$/, /^<!-- #?region[\s\w*-]*-->$/],
// 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,
}
}
}

Expand Down
Loading