Skip to content

Commit

Permalink
chore(website): add sitemap to website
Browse files Browse the repository at this point in the history
  • Loading branch information
sdorra committed May 11, 2024
1 parent 7eed25d commit 924e45e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
2 changes: 2 additions & 0 deletions website/app/robots.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FQDN } from "@/lib/env";
import { MetadataRoute } from "next";

export default function robots(): MetadataRoute.Robots {
Expand All @@ -6,5 +7,6 @@ export default function robots(): MetadataRoute.Robots {
userAgent: "*",
allow: "/",
},
sitemap: `${FQDN}/sitemap.xml`,
};
}
28 changes: 28 additions & 0 deletions website/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { FQDN } from "@/lib/env";
import { allDocs, allQuickstarts, allSamples } from "content-collections";
import { MetadataRoute } from "next";

export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: `${FQDN}/`,
lastModified: new Date(),
priority: 1,
},
...allQuickstarts.map((quickstart) => ({
url: `${FQDN}/${quickstart.href}`,
lastModified: quickstart.lastModified,
priority: 0.8,
})),
...allDocs.map((docPage) => ({
url: `${FQDN}/${docPage.href}`,
lastModified: docPage.lastModified,
priority: 0.7,
})),
...allSamples.map((sample) => ({
url: `${FQDN}/${sample.href}`,
lastModified: sample.lastModified,
priority: 0.7,
})),
];
}
37 changes: 35 additions & 2 deletions website/content-collections.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { defineCollection, defineConfig } from "@content-collections/core";
import {
Context,
Meta,
defineCollection,
defineConfig,
Document,
} from "@content-collections/core";
import rehypeSlug from "rehype-slug";
import rehypeShiki from "@shikijs/rehype";
import { Options, compileMDX } from "@content-collections/mdx";
import { selectAll } from "hast-util-select";
import { Root } from "hast";
import GithubSlugger from "github-slugger";
import { exec as cpExec } from "node:child_process";
import { promisify } from "node:util";
import path from "node:path";

const exec = promisify(cpExec);

function liCodeSlug() {
return (tree: Root) => {
Expand All @@ -20,6 +31,26 @@ function liCodeSlug() {
};
}

async function lastModificationDate(ctx: Context, document: Document) {
return ctx.cache(
// TODO: this is a dirty hack to avoid cache key conflicts
// we should find a way which handles this automatically
{ key: "_git_last_modified", ...document },
async (document) => {
const filePath = path.join(
ctx.collection.directory,
document._meta.filePath
);

const { stdout } = await exec(`git log -1 --format=%ai -- ${filePath}`);
if (stdout) {
return new Date(stdout.trim()).toISOString();
}
return new Date().toISOString();
}
);
}

const mdxOptions: Options = {
rehypePlugins: [
liCodeSlug,
Expand Down Expand Up @@ -57,6 +88,7 @@ const quickstart = defineCollection({
name,
body,
category: data.category,
lastModified: await lastModificationDate(ctx, data),
};
},
});
Expand Down Expand Up @@ -89,11 +121,11 @@ const samples = defineCollection({
name,
body,
tags: data.tags,
lastModified: await lastModificationDate(ctx, data),
};
},
});


const docs = defineCollection({
name: "docs",
directory: "../docs",
Expand Down Expand Up @@ -126,6 +158,7 @@ const docs = defineCollection({
body,
href,
slug,
lastModified: await lastModificationDate(ctx, data),
};
},
});
Expand Down
1 change: 1 addition & 0 deletions website/lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FQDN = "https://www.content-collections.dev";

0 comments on commit 924e45e

Please sign in to comment.