Skip to content

Commit f8d827b

Browse files
authored
FEATURE: Add support for custom icon url (#8)
* FEATURE: Add support for custom icon url * DEV: Remove unnecessary variable * FIX: ESLint error for `new` usage
1 parent 7b359e3 commit f8d827b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

common/common.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
}
2525
}
2626
}
27+
28+
.custom-header-icon-link img {
29+
width: 100%;
30+
}

javascripts/discourse/initializers/initialize-for-header-icon-links.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
import { withPluginApi } from "discourse/lib/plugin-api";
22
import { iconNode } from "discourse-common/lib/icon-library";
33
import { dasherize } from "@ember/string";
4+
import isValidUrl from "../lib/isValidUrl";
5+
import { h } from "virtual-dom";
6+
7+
function buildIcon(icon) {
8+
if (isValidUrl(icon)) {
9+
return h(
10+
"img",
11+
{
12+
attributes: {
13+
src: icon,
14+
},
15+
},
16+
""
17+
);
18+
} else {
19+
return iconNode(icon.toLowerCase());
20+
}
21+
}
422

523
export default {
624
name: "header-icon-links",
@@ -12,7 +30,7 @@ export default {
1230
splitLinks.forEach((link, index, links) => {
1331
const fragments = link.split(",").map((fragment) => fragment.trim());
1432
const title = fragments[0];
15-
const icon = iconNode(fragments[1].toLowerCase());
33+
const icon = buildIcon(fragments[1]);
1634
const href = fragments[2];
1735
const className = `header-icon-${dasherize(fragments[0])}`;
1836
const viewClass = fragments[3].toLowerCase();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function isValidUrl(string) {
2+
try {
3+
URL(string);
4+
} catch (_) {
5+
return false;
6+
}
7+
8+
return true;
9+
}

0 commit comments

Comments
 (0)