Skip to content

Commit c64dc37

Browse files
committed
use all blogs in the sitemap
Signed-off-by: Jessie Ssebuliba <jessiessebuliba@gmail.com>
1 parent d43dd12 commit c64dc37

1 file changed

Lines changed: 35 additions & 34 deletions

File tree

src/lib/markdown.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,36 @@ export interface PostData {
3434
*/
3535
function getPostFilename(slug: string, locale: string): string | null {
3636
const files = fs.readdirSync(postsDirectory);
37-
37+
3838
// For non-default locale (e.g., German), look for files with locale suffix
3939
if (locale !== 'en') {
4040
// Try to find a file with the locale suffix
41-
const localeFile = files.find((file) => {
41+
const localeFile = files.find(file => {
4242
const withoutExt = file.replace(/\.md$/, '');
43-
return withoutExt.endsWith(`.${locale}`) && withoutExt.replace(`.${locale}`, '').endsWith(slug);
43+
return (
44+
withoutExt.endsWith(`.${locale}`) &&
45+
withoutExt.replace(`.${locale}`, '').endsWith(slug)
46+
);
4447
});
45-
48+
4649
if (localeFile) return localeFile;
47-
50+
4851
// Also check if the slug itself matches
49-
const directMatch = files.find((file) => {
52+
const directMatch = files.find(file => {
5053
const withoutLocaleExt = file.replace(`.${locale}.md`, '');
5154
return withoutLocaleExt === slug || file === `${slug}.${locale}.md`;
5255
});
53-
56+
5457
if (directMatch) return directMatch;
5558
}
56-
59+
5760
// For English or fallback, look for files without locale suffix
58-
const englishFile = files.find((file) => {
61+
const englishFile = files.find(file => {
5962
if (file.endsWith('.de.md')) return false; // Skip German files
6063
const withoutExt = file.replace(/\.md$/, '');
6164
return withoutExt.endsWith(slug) || withoutExt === slug;
6265
});
63-
66+
6467
return englishFile || null;
6568
}
6669

@@ -81,51 +84,46 @@ function extractSlugFromFilename(filename: string, locale: string): string {
8184
*/
8285
export function getAllPosts(locale: string): PostData[] {
8386
const files = fs.readdirSync(postsDirectory);
84-
87+
8588
const posts: PostData[] = [];
86-
89+
8790
for (const filename of files) {
8891
// Skip non-markdown files
8992
if (!filename.endsWith('.md')) continue;
90-
93+
9194
// Determine which posts to include based on locale
9295
const isLocaleFile = filename.endsWith(`.${locale}.md`);
9396
const isDefaultFile = !filename.includes('.de.md'); // No locale suffix = English
94-
97+
9598
// For German locale, prefer .de.md files
9699
if (locale === 'de') {
97100
if (!isLocaleFile) continue; // Skip non-German files for German locale list
98101
} else {
99102
// For English, skip locale-specific files
100103
if (!isDefaultFile) continue;
101104
}
102-
105+
103106
const slug = extractSlugFromFilename(filename, locale);
104107
const fullPath = path.join(postsDirectory, filename);
105108
const fileContents = fs.readFileSync(fullPath, 'utf8');
106109
const { data, content } = matter(fileContents);
107-
110+
108111
const frontmatter = data as PostFrontmatter;
109-
110-
// Skip posts that shouldn't be shown in blog
111-
if (frontmatter.showInBlog === false || frontmatter.outdated === true) {
112-
continue;
113-
}
114-
112+
115113
posts.push({
116114
slug,
117115
frontmatter,
118116
content,
119117
});
120118
}
121-
119+
122120
// Sort by date (newest first)
123121
posts.sort((a, b) => {
124122
const dateA = new Date(a.frontmatter.date);
125123
const dateB = new Date(b.frontmatter.date);
126124
return dateB.getTime() - dateA.getTime();
127125
});
128-
126+
129127
return posts;
130128
}
131129

@@ -135,21 +133,21 @@ export function getAllPosts(locale: string): PostData[] {
135133
export function getAllPostSlugs(): Array<{ locale: string; slug: string }> {
136134
const files = fs.readdirSync(postsDirectory);
137135
const slugs: Array<{ locale: string; slug: string }> = [];
138-
136+
139137
for (const filename of files) {
140138
if (!filename.endsWith('.md')) continue;
141-
139+
142140
// Parse file to check if it should be shown
143141
const fullPath = path.join(postsDirectory, filename);
144142
const fileContents = fs.readFileSync(fullPath, 'utf8');
145143
const { data } = matter(fileContents);
146144
const frontmatter = data as PostFrontmatter;
147-
145+
148146
// Skip hidden posts
149147
if (frontmatter.showInBlog === false || frontmatter.outdated === true) {
150148
continue;
151149
}
152-
150+
153151
if (filename.endsWith('.de.md')) {
154152
// German post
155153
const slug = filename.replace('.de.md', '');
@@ -160,36 +158,39 @@ export function getAllPostSlugs(): Array<{ locale: string; slug: string }> {
160158
slugs.push({ locale: 'en', slug });
161159
}
162160
}
163-
161+
164162
return slugs;
165163
}
166164

167165
/**
168166
* Get a single post by slug and locale
169167
*/
170-
export async function getPostBySlug(slug: string, locale: string): Promise<PostData | null> {
168+
export async function getPostBySlug(
169+
slug: string,
170+
locale: string,
171+
): Promise<PostData | null> {
171172
try {
172173
const filename = getPostFilename(slug, locale);
173-
174+
174175
if (!filename) {
175176
// Fallback to English if locale-specific post doesn't exist
176177
if (locale !== 'en') {
177178
return getPostBySlug(slug, 'en');
178179
}
179180
return null;
180181
}
181-
182+
182183
const fullPath = path.join(postsDirectory, filename);
183184
const fileContents = fs.readFileSync(fullPath, 'utf8');
184185
const { data, content } = matter(fileContents);
185-
186+
186187
// Convert markdown to HTML
187188
const processedContent = await remark()
188189
.use(remarkHugoShortcodes)
189190
.use(html, { sanitize: false })
190191
.process(content);
191192
const contentHtml = processedContent.toString();
192-
193+
193194
return {
194195
slug,
195196
frontmatter: data as PostFrontmatter,

0 commit comments

Comments
 (0)