-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pasting URLs over markdown text
- Loading branch information
1 parent
9616dbe
commit 2644272
Showing
5 changed files
with
91 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
export function pathEscapeSegments(s) { | ||
return s.split('/').map(encodeURIComponent).join('/'); | ||
} | ||
|
||
function stripSlash(url) { | ||
return url.endsWith('/') ? url.slice(0, -1) : url; | ||
} | ||
|
||
export function isUrl(url) { | ||
try { | ||
return stripSlash((new URL(url).href)).trim() === stripSlash(url).trim(); | ||
} catch { | ||
return false; | ||
} | ||
} |
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,6 +1,13 @@ | ||
import {pathEscapeSegments} from './url.js'; | ||
import {pathEscapeSegments, isUrl} from './url.js'; | ||
|
||
test('pathEscapeSegments', () => { | ||
expect(pathEscapeSegments('a/b/c')).toEqual('a/b/c'); | ||
expect(pathEscapeSegments('a/b/ c')).toEqual('a/b/%20c'); | ||
}); | ||
|
||
test('isUrl', () => { | ||
expect(isUrl('https://example.com')).toEqual(true); | ||
expect(isUrl('https://example.com/')).toEqual(true); | ||
expect(isUrl('https://example.com/index.html')).toEqual(true); | ||
expect(isUrl('/index.html')).toEqual(false); | ||
}); |