Skip to content

Commit

Permalink
extract direct from slug field
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Jan 31, 2024
1 parent 0d4ab5d commit ae9604a
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,36 @@ import {themes as prismThemes} from 'prism-react-renderer';

import fs from 'fs';

/** the routes of pages in src/pages dir */
const srcPagesRoutes =
fs.readdirSync('src/pages')
.filter(f => f.endsWith('.md') || f.endsWith('.mdx'))
.map(f => f.replace(/\.mdx?$/, ''))
.map(f => '/' + f);
import { parseFileContentFrontMatter } from '@docusaurus/utils/lib/markdownUtils';

/** Return a Map of routes to directs, derived from `slug` field in front-matter.*/
const slugsInDir = (dir) => {
/** the routes of pages in dir, array of [route, path] */
const _srcPagesRoutes =
fs.readdirSync(dir)
.filter(f => f.endsWith('.md') || f.endsWith('.mdx'))
.map(f => [`/${f.replace(/\.mdx?$/, '')}`, `${dir}/${f}`])

/** the slugs of routes in dir, array of [route, slug?] */
const _routeSlugs = _srcPagesRoutes.map(r => {
const [route, path] = r;
const {frontMatter} = parseFileContentFrontMatter(fs.readFileSync(path, 'utf8'));
return [route, frontMatter['slug']];
});

/** the map of routes to valid slugs */
const routeSlugsMap = new Map();
_routeSlugs.forEach(r => {
const [route, slug] = r;
if (slug !== undefined) {
routeSlugsMap.set(route, slug);
}
});
return routeSlugsMap;
}

const pagesRedirects = slugsInDir('src/pages');


/** @type {import('@docusaurus/types').Config} */
const config = {
Expand Down Expand Up @@ -164,8 +188,8 @@ const config = {
{
createRedirects(existingPath) {
// create download.html, learn.html, community.html
if (srcPagesRoutes.includes(existingPath)) {
return [existingPath + '.html'];
if (pagesRedirects.has(existingPath)) {
return [pagesRedirects.get(existingPath)];
}
return undefined;
}
Expand Down

0 comments on commit ae9604a

Please sign in to comment.