forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert file URLs to Markdown links while pasting
- Loading branch information
Kamil Łopuszański
committed
May 10, 2024
1 parent
36c25fd
commit 124bcae
Showing
3 changed files
with
68 additions
and
0 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
40 changes: 40 additions & 0 deletions
40
packages/app-desktop/gui/NoteEditor/utils/textPasteEnhancer.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import enhancePastedText from './textPasteEnhancer'; | ||
|
|
||
| describe('enhancePastedText', () => { | ||
| it('should replace spaces with "%20" in file paths', () => { | ||
| const input = 'This is a file path: file:///Users/john/Documents/file with spaces.txt'; | ||
| const expectedOutput = 'This is a file path: file:///Users/john/Documents/file%20with%20spaces.txt'; | ||
| const output = enhancePastedText(input); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it('should not modify non-file paths', () => { | ||
| const input = 'This is not a file path: https://example.com/file.txt'; | ||
| const expectedOutput = 'This is not a file path: https://example.com/file.txt'; | ||
| const output = enhancePastedText(input); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it('should modify multiple file URLs', () => { | ||
| const input = 'This is a file path: file:///Users/john/Documents/file with spaces.txt\nThis is another file path: file:///Users/john/Documents/file with spaces.txt'; | ||
| const expectedOutput = 'This is a file path: file:///Users/john/Documents/file%20with%20spaces.txt\nThis is another file path: file:///Users/john/Documents/file%20with%20spaces.txt'; | ||
| const output = enhancePastedText(input); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| describe('toMarkdownLink is true', () => { | ||
| it('should convert to markdown link', () => { | ||
| const input = 'This is a file path: file:///Users/john/Documents/file with spaces.txt'; | ||
| const expectedOutput = 'This is a file path: [file with spaces.txt](file:///Users/john/Documents/file%20with%20spaces.txt)'; | ||
| const output = enhancePastedText(input, true); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it('should convert images to markdown image links', () => { | ||
| const input = 'This is a file path: file:///Users/john/Documents/file with spaces.png'; | ||
| const expectedOutput = 'This is a file path: '; | ||
| const output = enhancePastedText(input, true); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
| }); | ||
| }); |
25 changes: 25 additions & 0 deletions
25
packages/app-desktop/gui/NoteEditor/utils/textPasteEnhancer.ts
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| function enhancePastedText(text: string, toMarkdownLink = false): string { | ||
| const FILE_PATH_REGEX = /file:\/\/(\/([\w.]+ ?)+)+/g; | ||
|
|
||
| return text.replace(FILE_PATH_REGEX, (match) => { | ||
| const fileName = extractFileName(match); | ||
| const enhancedText = match.replace(/\s/g, '%20'); | ||
| if (toMarkdownLink) { | ||
| return isImage(fileName) ? `` : `[${fileName}](${enhancedText})`; | ||
| } | ||
| return enhancedText; | ||
| }); | ||
| } | ||
|
|
||
| function extractFileName(filePath: string): string { | ||
| return filePath.split('/').pop(); | ||
| } | ||
|
|
||
| function isImage(fileName: string): boolean { | ||
| const IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'tiff', 'avif', 'pjp']; | ||
|
|
||
| const extension = fileName.split('.').pop().toLowerCase(); | ||
| return IMAGE_EXTENSIONS.includes(extension); | ||
| } | ||
|
|
||
| export default enhancePastedText; |