joethei/obsidian-link-favicon

Support regex in ignored domains, custom domains and possibly schemes

Closed this issue · 1 comments

This would be nice, for example handling all possible medium.com subdomains in one rule, not one-by-one when I encounter them.
I'm not sure if you'd want it for schemes - but it's nice for consistency, and they might come in handy, for example handling all ms- ones in one rule.

I considered making a pull request, but I had some lint and build errors, and I'm not sure if this feature is welcome at all. Here's the my code and the errors.

main.ts
	getCustomDomainIcon(domain: string): string | HTMLImageElement {
		if (isPluginEnabled(this)) {
			const icons = this.settings.overwritten.filter(value => domain.match(new RegExp(value.domain)));
			if (icons.length > 0) {
				const iconApi = getApi(this);
				const icon = icons[0].icon;
				return iconApi.getIcon(icon, false);
			}
		}
	}

	getCustomSchemeIcon(scheme: string): string | HTMLImageElement {
		if (isPluginEnabled(this)) {
			const icons = this.settings.protocol.filter(value => scheme.substring(0, scheme.length - 1).match(new RegExp(value.domain)));
			if (icons.length > 0) {
				const iconApi = getApi(this);
				const icon = icons[0].icon;
				return iconApi.getIcon(icon, false);
			}
		}
	}

	async getIcon(domain: URL, provider: IconProvider): Promise<string | HTMLImageElement> {
		//custom protocols
		if (!domain.protocol.startsWith("http")) {
			const customSchemeIcon = this.getCustomSchemeIcon(domain.protocol);
			if (customSchemeIcon) {
				if (typeof customSchemeIcon !== "string") {
					customSchemeIcon.addClass("link-favicon");
					customSchemeIcon.dataset.target = domain.href;
					customSchemeIcon.dataset.protocol = domain.protocol;
				}
				return customSchemeIcon;
			}
			return null;
		}

		if (this.settings.ignored.split("\n")
			.some(value => domain.hostname.match(new RegExp(value)))) {
			return null;
		}
errors

I had this build error on npm run build:

 > src/OverwrittenIconModal.ts:55:66: error: Could not resolve "../schemas.json"
    55 │       let schemas: {schema: string, Description: string}[] = require("../schemas.json");

And these lint errors in main.ts in VSCode from a clean clone after npm i:

File '.../node_modules/fast-average-color/dist/index.d.ts' is not a module. - ts(2306) [5, 30]
Parameter 'color' implicitly has an 'any' type. - ts(7006) [134, 36]
Parameter 'e' implicitly has an 'any' type. - ts(7006) [145, 12]

Also, isPluginEnabled is deprecated.

The error of the schemas.json file missing can be resolved by running commands 2-4 from the
build step of the publish GH Action.

Not sure why VS Code is complaining about that code, WebStorm does not do that for me.

That being said, I have integrated this FR into the plugin, will close this issue once released.
I am currently in the middle of refactoring some of the code, so I would not recommend creating a PR at this time anyway.