This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
material-icons-docsify.js
57 lines (48 loc) · 1.66 KB
/
material-icons-docsify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(function () {
"use strict";
function install(hook) {
const miRegExp = /:mi(-[\w-]+)?(?:\s+([\w-]+|#[0-9A-Fa-f]{3,6}|rgb\([^)]+\)))?(?:\s+([\w-]+))?:/g;
function isValidColor(color) {
if (!color) return false;
const s = new Option().style;
s.color = color;
return s.color !== '';
}
function replaceIconSyntax(content) {
return content.replace(miRegExp, (match, iconType, param1, param2) => {
let className = 'material-icons';
let iconName = '';
let colorStyle = '';
if (iconType) className += iconType;
if (param1 && param2) {
if (isValidColor(param1)) {
colorStyle = `color: ${param1};`;
iconName = param2;
} else {
iconName = param2;
console.warn(`Invalid color "${param1}" in "${match}". Using default color.`);
}
} else if (param1) {
if (isValidColor(param1)) {
colorStyle = `color: ${param1};`;
} else {
iconName = param1;
}
}
if (!iconName) {
iconName = param2 || param1 || '';
}
const styleAttribute = colorStyle ? `style="${colorStyle}"` : '';
return `<i class="${className}" ${styleAttribute}>${iconName}</i>`;
});
}
hook.beforeEach(content => replaceIconSyntax(content));
hook.afterEach((html, next) => next(replaceIconSyntax(html)));
}
// Set up as a Docsify plugin
if (window.$docsify) {
$docsify.plugins = [].concat(install, $docsify.plugins || []);
} else {
console.error("Docsify instance not found. Make sure Docsify is loaded before this plugin.");
}
})();