mirror of
https://github.com/kremalicious/blog.git
synced 2024-06-28 08:37:57 +02:00
redirect_from solution
This commit is contained in:
parent
f713f5f381
commit
610dbde823
|
@ -2,6 +2,7 @@ import { defineConfig } from 'astro/config'
|
||||||
import remarkLeadParagraph from './src/lib/remark-lead-paragraph.mjs'
|
import remarkLeadParagraph from './src/lib/remark-lead-paragraph.mjs'
|
||||||
import remarkToc from './src/lib/remark-toc.mjs'
|
import remarkToc from './src/lib/remark-toc.mjs'
|
||||||
import react from '@astrojs/react'
|
import react from '@astrojs/react'
|
||||||
|
import redirects from './redirects.json'
|
||||||
|
|
||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
/** @type {import('astro/config').AstroUserConfig} */
|
/** @type {import('astro/config').AstroUserConfig} */
|
||||||
|
@ -17,6 +18,7 @@ export default defineConfig({
|
||||||
wrap: true
|
wrap: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
redirects,
|
||||||
vite: {
|
vite: {
|
||||||
resolve: {
|
resolve: {
|
||||||
// for making content -> src/content symlink work
|
// for making content -> src/content symlink work
|
||||||
|
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -14,4 +14,5 @@ src/images/icons/
|
||||||
.astro/
|
.astro/
|
||||||
|
|
||||||
# autogenerated stuff
|
# autogenerated stuff
|
||||||
public/get/
|
public/get/
|
||||||
|
.config/redirects.json
|
|
@ -7,7 +7,7 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"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",
|
"start": "npm run prebuild && astro dev --config .config/astro.config.mjs",
|
||||||
"build": "npm run prebuild && astro build --config .config/astro.config.mjs",
|
"build": "npm run prebuild && astro build --config .config/astro.config.mjs",
|
||||||
"preview": "astro preview",
|
"preview": "astro preview",
|
||||||
|
|
74
scripts/redirect-from.ts
Normal file
74
scripts/redirect-from.ts
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user