Skip to content

Commit ebfcb48

Browse files
committed
escape comment chars in options regex
1 parent db1b922 commit ebfcb48

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

src/core/lib/partition-cell-options.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export function langCommentChars(lang: string): string[] {
256256
}
257257
}
258258
export function optionCommentPattern(comment: string) {
259-
return new RegExp("^" + comment + "\\s*\\| ?");
259+
return new RegExp("^" + escapeRegExp(comment) + "\\s*\\| ?");
260260
}
261261

262262
// FIXME this is an awkward spot for this particular entry point
@@ -314,3 +314,7 @@ export const kLangCommentChars: Record<string, string | [string, string]> = {
314314
dot: "//",
315315
ojs: "//",
316316
};
317+
318+
function escapeRegExp(str: string) {
319+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
320+
}

src/resources/editor/tools/vs-code.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27455,7 +27455,7 @@ function langCommentChars(lang) {
2745527455
}
2745627456
}
2745727457
function optionCommentPattern(comment) {
27458-
return new RegExp("^" + comment + "\\s*\\| ?");
27458+
return new RegExp("^" + escapeRegExp(comment) + "\\s*\\| ?");
2745927459
}
2746027460
var kLangCommentChars = {
2746127461
r: "#",
@@ -27500,6 +27500,9 @@ var kLangCommentChars = {
2750027500
dot: "//",
2750127501
ojs: "//"
2750227502
};
27503+
function escapeRegExp(str2) {
27504+
return str2.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
27505+
}
2750327506

2750427507
// ../parse-shortcode.ts
2750527508
function isBlockShortcode(content) {

src/resources/editor/tools/yaml/web-worker.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resources/jupyter/notebook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def nb_cell_yaml_options(lang, cell):
465465
def nb_cell_yaml_lines(lang, source):
466466
# determine language comment chars
467467
comment_chars = nb_language_comment_chars(lang)
468-
option_pattern = "^" + comment_chars[0] + "\\s*\\| ?"
468+
option_pattern = "^" + re.escape(comment_chars[0]) + "\\s*\\| ?"
469469
option_suffix = comment_chars[1] if len(comment_chars) > 1 else None
470470

471471
# go through the lines until we've found all of the yaml

0 commit comments

Comments
 (0)