-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchangelog-recent.js
More file actions
206 lines (179 loc) · 6.62 KB
/
changelog-recent.js
File metadata and controls
206 lines (179 loc) · 6.62 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const fs = require("fs");
const path = require("path");
const matter = require("gray-matter");
/**
* Plugin that reads changelog markdown files and exposes the latest N entries
* via Docusaurus globalData for use in the landing page component.
*/
module.exports = function pluginChangelogRecent(context, options) {
const { count = 3 } = options;
return {
name: "changelog-recent",
async loadContent() {
const changelogDir = path.join(context.siteDir, "changelog");
if (!fs.existsSync(changelogDir)) {
return [];
}
const files = fs
.readdirSync(changelogDir)
.filter((f) => f.endsWith(".md") || f.endsWith(".mdx"));
const entries = files.map((file) => {
const raw = fs.readFileSync(path.join(changelogDir, file), "utf-8");
const { data: frontmatter, content } = matter(raw);
// Parse sections from content
const sections = parseSections(content);
// Extract date: prefer frontmatter, fallback to filename (YYYY-MM-DD prefix)
let date = "";
if (frontmatter.date) {
date = new Date(frontmatter.date).toISOString().split("T")[0];
} else {
const dateMatch = file.match(/^(\d{4}-\d{2}-\d{2})/);
if (dateMatch) {
date = dateMatch[1];
}
}
// Build slug matching Docusaurus blog URL format: YYYY/MM/DD/slug
// e.g. "2026-02-23-version-2.6.0.md" → "2026/02/23/version-2.6.0"
let slug = frontmatter.slug || "";
if (!slug) {
const dateMatch = file.match(/^(\d{4})-(\d{2})-(\d{2})-(.*?)\.mdx?$/);
if (dateMatch) {
const [, y, m, d, name] = dateMatch;
slug = `${y}/${m}/${d}/${name}`;
} else {
slug = file.replace(/\.mdx?$/, "");
}
}
return {
title: frontmatter.title || "",
date,
slug,
tags: frontmatter.tags || [],
sections,
};
});
// Sort by date descending and take the latest N
entries.sort((a, b) => b.date.localeCompare(a.date));
return entries.slice(0, count);
},
async contentLoaded({ content, actions }) {
const { setGlobalData } = actions;
setGlobalData({ recentChangelogs: content });
},
};
};
/**
* Parse markdown content into categorized sections with their items.
* Supports two formats:
*
* Format A (legacy):
* ## New Features
* - **Title:** Description
*
* Format B (new template):
* 🚀 New Features
* Title · Area
* Description text.
*/
function parseSections(content) {
const sections = [];
const lines = content.split("\n");
let currentSection = null;
let pendingTitle = null; // Format B: title line waiting for its description
for (const line of lines) {
// Empty line: flush any pending title without description
if (!line.trim()) {
pendingTitle = null;
continue;
}
// --- Section heading detection ---
// Format A: ## headings (e.g., "## New Features", "## 🚀 New Features")
const headingMatch = line.match(/^#{2,3}\s+\**(.+?)\**[:]*\s*$/);
if (headingMatch) {
pendingTitle = null;
const heading = cleanHeading(headingMatch[1].trim());
if (/^version\s/i.test(heading)) continue;
currentSection = { heading, type: categorizeSection(heading), items: [] };
sections.push(currentSection);
continue;
}
// Format A: bold-line sections (e.g., "**🚀 New Features:**")
const boldSectionMatch = line.match(/^\*\*(.+?)\*\*[:]*\s*$/);
if (boldSectionMatch) {
pendingTitle = null;
const heading = cleanHeading(boldSectionMatch[1].trim());
if (categorizeSection(heading) !== "improved" || /feature|fix|bug/i.test(heading)) {
currentSection = { heading, type: categorizeSection(heading), items: [] };
sections.push(currentSection);
continue;
}
}
// Format B: bare emoji section heading (e.g., "🚀 New Features", "🐛 Bug Fixes")
const emojiHeadingMatch = line.match(/^[\p{Emoji_Presentation}]\s+(.+)/u);
if (emojiHeadingMatch) {
pendingTitle = null;
const heading = cleanHeading(line.trim());
const type = categorizeSection(heading);
if (type !== "improved" || /feature|fix|bug|improv|new/i.test(heading)) {
currentSection = { heading, type, items: [] };
sections.push(currentSection);
continue;
}
}
// Skip admonition markers and import/MDX lines
if (line.startsWith(":::") || line.startsWith("import ")) continue;
// --- Item detection (only inside a section) ---
if (currentSection) {
// Format A: "- **Title:** Description" or "- **Title** Description"
const boldItemMatch = line.match(/^-\s+\*\*(.+?)\*\*[:.]?\s*(.*)/);
if (boldItemMatch) {
pendingTitle = null;
currentSection.items.push({
title: boldItemMatch[1].trim(),
description: boldItemMatch[2].trim(),
});
continue;
}
// Format A: "- Plain text description"
const plainItemMatch = line.match(/^-\s+(.+)/);
if (plainItemMatch) {
pendingTitle = null;
const text = plainItemMatch[1].trim();
const colonIdx = text.indexOf(":");
if (colonIdx > 0 && colonIdx < 60) {
currentSection.items.push({
title: text.substring(0, colonIdx).trim(),
description: text.substring(colonIdx + 1).trim(),
});
} else {
currentSection.items.push({ title: text, description: "" });
}
continue;
}
// Format B: "Title · Area" — store as pending title
if (line.includes("·") && !line.startsWith("#") && !line.startsWith("*")) {
pendingTitle = line.split("·")[0].trim();
continue;
}
// Format B: description line following a pending title
if (pendingTitle && !line.startsWith("#") && !line.startsWith("*") && !line.startsWith("-")) {
currentSection.items.push({ title: pendingTitle, description: line.trim() });
pendingTitle = null;
continue;
}
}
}
return sections;
}
/**
* Remove emoji prefixes from headings for clean display.
*/
function cleanHeading(heading) {
return heading.replace(/^[\p{Emoji}\p{Emoji_Presentation}\uFE0F\u200D]+\s*/u, "").trim();
}
function categorizeSection(heading) {
const lower = heading.toLowerCase();
if (lower.includes("bug") || lower.includes("fix") || lower.includes("correc")) return "fixed";
if (lower.includes("new") || lower.includes("feature") || lower.includes("added") || lower.includes("nueva") || lower.includes("funcionalidad")) return "added";
return "improved";
}