Skip to content

Commit

Permalink
Improve sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
cdedreuille committed Jul 11, 2024
1 parent db747c0 commit bca0f38
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 55 deletions.
18 changes: 16 additions & 2 deletions apps/frontpage/app/docs-all/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getFlatTreeSitemap,
} from '../../lib/get-flat-tree-sitemap';
import { getAllTrees } from '../../lib/get-all-trees';
import { docsVersions } from '@repo/utils';
import { docsVersions, latestVersion } from '@repo/utils';
import { list } from 'tar';

export default function sitemap(): MetadataRoute.Sitemap {
// Generate docs tree for each version
Expand All @@ -27,9 +28,22 @@ export default function sitemap(): MetadataRoute.Sitemap {
url: `https://storybook.js.org${node.slug}`,
}));

// Remove https://storybook.js.org/docs/get-started as we are redirecting to https://storybook.js.org/docs
const filteredDocsUrls = docsUrls.filter(
(node) => node.url !== 'https://storybook.js.org/docs/get-started',
);

const docsHomeUrls = docsVersions.map((version) => ({
url:
version.id === latestVersion.id
? 'https://storybook.js.org/docs'
: `https://storybook.js.org/docs/${version.inSlug || version.id}`,
}));

return [
{ url: 'https://storybook.js.org' },
{ url: 'https://storybook.js.org/community' },
...docsUrls,
...docsHomeUrls,
...filteredDocsUrls,
];
}
8 changes: 7 additions & 1 deletion apps/frontpage/app/docs/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ export default function sitemap(): MetadataRoute.Sitemap {
}))
: [];

// Remove https://storybook.js.org/docs/get-started as we are redirecting to https://storybook.js.org/docs
const filteredDocsUrls = docsUrls.filter(
(node) => node.url !== 'https://storybook.js.org/docs/get-started',
);

return [
{ url: 'https://storybook.js.org' },
{ url: 'https://storybook.js.org/community' },
...docsUrls,
{ url: 'https://storybook.js.org/docs' },
...filteredDocsUrls,
];
}
13 changes: 12 additions & 1 deletion apps/frontpage/lib/get-all-trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ const addSlugToNode = (node: RawTreeProps): TreeProps => {

export const getAllTrees = () => {
const rawTree = getDocsTreeFromPath();
const transformedTree = rawTree.map(addSlugToNode);

// Remove versions from all trees
// This is a temporary solution until we have a better way to handle versions.
// Ideally the versions folder should not be part of the docs.
const treewithoutVersion = rawTree.map((tree) => {
const treeChildren = tree.children?.filter(
(child) => child.name !== 'versions',
);
return { ...tree, children: treeChildren };
});

const transformedTree = treewithoutVersion.map(addSlugToNode);

return transformedTree;
};
96 changes: 45 additions & 51 deletions apps/frontpage/lib/get-docs-tree-from-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ function getMetadata(filePath: string): Metadata {
};
}

function shouldParse(fileOrPath: string) {
return !fileOrPath.endsWith('/versions');
}

export const getDocsTreeFromPath = (
pathToFiles?: string,
docsRoot?: string,
Expand All @@ -33,58 +29,56 @@ export const getDocsTreeFromPath = (
const files = fs.readdirSync(path.join(process.cwd(), newPath));
const tree: RawTreeProps[] = [];

if (shouldParse(newPath)) {
files.forEach((file) => {
const filePath = path.join(newPath, file);
const isDirectory = fs.lstatSync(filePath).isDirectory();
files.forEach((file) => {
const filePath = path.join(newPath, file);
const isDirectory = fs.lstatSync(filePath).isDirectory();

if (isDirectory) {
const childItems = getDocsTreeFromPath(filePath, newDocsRoot);
if (isDirectory) {
const childItems = getDocsTreeFromPath(filePath, newDocsRoot);

if (childItems) {
const indexFile = childItems.find(
(item) => item.name === 'index.mdx' || item.name === 'index.md',
);
const children = childItems
.sort((a, b) =>
a.sidebar?.order && b.sidebar?.order
? a.sidebar.order - b.sidebar.order
: 0,
)
.filter((item) => item.name !== 'index.mdx')
.filter((item) => item.name !== 'index.md');
const isTab = indexFile?.isTab || false;
if (childItems) {
const indexFile = childItems.find(
(item) => item.name === 'index.mdx' || item.name === 'index.md',
);
const children = childItems
.sort((a, b) =>
a.sidebar?.order && b.sidebar?.order
? a.sidebar.order - b.sidebar.order
: 0,
)
.filter((item) => item.name !== 'index.mdx')
.filter((item) => item.name !== 'index.md');
const isTab = indexFile?.isTab || false;

if (indexFile) {
tree.push({
...indexFile,
name: file,
pathSegment: filePath,
type: 'directory',
children: isTab ? [] : children,
});
} else {
tree.push({
title: 'No title',
name: file,
pathSegment: filePath,
type: 'directory',
children,
});
}
if (indexFile) {
tree.push({
...indexFile,
name: file,
pathSegment: filePath,
type: 'directory',
children: isTab ? [] : children,
});
} else {
tree.push({
title: 'No title',
name: file,
pathSegment: filePath,
type: 'directory',
children,
});
}
} else if (file.endsWith('.mdx') || file.endsWith('.md')) {
const metaData = getMetadata(filePath);

tree.push({
name: file,
pathSegment: filePath,
type: 'link',
...metaData,
});
}
});
}
} else if (file.endsWith('.mdx') || file.endsWith('.md')) {
const metaData = getMetadata(filePath);

tree.push({
name: file,
pathSegment: filePath,
type: 'link',
...metaData,
});
}
});

return tree.sort((a, b) =>
a.sidebar?.order && b.sidebar?.order
Expand Down

0 comments on commit bca0f38

Please sign in to comment.