generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from decaf-dev/dev
1.2.1
- Loading branch information
Showing
12 changed files
with
80 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
/** | ||
* Sanitizes a file name for use in a file system | ||
*/ | ||
export const sanitizeFileName = (name: string) => { | ||
// Replace colon with hyphen | ||
name = name.replace(/:/g, "-"); | ||
// Replace back slash with space | ||
name = name.replace(/\\/g, " "); | ||
// Replace forward slash with space | ||
name = name.replace(/\//g, " "); | ||
// Replace carrot with nothing | ||
name = name.replace(/\^/g, ""); | ||
// Replace left bracket with nothing | ||
name = name.replace(/\[/g, ""); | ||
// Replace right bracket with nothing | ||
name = name.replace(/\]/g, ""); | ||
// Replace hash tag with nothing | ||
name = name.replace(/#/g, ""); | ||
// Replace pipe with nothing | ||
name = name.replace(/\|/g, ""); | ||
return name.trim(); | ||
export const sanitizeFileName = (name: string, isWindows: boolean) => { | ||
if (isWindows) { | ||
name = name.replace(/\?/g, ""); // Remove question mark (Windows) | ||
name = name.replace(/</g, ""); // Remove less than (Windows) | ||
name = name.replace(/>/g, ""); // Remove greater than (Windows) | ||
name = name.replace(/"/g, ""); // Remove double quote (Windows) | ||
name = name.replace(/\*/g, ""); // Remove asterisk (Windows) | ||
name = name.replace(/`/g, ""); // Remove backtick (Windows) | ||
} | ||
|
||
name = name.replace(/:/g, ""); // Remove colon (Windows, macOS) | ||
name = name.replace(/\|/g, ""); // Remove pipe (Windows) | ||
name = name.replace(/\\/g, ""); // Remove backslash (Windows, general cross-platform safety) | ||
name = name.replace(/\//g, ""); // Remove forward slash (Windows, Linux, macOS) | ||
name = name.replace(/\[/g, ""); // Remove left bracket (General safety, no specific OS restriction) | ||
name = name.replace(/\]/g, ""); // Remove right bracket (General safety, no specific OS restriction) | ||
name = name.replace(/#/g, ""); // Remove hashtag (General safety, no specific OS restriction) | ||
name = name.replace(/\^/g, ""); // Remove caret (General safety, no specific OS restriction) | ||
return name.trim(); // Trim whitespace from start and end (General cleanup) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
/** | ||
* Truncates the string to the maximum length allowed for a file name | ||
*/ | ||
export const truncateFileName = (name: string) => { | ||
export const truncateFileName = (name: string, extension: string) => { | ||
const MAX_LENGTH = 255; | ||
|
||
const splitArr = name.split("."); | ||
if (splitArr.length < 2) { | ||
throw new Error("Invalid file name"); | ||
//Add support for links | ||
let length = name.length; | ||
if (length > MAX_LENGTH) { | ||
length = MAX_LENGTH; | ||
} | ||
|
||
const baseName = splitArr[0]; | ||
const extension = splitArr[1]; | ||
return baseName.substring(0, MAX_LENGTH - extension.length - 1) + "." + splitArr[1]; | ||
return name.substring(0, length - extension.length) + extension; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import { truncateFileName } from "src/splitter/truncate-file-name"; | ||
|
||
describe("truncateFileName", () => { | ||
it("should throw an error if the file name is invalid", () => { | ||
it("should truncate the string to 255 characters", () => { | ||
// Arrange | ||
const fileName = "file"; | ||
const fileName = "a".repeat(260) + ".md"; | ||
|
||
// Act | ||
const action = () => truncateFileName(fileName); | ||
const result = truncateFileName(fileName, ".md"); | ||
|
||
// Assert | ||
expect(action).toThrow(); | ||
expect(result.length).toEqual(255); | ||
expect(result.endsWith(".md")).toEqual(true); | ||
}); | ||
|
||
it("should truncate the string to 255 characters", () => { | ||
it("should not modify a link shorter than 255 characters", () => { | ||
// Arrange | ||
const fileName = "a".repeat(260) + ".md"; | ||
const fileName = "https://example.com.md"; | ||
|
||
// Act | ||
const result = truncateFileName(fileName); | ||
const result = truncateFileName(fileName, ".md"); | ||
|
||
// Assert | ||
expect(result.length).toEqual(255); | ||
expect(result.endsWith(".md")).toEqual(true); | ||
expect(result).toEqual(fileName); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters