Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
docs/.vuepress/.cache/
docs/.vuepress/.temp/
docs/.vuepress/dist/
docs/.vuepress/public/_redirects
3 changes: 3 additions & 0 deletions docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import theme from "./theme";
import { path } from 'vuepress/utils';
import { viteBundler } from '@vuepress/bundler-vite';
import { registerComponentsPlugin } from '@vuepress/plugin-register-components'
import { generateShortLinkRedirects } from "./utils/ShortLinkHelper";

generateShortLinkRedirects();

export default defineUserConfig({
lang: "zh-CN",
Expand Down
39 changes: 39 additions & 0 deletions docs/.vuepress/utils/ShortLinkHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'node:fs';
import path from 'node:path';
import matter from 'gray-matter';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const postsDir = path.resolve(__dirname, '../../posts');
const publicDir = path.resolve(__dirname, '../public');

const VOL_PATTERN = /Vol\.\s*(\S+)/;

function normalizeVolId(raw: string): string {
if (/^\d+$/.test(raw)) return String(parseInt(raw, 10));
if (raw.toLowerCase() === 'sp') return '0';
return raw.toLowerCase();
}

export function generateShortLinkRedirects(): void {
const folders = fs.readdirSync(postsDir).filter((item) => {
return fs.statSync(path.join(postsDir, item)).isDirectory();
});
folders.sort();

const lines: string[] = [];
for (const folder of folders) {
const readmePath = path.join(postsDir, folder, 'README.md');
if (!fs.existsSync(readmePath)) continue;

const { data } = matter(fs.readFileSync(readmePath, 'utf-8'));
const match = typeof data.title === 'string' ? VOL_PATTERN.exec(data.title) : null;
if (!match) continue;

const volId = normalizeVolId(match[1]);
lines.push(`/n/${volId} /posts/${folder}/ 301`);
}

fs.writeFileSync(path.join(publicDir, '_redirects'), lines.join('\n') + '\n');
}
Loading