Skip to content

Commit

Permalink
feat: update isUrl method to utilize browser URL API
Browse files Browse the repository at this point in the history
now regex option acts as a fallback option to the native URL validation

Closes #3
  • Loading branch information
aidenlx committed Apr 20, 2021
1 parent 2622a00 commit 1254e5b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ export default class UrlIntoSelection extends Plugin {

private isUrl(text: string): boolean {
let urlRegex = new RegExp(this.settings.regex);
return urlRegex.test(text);

try {
// throw TypeError: Invaild URL if not vaild
new URL(text);
return true;
} catch (error) {
// fallback test allows url without protocol (http,file...)
return urlRegex.test(text);
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"rollup": "^2.32.1",
"standard-version": "^9.0.0",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
"typescript": "^4.0.3",
"url-parse": "^1.5.1"
}
}
4 changes: 2 additions & 2 deletions setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class UrlIntoSelectionSettingsTab extends PluginSettingTab {
containerEl.createEl("h2", { text: "URL-into-selection Settings" });

new Setting(containerEl)
.setName("Regular expression")
.setDesc("Regular expression used to match URLs in the clipboard.")
.setName("Fallback Regular expression")
.setDesc("Regular expression used to match URLs when default match fails.")
.addText((text) =>
text
.setPlaceholder("Enter regular expression here..")
Expand Down

0 comments on commit 1254e5b

Please sign in to comment.