-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathIndex.svelte
More file actions
114 lines (102 loc) · 2.47 KB
/
Index.svelte
File metadata and controls
114 lines (102 loc) · 2.47 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<script>
import parseUrl from './parse-url.js';
import * as urlDetection from '../index';
import { getAllUrls, testableUrls } from '../collector';
const defaultUrl = 'https://github.com/refined-github/github-url-detection';
const urlParameter = new URLSearchParams(location.search);
const parsedUrlParameter = parseUrl(urlParameter.get('url'), 'https://github.com').href;
// Parse partial URL in the parameter so that it's shown as full URL in the field
let urlField = parsedUrlParameter || '';
const allUrls = [...getAllUrls()].sort();
let parsedUrl;
// Do not use ?? because it should work on empty strings
$: parsedUrl = parseUrl(urlField || defaultUrl);
let detections = [];
$: {
if (parsedUrl) {
if (urlField) {
urlParameter.set('url', urlField.replace('https://github.com', ''));
history.replaceState(null, '', `?${urlParameter}`);
} else {
history.replaceState(null, '', location.pathname);
}
detections = Object.entries(urlDetection)
.map(([name, detect]) => {
if (typeof detect !== 'function') {
return;
}
if (!String(detect).startsWith('()')) {
return {
name,
detect,
result: detect(parsedUrl)
};
} else {
return {name};
}
})
.filter(Boolean)
.sort((a, b) => {
// Pull true values to the top
if (a.result || b.result) {
return a.result ? b.result ? 0 : -1 : 1;
}
// Push false values to the top
if (a.detect || b.detect) {
return a.detect ? b.detect ? 0 : 1 : -1;
}
// DOM-based detections should be in the middle
});
}
}
</script>
<style>
input {
width: 100%;
}
.true {
font-weight: bold;
}
.true span {
color: #22863a;
}
.false span {
color: #ff3e00;
}
.undefined {
color: gray;
}
pre a {
color: inherit;
}
</style>
<label>
<span>URL:</span>
<input
type="search"
bind:value={urlField}
placeholder={defaultUrl}
autocomplete="off"
autocorrect="off"
list="url-examples" />
</label>
<datalist id="url-examples">
{#each [...allUrls] as url}
<option value={url}></option>
{/each}
</datalist>
{#if parsedUrl}
<pre><code>
{#each detections as {name, detect, result} (name)}
{#if detect}
<div class={String(result)}>
<a href="detections.html#{name}">{name}</a>(url) // <span>{String(result)}</span></div>
{:else}
<div class="undefined">
{name}() // undeterminable via URL</div>
{/if}
{/each}
</code></pre>
{:else}
<p>URL entered isn’t valid</p>
{/if}