-
Notifications
You must be signed in to change notification settings - Fork 0
blog: use date-slug url format #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,15 +14,20 @@ years.forEach(year => { | |
| if (file.endsWith('.md')) { | ||
| const fileName = file.replace('.md', ''); | ||
| const parts = fileName.split('-'); | ||
| const date = parts.slice(0, 3).join('-'); | ||
|
|
||
| const y = parts[0]; | ||
| const m = parts[1]; | ||
| const d = parts[2]; | ||
| const slug = parts.slice(3).join('-'); | ||
|
Comment on lines
16
to
21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation is vulnerable to malformed filenames (e.g., without a date or slug), which could lead to const parts = fileName.split('-');
if (parts.length < 4) {
console.warn(`[!] Skipping post with invalid filename format: ${file}`);
return;
}
const [y, m, d, ...slugParts] = parts;
const slug = slugParts.join('-'); |
||
|
|
||
| allPosts.push({ | ||
| year, | ||
| date, | ||
| year: y, | ||
| month: m, | ||
| day: d, | ||
| slug, | ||
| originalName: fileName, | ||
| title: slug.replace(/-/g, ' ') | ||
| title: slug.replace(/-/g, ' '), | ||
| date: `${y}-${m}-${d}` | ||
| }); | ||
| } | ||
| }); | ||
|
|
@@ -38,7 +43,7 @@ if (fs.existsSync(path.join(distDir, 'index.html'))) { | |
| fs.writeFileSync(path.join(distDir, 'posts.json'), postsData); | ||
|
|
||
| allPosts.forEach(post => { | ||
| const targetDir = path.join(distDir, post.year); | ||
| const targetDir = path.join(distDir, post.year, post.month, post.day); | ||
| if (!fs.existsSync(targetDir)) { | ||
| fs.mkdirSync(targetDir, { recursive: true }); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,14 @@ export default function Home({ posts }) { | |
| {currentPosts.map(post => ( | ||
| <li key={post.originalName} className="post-item"> | ||
| <span className="post-date">{post.date}</span> | ||
| <a href={`/${post.year}/${post.slug}.html`} className="post-link"> | ||
| <a href={`/${post.year}/${post.month}/${post.day}/${post.slug}.html`} className="post-link"> | ||
| {post.title} | ||
| </a> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
|
|
||
| <nav className="pagination"> | ||
| <nav className="pagination" style={{ display: 'flex', gap: '10px', marginTop: '20px' }}> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| {hasPrev && ( | ||
| <button onClick={() => setCurrentPage(p => p - 1)}>← Newer</button> | ||
| )} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛑 Logic Error: Accessing array indices without validation will crash when filename format is invalid. If a markdown file doesn't follow the expected
YYYY-MM-DD-slug.mdformat (e.g.,incomplete.mdor2024-01.md), accessingparts[0],parts[1], orparts[2]will return undefined, causing the build to produce invalid data or fail.