Skip to content

Commit

Permalink
fix: failed to save note in windows by removing invisible characters …
Browse files Browse the repository at this point in the history
…in the filename
  • Loading branch information
sywhb committed Jun 19, 2023
1 parent a349026 commit cf99fc5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
64 changes: 47 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"luxon": "^3.1.1",
"markdown-escape": "^2.0.0",
"mustache": "^4.2.0",
"out-of-character": "^1.2.1",
"process": "^0.11.10"
},
"volta": {
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/path_validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const expectedManualIllegalChars: string[] = [
"\u001F",
];

const expectedInvisibleChars: string[] = ["­", "‍"];

describe("replaceIllegalChars() removes all expected characters", () => {
test.each(expectedManualIllegalChars)(
'Illegal character "%s" is removed',
Expand Down Expand Up @@ -97,3 +99,16 @@ describe("file system behavior with non-alphanumeric characters not in the illeg
}
);
});

describe("replaceIllegalChars() function removes all occurrences of invisible characters", () => {
test.each(expectedInvisibleChars)(
"Invisible character '%s' is replaced",
(char) => {
const input = `${char}foo${char}bar`;
const expectedOutput = "foobar";
const output = replaceIllegalChars(input);
expect(output).toEqual(expectedOutput);
expect(output.match(ILLEGAL_CHAR_REGEX)).toBeNull();
}
);
});
4 changes: 3 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { diff_match_patch } from "diff-match-patch";
import { DateTime } from "luxon";
import escape from "markdown-escape";
import { parseYaml } from "obsidian";
import { replace } from "out-of-character";
import { Highlight } from "./api";

export const DATE_FORMAT_W_OUT_SECONDS = "yyyy-MM-dd'T'HH:mm";
Expand Down Expand Up @@ -91,7 +92,8 @@ export const unicodeSlug = (str: string, savedAt: string) => {
};

export const replaceIllegalChars = (str: string): string => {
return str.replace(ILLEGAL_CHAR_REGEX, REPLACEMENT_CHAR);
// remove invisible characters
return replace(str.replace(ILLEGAL_CHAR_REGEX, REPLACEMENT_CHAR));
};

export function formatDate(date: string, format: string): string {
Expand Down

0 comments on commit cf99fc5

Please sign in to comment.