Improve version and article linking

This commit is contained in:
Misode
2022-05-12 16:58:10 +02:00
parent ad13cc131b
commit e2bcf2d1a7
7 changed files with 90 additions and 62 deletions

View File

@@ -3,18 +3,13 @@ import { isObject } from '../Utils'
const repo = 'https://raw.githubusercontent.com/misode/technical-changes/main'
export type Change = {
group: ChangelogVersion,
version: ChangelogVersion,
group: string,
version: string,
order: number,
tags: string[],
content: string,
}
export type ChangelogVersion = {
id: string,
article: string | null,
}
let Changelogs: Change[] | Promise<Change[]> | null = null
export async function getChangelogs() {
@@ -24,21 +19,21 @@ export async function getChangelogs() {
index.map((group, i) => fetchGroup(parseVersion(group), i))
)).flat().map<Change>(change => ({
...change,
tags: [change.group.id, ...change.tags],
tags: [change.group, ...change.tags],
}))
}
return Changelogs
}
async function fetchGroup(group: ChangelogVersion, groupIndex: number) {
const index = await (await fetch(`${repo}/${group.id}/index.json`)).json() as string[]
async function fetchGroup(group: string, groupIndex: number) {
const index = await (await fetch(`${repo}/${group}/index.json`)).json() as string[]
return (await Promise.all(
index.map((version, i) => fetchChangelog(group, parseVersion(version), groupIndex, i))
)).flat()
}
async function fetchChangelog(group: ChangelogVersion, version: ChangelogVersion, groupIndex: number, versionIndex: number) {
const text = await (await fetch(`${repo}/${group.id}/${version.id}.md`)).text()
async function fetchChangelog(group: string, version: string, groupIndex: number, versionIndex: number) {
const text = await (await fetch(`${repo}/${group}/${version}.md`)).text()
return parseChangelog(text).map(change => ({
version,
group,
@@ -60,14 +55,32 @@ function parseChangelog(text: string) {
})
}
function parseVersion(version: unknown): ChangelogVersion {
function parseVersion(version: unknown): string {
if (typeof version === 'string') {
return {
id: version,
article: version.match(/\d\dw\d\d[a-z]/) ? 'minecraft-snapshot-' + version : version.match(/\d+\.\d+(\.\d+)?-pre[0-9]+/) ? 'minecraft-' + version.replaceAll('.', '-').replaceAll('pre', 'pre-release-') : null,
}
return version
} else if (isObject(version)) {
return version as ChangelogVersion
return version.id
}
return { id: 'unknown', article: null }
return 'unknown'
}
const ARTICLE_PREFIX = 'https://www.minecraft.net/article/'
const ARTICLE_OVERRIDES = new Map(Object.entries({
1.17: 'caves---cliffs--part-i-out-today-java',
1.18: 'caves---cliffs--part-ii-out-today-java',
'1.18.2': 'minecraft-java-edition-1-18-2',
}))
export function getArticleLink(version: string): string | undefined {
const override = ARTICLE_OVERRIDES.get(version)
if (override) {
return ARTICLE_PREFIX + override
}
if (version.match(/^\d\dw\d\d[a-z]$/)) {
return ARTICLE_PREFIX + 'minecraft-snapshot-' + version
}
if (version.match(/^\d+\.\d+(\.\d+)?-(pre|rc)[0-9]+$/)) {
return ARTICLE_PREFIX + 'minecraft-' + version.replaceAll('.', '-').replaceAll('pre', 'pre-release-')
}
return undefined
}