summaryrefslogtreecommitdiff
path: root/scripts/changelog-checker/src/parser.ts
blob: 894d60d6aff3bb665d9e61f8fb96955dbce25f2d (plain)
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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import * as fs from 'node:fs';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import { Heading, List, Node } from 'mdast';
import { toString } from 'mdast-util-to-string';

export class Release {
	public readonly releaseName: string;
	public readonly categories: ReleaseCategory[];

	constructor(releaseName: string, categories: ReleaseCategory[] = []) {
		this.releaseName = releaseName;
		this.categories = [...categories];
	}
}

export class ReleaseCategory {
	public readonly categoryName: string;
	public readonly items: string[];

	constructor(categoryName: string, items: string[] = []) {
		this.categoryName = categoryName;
		this.items = [...items];
	}
}

function isHeading(node: Node): node is Heading {
	return node.type === 'heading';
}

function isList(node: Node): node is List {
	return node.type === 'list';
}

export function parseChangeLog(path: string): Release[] {
	const input = fs.readFileSync(path, { encoding: 'utf8' });
	const processor = unified().use(remarkParse);

	const releases: Release[] = [];
	const root = processor.parse(input);

	let release: Release | null = null;
	let category: ReleaseCategory | null = null;
	for (const it of root.children) {
		if (isHeading(it) && it.depth === 2) {
			// リリース
			release = new Release(toString(it));
			releases.push(release);
		} else if (isHeading(it) && it.depth === 3 && release) {
			// リリース配下のカテゴリ
			category = new ReleaseCategory(toString(it));
			release.categories.push(category);
		} else if (isList(it) && category) {
			for (const listItem of it.children) {
				// カテゴリ配下のリスト項目
				category.items.push(toString(listItem));
			}
		}
	}

	return releases;
}