This commit is contained in:
parent
eceb31a9ad
commit
e9b6035c58
48 changed files with 354 additions and 340 deletions
|
|
@ -7,34 +7,35 @@ import rehypeAutolinkHeadings from 'rehype-autolink-headings';
|
|||
import rehypeSlug from 'rehype-slug';
|
||||
|
||||
// Build a lookup of post slugs to their last modification dates so the sitemap
|
||||
// can advertise accurate <lastmod> values to crawlers. We parse the markdown
|
||||
// frontmatter ourselves rather than importing `astro:content` (a virtual module
|
||||
// that may not be available inside the config). Failures are non-fatal —
|
||||
// sitemap entries simply fall back to no lastmod.
|
||||
const postLastmodLookup = new Map();
|
||||
try {
|
||||
const postsDir = path.resolve(
|
||||
path.dirname(fileURLToPath(import.meta.url)),
|
||||
'src/content/posts'
|
||||
);
|
||||
for (const entry of readdirSync(postsDir, { withFileTypes: true })) {
|
||||
if (!entry.isFile() || !entry.name.endsWith('.md')) continue;
|
||||
const slug = entry.name.replace(/\.md$/, '');
|
||||
const raw = readFileSync(path.join(postsDir, entry.name), 'utf8');
|
||||
const frontmatterMatch = raw.match(/^---\r?\n([\s\S]*?)\r?\n---/);
|
||||
if (!frontmatterMatch) continue;
|
||||
const frontmatter = frontmatterMatch[1];
|
||||
const updatedMatch = frontmatter.match(/^updated:\s*(.+?)\s*$/m);
|
||||
const dateMatch = frontmatter.match(/^date:\s*(.+?)\s*$/m);
|
||||
const rawDate = (updatedMatch ?? dateMatch)?.[1]?.replace(/^['"]|['"]$/g, '');
|
||||
if (!rawDate) continue;
|
||||
const parsed = new Date(rawDate);
|
||||
if (!Number.isNaN(parsed.valueOf())) postLastmodLookup.set(slug, parsed);
|
||||
}
|
||||
} catch {
|
||||
// Directory missing or unreadable; sitemap will fall back to no lastmod.
|
||||
// can advertise accurate <lastmod> values to crawlers. astro:content isn't
|
||||
// available inside the config, so we read post frontmatter directly. Our posts
|
||||
// always use single-line scalar `date:` / `updated:` keys, so a small regex
|
||||
// extraction is sufficient and intentional.
|
||||
const postsDir = path.resolve(
|
||||
path.dirname(fileURLToPath(import.meta.url)),
|
||||
'src/content/posts'
|
||||
);
|
||||
|
||||
function extractScalar(frontmatter, key) {
|
||||
const match = frontmatter.match(new RegExp(`^${key}:\\s*(.+?)\\s*$`, 'm'));
|
||||
return match?.[1]?.replace(/^['"]|['"]$/g, '');
|
||||
}
|
||||
|
||||
const postLastmodLookup = new Map(
|
||||
readdirSync(postsDir, { withFileTypes: true })
|
||||
.filter((entry) => entry.isFile() && entry.name.endsWith('.md'))
|
||||
.map((entry) => {
|
||||
const raw = readFileSync(path.join(postsDir, entry.name), 'utf8');
|
||||
const frontmatter = raw.match(/^---\r?\n([\s\S]*?)\r?\n---/)?.[1] ?? '';
|
||||
const rawDate =
|
||||
extractScalar(frontmatter, 'updated') ?? extractScalar(frontmatter, 'date');
|
||||
const parsed = rawDate ? new Date(rawDate) : null;
|
||||
const valid = parsed && !Number.isNaN(parsed.valueOf()) ? parsed : null;
|
||||
return [entry.name.replace(/\.md$/, ''), valid];
|
||||
})
|
||||
.filter(([, date]) => date !== null)
|
||||
);
|
||||
|
||||
export default defineConfig({
|
||||
site: 'https://schmelczer.dev',
|
||||
trailingSlash: 'always',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue