Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(changelog): add multi-platform entry loading and pagination

Richie McIlroy03071c392749
diff --git a/apps/web/utils/changelog.ts b/apps/web/utils/changelog.ts
index 46aa3c1f3..547670ac8 100644
--- a/apps/web/utils/changelog.ts
+++ b/apps/web/utils/changelog.ts
@@ -9,6 +9,30 @@ export type ChangelogMetadata = {
image?: string;
};
+export const CHANGELOG_PLATFORMS = [
+ "desktop",
+ "web",
+ "mobile",
+ "extension",
+] as const;
+
+export type ChangelogPlatform = (typeof CHANGELOG_PLATFORMS)[number];
+
+export type ChangelogEntryMetadata = {
+ title: string;
+ publishedAt: string;
+ version?: string;
+ image?: string;
+};
+
+export type ChangelogEntry = {
+ platform: ChangelogPlatform;
+ slug: string;
+ metadata: ChangelogEntryMetadata;
+ intro: string;
+ details: string;
+};
+
function parseFrontmatter(fileContent: string) {
const frontmatterRegex = /---\s*([\s\S]*?)\s*---/;
const match = frontmatterRegex.exec(fileContent);
@@ -30,30 +54,116 @@ function parseFrontmatter(fileContent: string) {
return { metadata: metadata as ChangelogMetadata, content };
}
-const dir = path.join(process.cwd(), "content/changelog");
+const contentRoot = path.join(process.cwd(), "content/changelog");
-function getMDXFiles() {
- return fs.readdirSync(dir).filter((file) => path.extname(file) === ".mdx");
+function readPlatformDir(platform: ChangelogPlatform) {
+ const dir = path.join(contentRoot, platform);
+ if (!fs.existsSync(dir)) return [];
+ return fs
+ .readdirSync(dir)
+ .filter((file) => path.extname(file) === ".mdx")
+ .map((file) => {
+ const { metadata, content } = parseFrontmatter(
+ fs.readFileSync(path.join(dir, file), "utf-8"),
+ );
+ const slug = path.basename(file, path.extname(file));
+ return { metadata, slug, content };
+ });
}
-function readMDXFile(filePath: string) {
- const rawContent = fs.readFileSync(filePath, "utf-8");
- return parseFrontmatter(rawContent);
+export function getChangelogPosts() {
+ return readPlatformDir("desktop");
}
-function getMDXData() {
- const mdxFiles = getMDXFiles();
- return mdxFiles.map((file) => {
- const { metadata, content } = readMDXFile(path.join(dir, file));
- const slug = path.basename(file, path.extname(file));
+function splitEntryContent(content: string) {
+ const headingMatch = /^## /m.exec(content);
+ if (headingMatch) {
return {
- metadata,
- slug,
- content,
+ intro: content.slice(0, headingMatch.index).trim(),
+ details: content.slice(headingMatch.index).trim(),
};
- });
+ }
+ const blocks = content.split(/\n\n+/);
+ const introBlocks: string[] = [];
+ for (const block of blocks) {
+ if (/^[-*#]/.test(block.trim())) break;
+ introBlocks.push(block);
+ }
+ return {
+ intro: introBlocks.join("\n\n").trim(),
+ details: blocks.slice(introBlocks.length).join("\n\n").trim(),
+ };
}
-export function getChangelogPosts() {
- return getMDXData();
+function publishedAtMs(entry: ChangelogEntry) {
+ const ms = new Date(entry.metadata.publishedAt).getTime();
+ return Number.isNaN(ms) ? 0 : ms;
+}
+
+function compareEntries(a: ChangelogEntry, b: ChangelogEntry) {
+ const dateDiff = publishedAtMs(b) - publishedAtMs(a);
+ if (dateDiff !== 0) return dateDiff;
+ const aNum = Number(a.slug);
+ const bNum = Number(b.slug);
+ if (
+ a.platform === b.platform &&
+ Number.isFinite(aNum) &&
+ Number.isFinite(bNum)
+ ) {
+ return bNum - aNum;
+ }
+ return b.slug.localeCompare(a.slug);
+}
+
+export function getChangelogEntries(): ChangelogEntry[] {
+ return CHANGELOG_PLATFORMS.flatMap((platform) =>
+ readPlatformDir(platform).map(({ metadata, slug, content }) => ({
+ platform,
+ slug,
+ metadata: metadata as ChangelogEntryMetadata,
+ ...splitEntryContent(content),
+ })),
+ ).sort(compareEntries);
+}
+
+export const CHANGELOG_PAGE_SIZE = 20;
+
+export type ChangelogPage = {
+ entries: ChangelogEntry[];
+ page: number;
+ pageCount: number;
+ totalEntries: number;
+};
+
+function entriesFor(platform?: ChangelogPlatform) {
+ const entries = getChangelogEntries();
+ return platform
+ ? entries.filter((entry) => entry.platform === platform)
+ : entries;
+}
+
+export function getChangelogPageCount(platform?: ChangelogPlatform) {
+ return Math.max(
+ 1,
+ Math.ceil(entriesFor(platform).length / CHANGELOG_PAGE_SIZE),
+ );
+}
+
+export function getChangelogPage(
+ page: number,
+ platform?: ChangelogPlatform,
+): ChangelogPage | null {
+ const entries = entriesFor(platform);
+ const pageCount = Math.max(
+ 1,
+ Math.ceil(entries.length / CHANGELOG_PAGE_SIZE),
+ );
+ if (!Number.isInteger(page) || page < 1 || page > pageCount) return null;
+ const start = (page - 1) * CHANGELOG_PAGE_SIZE;
+ return {
+ entries: entries.slice(start, start + CHANGELOG_PAGE_SIZE),
+ page,
+ pageCount,
+ totalEntries: entries.length,
+ };
}