forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss-vars.js
36 lines (30 loc) · 858 Bytes
/
css-vars.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
import * as dom from '../dom';
import { get } from '../../fetch/ajax';
function replaceVar(block, color) {
block.innerHTML = block.innerHTML.replace(
/var\(\s*--theme-color.*?\)/g,
color
);
}
export default function(color) {
// Variable support
if (window.CSS && window.CSS.supports && window.CSS.supports('(--v:red)')) {
return;
}
const styleBlocks = dom.findAll('style:not(.inserted),link');
[].forEach.call(styleBlocks, block => {
if (block.nodeName === 'STYLE') {
replaceVar(block, color);
} else if (block.nodeName === 'LINK') {
const href = block.getAttribute('href');
if (!/\.css$/.test(href)) {
return;
}
get(href).then(res => {
const style = dom.create('style', res);
dom.head.appendChild(style);
replaceVar(style, color);
});
}
});
}