forked from type-challenges/type-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoUrl.ts
85 lines (71 loc) · 3.16 KB
/
toUrl.ts
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
import lzs from 'lz-string'
import type { Quiz } from './types'
import { defaultLocale } from './locales'
import { resolveInfo } from './loader'
export const REPO = 'https://github.com/type-challenges/type-challenges'
export const DOMAIN = 'https://tsch.js.org'
export const TYPESCRIPT_PLAYGROUND = 'https://www.typescriptlang.org/play'
// https://github.com/microsoft/TypeScript-Website/tree/v2/packages/playground
export function toPlaygroundUrl(
code: string,
config: Object = {},
site = TYPESCRIPT_PLAYGROUND,
) {
return `${site}?${Object.entries(config).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')}#code/${lzs.compressToEncodedURIComponent(code)}`
}
export function toSolutionsFull(no: number) {
return `${REPO}/issues?q=label%3A${no}+label%3Aanswer`
}
export function toQuizREADME(quiz: Quiz, locale?: string, absolute = false) {
const prefix = absolute ? `${REPO}/blob/main` : '.'
return locale && locale !== defaultLocale && quiz.readme[locale]
? `${prefix}/questions/${quiz.path}/README.${locale}.md`
: `${prefix}/questions/${quiz.path}/README.md`
}
export function toRawREADME(quiz: Quiz, locale?: string) {
const provider = 'https://cdn.jsdelivr.net/gh/type-challenges/type-challenges'
// const provider = 'https://raw.githubusercontent.com/type-challenges/type-challenges/main'
return locale && locale !== defaultLocale && quiz.readme[locale]
? `${provider}/questions/${quiz.path}/README.${locale}.md`
: `${provider}/questions/${quiz.path}/README.md`
}
export function toQuestionsRawREADME(locale?: string) {
const provider = 'https://cdn.jsdelivr.net/gh/type-challenges/type-challenges'
return locale && locale !== defaultLocale ? `${provider}/README.${locale}.md` : `${provider}/README.md`
}
export function toNearborREADME(quiz: Quiz, locale?: string) {
return locale && locale !== defaultLocale && quiz.readme[locale]
? `./README.${locale}.md`
: './README.md'
}
export function toShareAnswerFull(quiz: Quiz, locale: string = defaultLocale) {
const info = resolveInfo(quiz, locale)
if (locale === defaultLocale)
return `https://github.com/type-challenges/type-challenges/issues/new?labels=answer,${encodeURIComponent(locale)}&template=0-answer.md&title=${encodeURIComponent(`${quiz.no} - ${info.title}`)}`
else
return `https://github.com/type-challenges/type-challenges/issues/new?labels=answer,${encodeURIComponent(locale)}&template=1-answer.${locale}.md&title=${encodeURIComponent(`${quiz.no} - ${info.title}`)}`
}
// Short
export function toReadmeShort(no: number, locale?: string) {
return locale !== defaultLocale
? `${DOMAIN}/${no}/${locale}`
: `${DOMAIN}/${no}`
}
export function toSolutionsShort(no: number) {
return `${DOMAIN}/${no}/solutions`
}
export function toPlayShort(no: number, locale?: string) {
return locale !== defaultLocale
? `${DOMAIN}/${no}/play/${locale}`
: `${DOMAIN}/${no}/play`
}
export function toAnswerShort(no: number, locale?: string) {
return locale !== defaultLocale
? `${DOMAIN}/${no}/answer/${locale}`
: `${DOMAIN}/${no}/answer`
}
export function toHomepageShort(locale?: string) {
return locale !== defaultLocale
? `${DOMAIN}/${locale}`
: `${DOMAIN}`
}