Skip to content

Commit 3096c92

Browse files
committed
fix: escape modes improve character removal logic #14
1 parent 5814256 commit 3096c92

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/settings-tab.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ export class GitHubTrackerSettingTab extends PluginSettingTab {
285285
)
286286
.addOption(
287287
"strict",
288-
"Strict - Only alphanumeric characters and links will be allowed",
288+
"Strict - Remove potentially dangerous characters",
289289
)
290290
.addOption(
291291
"veryStrict",
292-
"Very strict - Only alphanumeric characters, and punctuation",
292+
"Very strict - Remove many special characters",
293293
)
294294
.setValue(this.plugin.settings.escapeMode)
295295
.onChange(async (value) => {

src/util/escapeUtils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
/**
22
* Utility function for escaping content in different modes
33
* @param unsafe The string to escape
4-
* @param mode The escaping mode: "disabled", "normal", or "strict"
4+
* @param mode The escaping mode: "disabled", "normal", "strict", or "veryStrict"
55
* @returns The escaped string
66
* @throws Error if input is null or undefined
7+
*
8+
* Modes:
9+
* - disabled: No escaping applied
10+
* - normal: Basic escaping for Templater and Dataview compatibility
11+
* - strict: Remove potentially dangerous HTML/JS characters (preserves Unicode)
12+
* - veryStrict: Remove more special characters (preserves Unicode but more restrictive)
713
*/
814
export function escapeBody(
915
unsafe: string,
@@ -18,15 +24,19 @@ export function escapeBody(
1824
}
1925

2026
if (mode === "strict") {
21-
// Allow alphanumeric, whitespace, common punctuation, and URL/Markdown specific characters
27+
// Allow Unicode characters, whitespace, common punctuation, and URL/Markdown specific characters
28+
// Remove potentially dangerous characters while preserving Chinese and other Unicode characters
2229
return unsafe
23-
.replace(/[^a-zA-Z0-9\s.,()\[\]*+\-:"#!'?&|*>~^\/:?=&%#_]/g, "")
24-
.replace(/---/g, "- - -");
30+
.replace(/[<>{}$`\\]/g, "") // Remove potentially dangerous HTML/JS/template characters
31+
.replace(/---/g, "- - -"); // Escape YAML frontmatter separators
2532
}
2633

2734
if (mode === "veryStrict") {
28-
// Allow alphanumeric, whitespace, basic punctuation, and essential URL/Markdown image characters
29-
return unsafe.replace(/[^a-zA-Z0-9\s.,?!\[\]():\/.\-]/g, "");
35+
// Allow Unicode characters, whitespace, basic punctuation, and essential URL/Markdown characters
36+
// More restrictive than strict mode but still preserves Chinese and other Unicode characters
37+
return unsafe
38+
.replace(/[<>{}$`\\"'|&*~^]/g, "") // Remove more potentially dangerous characters
39+
.replace(/---/g, "- - -"); // Escape YAML frontmatter separators
3040
}
3141

3242
// normal mode

0 commit comments

Comments
 (0)