-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmarkdown.ts
More file actions
752 lines (642 loc) · 20.9 KB
/
Copy pathmarkdown.ts
File metadata and controls
752 lines (642 loc) · 20.9 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import GithubSlugger from 'github-slugger';
import Prism from 'prismjs';
import loadLanguages from 'prismjs/components/index.js';
import { remark } from 'remark';
import remarkGfm from 'remark-gfm';
import html from 'remark-html';
import { transformHugoShortcodes } from './remark-hugo-shortcodes';
// Path to the posts directory
const postsDirectory = path.join(process.cwd(), 'content/posts');
export interface PostFrontmatter {
title: string;
date: string;
author: string;
excerpt: string;
categories: string[];
preview_image: string;
slug?: string;
outdated?: boolean;
showInBlog?: boolean;
}
export interface PostData {
slug: string;
frontmatter: PostFrontmatter;
content: string;
contentHtml?: string;
}
const INTERNAL_HOSTNAMES = new Set([
'open-elements.com',
'www.open-elements.com',
'localhost',
'127.0.0.1',
'::1',
]);
const EXTERNAL_LINK_ICON_HTML =
'<span class="iconify inline" data-icon="mdi-open-in-new" aria-hidden="true"></span>';
const HEADING_ANCHOR_ICON_HTML =
'<span class="iconify w-7 h-7 shrink-0 text-lightgray inline" data-icon="mdi-link-variant" aria-hidden="true"></span>';
const CODE_BLOCK_PATTERN =
/<pre><code class="language-([^"]+)">([\s\S]*?)<\/code><\/pre>/gi;
const PRISM_TOKEN_COLORS = new Map<string, string>([
['comment', '#75715e'],
['prolog', '#75715e'],
['doctype', '#75715e'],
['cdata', '#75715e'],
['punctuation', '#f92672'],
['operator', '#f92672'],
['tag', '#f92672'],
['deleted', '#f92672'],
['keyword', '#66d9ef'],
['class-name', '#a6e22e'],
['function', '#a6e22e'],
['function-variable', '#a6e22e'],
['method', '#a6e22e'],
['selector', '#a6e22e'],
['attr-name', '#a6e22e'],
['builtin', '#a6e22e'],
['property', '#a6e22e'],
['string', '#e6db74'],
['char', '#e6db74'],
['attr-value', '#e6db74'],
['regex', '#e6db74'],
['variable', '#e6db74'],
['number', '#ae81ff'],
['boolean', '#ae81ff'],
['constant', '#ae81ff'],
['symbol', '#ae81ff'],
['important', '#ae81ff'],
['entity', '#f8f8f2'],
['url', '#f8f8f2'],
['namespace', '#f8f8f2'],
]);
loadLanguages([
'bash',
'css',
'docker',
'html',
'java',
'javascript',
'jsdoc',
'json',
'log',
'markup',
'shell-session',
'typescript',
'xml',
]);
Prism.hooks.add('wrap', environment => {
const tokenClass = environment.classes.find(
className => className !== 'token' && PRISM_TOKEN_COLORS.has(className),
);
if (!tokenClass) {
return;
}
const color = PRISM_TOKEN_COLORS.get(tokenClass);
if (!color) {
return;
}
const existingStyle = environment.attributes.style;
environment.attributes.style = existingStyle
? `${existingStyle};color:${color};`
: `color:${color};`;
});
/**
* Derive the canonical slug segment from a post filename.
* Strips the `.md` / `.de.md` extension and any leading `YYYY-MM-DD-` date
* prefix, so e.g. `2026-03-19-container-gov.de.md` -> `container-gov`.
*/
function slugFromFilename(filename: string): string {
const base = filename.endsWith('.de.md')
? filename.slice(0, -'.de.md'.length)
: filename.slice(0, -'.md'.length);
return base.replace(/^\d{4}-\d{2}-\d{2}-/, '');
}
/**
* Generate a post slug.
* Prefers an explicit `slug` frontmatter field (kept for legacy posts whose
* URLs must not change); otherwise derives the slug from the filename stem.
*/
function generatePostSlug(
frontmatter: PostFrontmatter,
filename: string,
): string {
if (frontmatter.slug) {
return frontmatter.slug;
}
return slugFromFilename(filename);
}
/**
* Generate the full date-based post path from frontmatter.
* Format: YYYY/MM/DD/slug-text
*/
function generatePostPath(
frontmatter: PostFrontmatter,
filename: string,
): string {
const date = new Date(frontmatter.date);
const year = date.getUTCFullYear().toString();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
const slug = generatePostSlug(frontmatter, filename);
return `${year}/${month}/${day}/${slug}`;
}
function normalizeLegacySlugSegment(slug: string): string {
return slug
.toLowerCase()
.replace(/[^\p{L}\p{N}]+/gu, '-')
.replace(/-+/g, '-')
.replace(/^-+|-+$/g, '');
}
function isDateBasedSlugMatch(
candidatePath: string,
requestedPath: string,
): boolean {
if (candidatePath === requestedPath) {
return true;
}
const candidateSegments = candidatePath.split('/');
const requestedSegments = requestedPath.split('/');
if (candidateSegments.length !== 4 || requestedSegments.length !== 4) {
return false;
}
const candidateDatePrefix = candidateSegments.slice(0, 3).join('/');
const requestedDatePrefix = requestedSegments.slice(0, 3).join('/');
if (candidateDatePrefix !== requestedDatePrefix) {
return false;
}
return (
normalizeLegacySlugSegment(candidateSegments[3]) ===
normalizeLegacySlugSegment(requestedSegments[3])
);
}
function decodePathSegments(pathValue: string): string {
return pathValue
.split('/')
.map(segment => {
try {
return decodeURIComponent(segment);
} catch {
return segment;
}
})
.join('/');
}
/**
* Find the filename for a post matching the given date-based slug path and locale.
* slugPath format: "YYYY/MM/DD/slug-text"
*
* Each locale derives slugs from its own frontmatter, so EN and DE may have
* different slug paths for the same post.
*/
function getPostFilename(slugPath: string, locale: string): string | null {
const files = fs.readdirSync(postsDirectory);
const decodedSlugPath = decodePathSegments(slugPath);
// Legacy NextJS path format: "YYYY-MM-DD-post-slug" (filename base)
if (!decodedSlugPath.includes('/')) {
if (locale === 'de') {
const deFilename = `${decodedSlugPath}.de.md`;
return files.includes(deFilename) ? deFilename : null;
}
const enFilename = `${decodedSlugPath}.md`;
if (files.includes(enFilename) && !enFilename.includes('.de.md')) {
return enFilename;
}
return null;
}
for (const filename of files) {
if (!filename.endsWith('.md')) continue;
// Filter to the requested locale's files
const isLocaleFile = filename.endsWith(`.${locale}.md`);
const isDefaultFile = !filename.includes('.de.md');
if (locale === 'de' && !isLocaleFile) continue;
if (locale === 'en' && !isDefaultFile) continue;
const fullPath = path.join(postsDirectory, filename);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data } = matter(fileContents);
if (
isDateBasedSlugMatch(
generatePostPath(data as PostFrontmatter, filename),
decodedSlugPath,
)
) {
return filename;
}
}
return null;
}
/**
* Get the base filename shared between EN and DE versions of a post.
* e.g. "2026-02-10-review-2025.de.md" → "2026-02-10-review-2025"
* e.g. "2026-02-10-review-2025.md" → "2026-02-10-review-2025"
*/
function getBaseFilename(filename: string): string {
if (filename.endsWith('.de.md')) {
return filename.replace('.de.md', '');
}
return filename.replace('.md', '');
}
/**
* Given a slug path and the current locale, find all available locale versions
* by filename pairing. Returns each locale's own slug path.
*/
export function getPostLocaleAlternates(
slugPath: string,
currentLocale: string,
): Array<{ locale: string; slugPath: string }> {
const currentFile = getPostFilename(slugPath, currentLocale);
if (!currentFile) return [];
const base = getBaseFilename(currentFile);
const files = fs.readdirSync(postsDirectory);
const alternates: Array<{ locale: string; slugPath: string }> = [];
// Check English version
const enFile = `${base}.md`;
if (files.includes(enFile)) {
const fullPath = path.join(postsDirectory, enFile);
const { data } = matter(fs.readFileSync(fullPath, 'utf8'));
alternates.push({
locale: 'en',
slugPath: generatePostPath(data as PostFrontmatter, enFile),
});
}
// Check German version
const deFile = `${base}.de.md`;
if (files.includes(deFile)) {
const fullPath = path.join(postsDirectory, deFile);
const { data } = matter(fs.readFileSync(fullPath, 'utf8'));
alternates.push({
locale: 'de',
slugPath: generatePostPath(data as PostFrontmatter, deFile),
});
}
return alternates;
}
function isInternalHostname(hostname: string): boolean {
const normalizedHostname = hostname.toLowerCase();
return (
INTERNAL_HOSTNAMES.has(normalizedHostname) ||
normalizedHostname.endsWith('.open-elements.com')
);
}
function isExternalContentLink(href: string): boolean {
const normalizedHref = href.trim();
if (
!normalizedHref ||
normalizedHref.startsWith('/') ||
normalizedHref.startsWith('#') ||
normalizedHref.startsWith('mailto:') ||
normalizedHref.startsWith('tel:')
) {
return false;
}
try {
const url = new URL(normalizedHref, 'https://open-elements.com');
return !isInternalHostname(url.hostname);
} catch {
return false;
}
}
function ensureBlankTarget(attributes: string): string {
if (/\btarget\s*=/i.test(attributes)) {
return attributes.replace(/\btarget\s*=\s*(["']).*?\1/i, 'target="_blank"');
}
return `${attributes} target="_blank"`;
}
function ensureSafeRel(attributes: string): string {
const relPattern = /\brel\s*=\s*(["'])(.*?)\1/i;
if (relPattern.test(attributes)) {
return attributes.replace(
relPattern,
(_match, quote: string, relValue: string) => {
const tokens = new Set(relValue.split(/\s+/).filter(Boolean));
tokens.add('noopener');
tokens.add('noreferrer');
return `rel=${quote}${Array.from(tokens).join(' ')}${quote}`;
},
);
}
return `${attributes} rel="noopener noreferrer"`;
}
function addExternalLinkIcon(linkContent: string): string {
if (/data-icon\s*=\s*(["'])mdi-open-in-new\1/i.test(linkContent)) {
return linkContent;
}
return `${linkContent}${EXTERNAL_LINK_ICON_HTML}`;
}
function decorateExternalLinks(contentHtml: string): string {
return contentHtml.replace(
/<a\b([^>]*?)href=(["'])(.*?)\2([^>]*)>([\s\S]*?)<\/a>/gi,
(
fullMatch,
attributesBeforeHref: string,
quote: string,
href: string,
attributesAfterHref: string,
linkContent: string,
) => {
if (!isExternalContentLink(href)) {
return fullMatch;
}
let attributes = `${attributesBeforeHref}href=${quote}${href}${quote}${attributesAfterHref}`;
attributes = ensureBlankTarget(attributes);
attributes = ensureSafeRel(attributes);
return `<a${attributes}>${addExternalLinkIcon(linkContent)}</a>`;
},
);
}
const STANDALONE_IMAGE_STYLE =
'display: block; max-width: 100%; height: auto; margin-left: auto; margin-right: auto;';
function applyStandaloneImageStyle(imageHtml: string): string {
if (/\bstyle\s*=/i.test(imageHtml)) {
return imageHtml.replace(
/\bstyle\s*=\s*(["'])(.*?)\1/i,
(_match, quote: string, styleValue: string) => {
const normalizedStyle = styleValue.trim().replace(/;?\s*$/, ';');
return `style=${quote}${normalizedStyle} ${STANDALONE_IMAGE_STYLE}${quote}`;
},
);
}
return imageHtml.replace('<img', `<img style="${STANDALONE_IMAGE_STYLE}"`);
}
function centerStandaloneHtmlImages(contentHtml: string): string {
return contentHtml.replace(
/(^|\n)\s*(<img\b[^>]*>)\s*(?=\n|$)/gi,
(_match, prefix: string, imageHtml: string) =>
`${prefix}<div style="text-align: center; margin: 2rem 0;">${applyStandaloneImageStyle(imageHtml)}</div>`,
);
}
function escapeHtml(text: string): string {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function decodeHtmlEntities(text: string): string {
const namedEntities: Record<string, string> = {
amp: '&',
apos: "'",
gt: '>',
lt: '<',
nbsp: ' ',
quot: '"',
};
return text.replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (entity, value: string) => {
const normalizedValue = value.toLowerCase();
if (normalizedValue.startsWith('#x')) {
const codePoint = Number.parseInt(normalizedValue.slice(2), 16);
return Number.isNaN(codePoint) ? entity : String.fromCodePoint(codePoint);
}
if (normalizedValue.startsWith('#')) {
const codePoint = Number.parseInt(normalizedValue.slice(1), 10);
return Number.isNaN(codePoint) ? entity : String.fromCodePoint(codePoint);
}
return namedEntities[normalizedValue] ?? entity;
});
}
function extractHeadingText(headingHtml: string): string {
return decodeHtmlEntities(headingHtml.replace(/<[^>]*>/g, ' '))
.replace(/\s+/g, ' ')
.trim();
}
function appendHtmlAttribute(attributes: string, attribute: string): string {
const normalizedAttributes = attributes.trimEnd();
return normalizedAttributes
? `${normalizedAttributes} ${attribute}`
: ` ${attribute}`;
}
function ensureHtmlClass(attributes: string, className: string): string {
const classPattern = /\bclass\s*=\s*(["'])(.*?)\1/i;
if (classPattern.test(attributes)) {
return attributes.replace(
classPattern,
(_match, quote: string, classValue: string) => {
const classes = new Set(classValue.split(/\s+/).filter(Boolean));
classes.add(className);
return `class=${quote}${Array.from(classes).join(' ')}${quote}`;
},
);
}
return appendHtmlAttribute(attributes, `class="${className}"`);
}
function createHeadingAnchorHtml(headingId: string): string {
return `<a href="#${headingId}" class="inline ml-2" aria-label="Link to this section">${HEADING_ANCHOR_ICON_HTML}</a>`;
}
function decorateHeadlinesWithAnchors(contentHtml: string): string {
const slugger = new GithubSlugger();
return contentHtml.replace(
/<h([2-6])([^>]*)>([\s\S]*?)<\/h\1>/gi,
(fullMatch, level: string, attributes: string, headingContent: string) => {
if (/data-icon\s*=\s*(["'])mdi-link-variant\1/i.test(headingContent)) {
return fullMatch;
}
const headingText = extractHeadingText(headingContent);
if (!headingText) {
return fullMatch;
}
const existingIdMatch = attributes.match(/\bid\s*=\s*(["'])(.*?)\1/i);
const headingId = existingIdMatch?.[2] || slugger.slug(headingText);
let nextAttributes = attributes;
if (!existingIdMatch) {
nextAttributes = appendHtmlAttribute(
nextAttributes,
`id="${headingId}"`,
);
}
nextAttributes = ensureHtmlClass(nextAttributes, 'scroll-mt-10');
return `<h${level}${nextAttributes}>${headingContent}${createHeadingAnchorHtml(headingId)}</h${level}>`;
},
);
}
function normalizeCodeLanguage(language: string): string | null {
const normalizedLanguage = language.trim().toLowerCase();
if (!normalizedLanguage) {
return null;
}
const aliases: Array<[string, string | null]> = [
['bash', 'bash'],
['sh', 'bash'],
['shell', 'bash'],
['shell-session', 'shell-session'],
['css', 'css'],
['docker', 'docker'],
['html', 'markup'],
['java', 'java'],
['javascript', 'javascript'],
['js', 'javascript'],
['json', 'json'],
['log', 'log'],
['text', null],
['txt', null],
['plaintext', null],
['typescript', 'typescript'],
['ts', 'typescript'],
['xml', 'markup'],
];
for (const [prefix, mappedLanguage] of aliases) {
if (
normalizedLanguage === prefix ||
normalizedLanguage.startsWith(prefix)
) {
return mappedLanguage;
}
}
return normalizedLanguage in Prism.languages ? normalizedLanguage : null;
}
function highlightCode(language: string, code: string): string {
const normalizedLanguage = normalizeCodeLanguage(language);
if (!normalizedLanguage) {
return escapeHtml(code);
}
const grammar = Prism.languages[normalizedLanguage];
if (!grammar) {
return escapeHtml(code);
}
return Prism.highlight(code, grammar, normalizedLanguage);
}
function highlightCodeBlocks(contentHtml: string): string {
return contentHtml.replace(
CODE_BLOCK_PATTERN,
(_fullMatch, language: string, encodedCode: string) => {
const decodedCode = decodeHtmlEntities(encodedCode);
const highlightedCode = highlightCode(language, decodedCode);
return [
'<div class="highlight">',
`<pre tabindex="0" class="language-${language}" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">`,
`<code class="language-${language}" data-lang="${language}">${highlightedCode}</code>`,
'</pre>',
'</div>',
].join('');
},
);
}
/**
* Get all posts for a specific locale.
* By default, posts with showInBlog === false are excluded (for the blog listing UI).
* Pass includeHidden = true to include all posts (e.g. for sitemap generation).
*/
export function getAllPosts(
locale: string,
{ includeHidden = false }: { includeHidden?: boolean } = {},
): PostData[] {
const files = fs.readdirSync(postsDirectory);
const posts: PostData[] = [];
for (const filename of files) {
// Skip non-markdown files
if (!filename.endsWith('.md')) continue;
// Determine which posts to include based on locale
const isLocaleFile = filename.endsWith(`.${locale}.md`);
const isDefaultFile = !filename.includes('.de.md'); // No locale suffix = English
// For German locale, prefer .de.md files
if (locale === 'de') {
if (!isLocaleFile) continue; // Skip non-German files for German locale list
} else {
// For English, skip locale-specific files
if (!isDefaultFile) continue;
}
const fullPath = path.join(postsDirectory, filename);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data, content } = matter(fileContents);
const frontmatter = data as PostFrontmatter;
// Skip posts not meant for the blog listing unless includeHidden is true
if (!includeHidden && frontmatter.showInBlog === false) {
continue;
}
const slug = generatePostPath(frontmatter, filename);
posts.push({
slug,
frontmatter,
content,
});
}
// Sort by date (newest first)
posts.sort((a, b) => {
const dateA = new Date(a.frontmatter.date);
const dateB = new Date(b.frontmatter.date);
return dateB.getTime() - dateA.getTime();
});
return posts;
}
/**
* Get all post slugs for all locales (used in generateStaticParams)
* Returns slug as a string array for catch-all routing: ['YYYY', 'MM', 'DD', 'slug-text']
*/
export function getAllPostSlugs(): Array<{ locale: string; slug: string[] }> {
const files = fs.readdirSync(postsDirectory);
const slugs: Array<{ locale: string; slug: string[] }> = [];
for (const filename of files) {
if (!filename.endsWith('.md')) continue;
// Parse file to check if it should be shown
const fullPath = path.join(postsDirectory, filename);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data } = matter(fileContents);
const frontmatter = data as PostFrontmatter;
const postPath = generatePostPath(frontmatter, filename);
const slugSegments = postPath.split('/');
const legacyBaseSlug = filename.endsWith('.de.md')
? filename.replace('.de.md', '')
: filename.replace('.md', '');
const legacySlugSegments = [legacyBaseSlug];
if (filename.endsWith('.de.md')) {
slugs.push({ locale: 'de', slug: slugSegments });
slugs.push({ locale: 'de', slug: legacySlugSegments });
} else {
slugs.push({ locale: 'en', slug: slugSegments });
slugs.push({ locale: 'en', slug: legacySlugSegments });
}
}
return slugs;
}
/**
* Get a single post by slug path and locale.
* slugPath can be a joined path like "2026/03/26/slug-text" or segments joined with "/".
*/
export async function getPostBySlug(
slugPath: string,
locale: string,
): Promise<PostData | null> {
try {
const filename = getPostFilename(slugPath, locale);
if (!filename) {
return null;
}
const fullPath = path.join(postsDirectory, filename);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data, content } = matter(fileContents);
const transformedContent = transformHugoShortcodes(content);
// Convert markdown to HTML
const processedContent = await remark()
.use(remarkGfm)
.use(html, { sanitize: false })
.process(transformedContent);
const contentHtml = highlightCodeBlocks(
decorateHeadlinesWithAnchors(
decorateExternalLinks(
centerStandaloneHtmlImages(processedContent.toString()),
),
),
);
return {
slug: slugPath,
frontmatter: data as PostFrontmatter,
content,
contentHtml,
};
} catch (error) {
console.error(
`Error fetching post ${slugPath} for locale ${locale}:`,
error,
);
return null;
}
}
/**
* Check if a post exists for a given locale.
* slugPath format: "YYYY/MM/DD/slug-text"
*/
export function postExistsForLocale(slugPath: string, locale: string): boolean {
return getPostFilename(slugPath, locale) !== null;
}