1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-25 02:36:26 +02:00

redirect_from solution

This commit is contained in:
Matthias Kretschmann 2023-09-05 00:22:51 +01:00
parent f713f5f381
commit 610dbde823
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 79 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import { defineConfig } from 'astro/config'
import remarkLeadParagraph from './src/lib/remark-lead-paragraph.mjs'
import remarkToc from './src/lib/remark-toc.mjs'
import react from '@astrojs/react'
import redirects from './redirects.json'
// https://astro.build/config
/** @type {import('astro/config').AstroUserConfig} */
@ -17,6 +18,7 @@ export default defineConfig({
wrap: true
}
},
redirects,
vite: {
resolve: {
// for making content -> src/content symlink work

3
.gitignore vendored
View File

@ -14,4 +14,5 @@ src/images/icons/
.astro/
# autogenerated stuff
public/get/
public/get/
.config/redirects.json

View File

@ -7,7 +7,7 @@
"license": "MIT",
"type": "module",
"scripts": {
"prebuild": "npm run create:icons && npm run move:downloads",
"prebuild": "npm run create:icons && npm run create:redirects && npm run move:downloads",
"start": "npm run prebuild && astro dev --config .config/astro.config.mjs",
"build": "npm run prebuild && astro build --config .config/astro.config.mjs",
"preview": "astro preview",

74
scripts/redirect-from.ts Normal file
View File

@ -0,0 +1,74 @@
//
// astro-redirect-from
//
import fs from 'node:fs/promises'
import path from 'node:path'
import frontmatter from 'front-matter'
import ora from 'ora'
const contentDir = 'content/'
const outputFilePath = '.config/redirects.json'
let fileCount = 0
type Frontmatter = { redirect_from?: string[]; slug?: string }
const spinner = ora('Extract redirects').start()
async function findMarkdownFilesWithRedirects(
dir: string
): Promise<{ [old: string]: string }> {
const redirects: { [old: string]: string } = {}
async function processDir(currentDir: string) {
const items = await fs.readdir(currentDir, { recursive: true })
for (const item of items) {
const itemPath = path.join(currentDir, item)
const stats = await fs.stat(itemPath)
if (
stats.isFile() &&
item.endsWith('.md') &&
!itemPath.includes('links')
) {
const fileContent = await fs.readFile(itemPath, 'utf-8')
const { attributes }: { attributes: Frontmatter } =
frontmatter(fileContent)
// construct slug from frontmatter or folder name
const postSlug =
attributes.slug ||
itemPath.split('2')[1].split('/index.md')[0].substring(10)
// Check if the Markdown file has a redirect_from field
if (attributes.redirect_from) {
fileCount++
const redirectFromSlugs = attributes.redirect_from
for (const slug of redirectFromSlugs) {
// Add entries to the redirects object
redirects[slug] = postSlug
}
}
}
}
}
await processDir(dir)
return redirects
}
try {
const redirects = await findMarkdownFilesWithRedirects(contentDir)
const redirectsJSON = JSON.stringify(redirects, null, 2)
// Write the redirects object to the output file
fs.writeFile(outputFilePath, redirectsJSON, 'utf-8')
spinner.succeed(
`Extracted ${
Object.keys(redirects).length
} redirects from ${fileCount} files`
)
} catch (error: any) {
spinner.fail((error as Error).message)
}