forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetector-hints.ts
More file actions
62 lines (54 loc) · 2.19 KB
/
Copy pathdetector-hints.ts
File metadata and controls
62 lines (54 loc) · 2.19 KB
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
58
59
60
61
62
import type {DetectorHint} from '../definitions';
import {parseArray, formatArray} from '../utils/text';
import {compareURLPatterns} from '../utils/url';
import {formatSitesFixesConfig} from './utils/format';
import {parseSitesFixesConfig, getSitesFixesFor} from './utils/parse';
import type {SitePropsIndex, SitesFixesParserOptions} from './utils/parse';
const detectorHintsCommands: { [key: string]: keyof DetectorHint } = {
'TARGET': 'target',
'MATCH': 'match',
'NO DARK THEME': 'noDarkTheme',
'SYSTEM THEME': 'systemTheme',
};
const detectorParserOptions: SitesFixesParserOptions<DetectorHint> = {
commands: Object.keys(detectorHintsCommands),
getCommandPropName: (command) => detectorHintsCommands[command],
parseCommandValue: (command, value) => {
if (command === 'TARGET') {
return value.trim();
}
if (command === 'NO DARK THEME' || command === 'SYSTEM THEME') {
return true;
}
return parseArray(value);
},
};
export function parseDetectorHints(text: string): DetectorHint[] {
return parseSitesFixesConfig<DetectorHint>(text, detectorParserOptions);
}
export function formatDetectorHints(detectorHints: DetectorHint[]): string {
const fixes = detectorHints.slice().sort((a, b) => compareURLPatterns(a.url[0], b.url[0]));
return formatSitesFixesConfig(fixes, {
props: Object.values(detectorHintsCommands),
getPropCommandName: (prop) => Object.entries(detectorHintsCommands).find(([, p]) => p === prop)![0],
formatPropValue: (prop: keyof DetectorHint, value) => {
if (Array.isArray(value)) {
return formatArray(value).trim();
}
if (prop === 'noDarkTheme' || prop === 'systemTheme') {
return '';
}
return String(value).trim();
},
shouldIgnoreProp: (_prop, value) => {
return !value;
},
});
}
export function getDetectorHintsFor(url: string, text: string, index: SitePropsIndex<DetectorHint>): DetectorHint[] | null {
const fixes = getSitesFixesFor(url, text, index, detectorParserOptions);
if (fixes.length === 0) {
return null;
}
return fixes;
}