misskey/scripts/changelog-checker/src/parser.ts
zyoshoka 29f6ba6310
chore: add missing SPDX ID and workflow check (#13570)
* chore: add workflow which checks if SPDX ID exists

* chore: add missing SPDX ID in some files

* chore: change trigger condition

* chore: trigger on push

* lint
2024-03-13 22:37:18 +09:00

68 lines
1.8 KiB
TypeScript

/*
* 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;
}