1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 14:06:40 -04:00

generate permalinks when parsing note

This commit is contained in:
2022-01-03 16:26:20 -05:00
parent 6d81939b8b
commit d2b71887b4
5 changed files with 16 additions and 111 deletions

View File

@ -1,5 +1,4 @@
import * as Sentry from "@sentry/node";
import * as config from "../../lib/config";
import { getAllNotes } from "../../lib/parse-notes";
import pRetry from "p-retry";
import faunadb from "faunadb";
@ -101,7 +100,7 @@ const getSiteStats = async (client) => {
const match: any = notes.find((note) => `notes/${note.slug}` === page.slug);
if (match) {
page.title = match.title;
page.url = `${config.baseUrl}/${page.slug}/`;
page.url = match.permalink;
page.date = match.date;
}

View File

@ -1,95 +0,0 @@
import * as Sentry from "@sentry/node";
import { graphql } from "@octokit/graphql";
import type { NextApiRequest, NextApiResponse } from "next";
Sentry.init({
dsn: process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV || "",
});
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method !== "GET") {
return res.status(405).send(""); // 405 Method Not Allowed
}
// allow custom limit, max. 24 results
let limit = 24;
if (parseInt(req.query.limit as string) > 0 && parseInt(req.query.limit as string) < limit) {
limit = parseInt(req.query.limit as string);
}
const result = await fetchRepos(req.query.sort === "top" ? "STARGAZERS" : "PUSHED_AT", limit);
// let Vercel edge cache results for 15 mins
res.setHeader("Cache-Control", "s-maxage=900, stale-while-revalidate");
return res.status(200).json(result);
} catch (error) {
console.error(error);
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);
const message = error instanceof Error ? error.message : "Unknown error.";
// 500 Internal Server Error
return res.status(500).json({ success: false, message });
}
};
const fetchRepos = async (sort, limit) => {
// https://docs.github.com/en/graphql/reference/objects#repository
const { user } = await graphql(
`
query ($username: String!, $sort: String, $limit: Int) {
user(login: $username) {
repositories(
first: $limit
isLocked: false
isFork: false
ownerAffiliations: OWNER
privacy: PUBLIC
orderBy: { field: $sort, direction: DESC }
) {
edges {
node {
name
url
description
pushedAt
stargazerCount
forkCount
primaryLanguage {
name
color
}
}
}
}
}
}
`,
{
username: "jakejarvis",
limit,
sort,
headers: {
authorization: `token ${process.env.GH_PUBLIC_TOKEN}`,
},
}
);
return user.repositories.edges.map(({ node: repo }) => ({
name: repo.name,
url: repo.url,
description: repo.description,
updatedAt: new Date(repo.pushedAt),
stars: repo.stargazerCount,
forks: repo.forkCount,
language: repo.primaryLanguage,
}));
};
export default handler;

View File

@ -20,15 +20,15 @@ import rehypeExternalLinks from "rehype-external-links";
import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
const Note = ({ source, frontMatter, slug }) => (
const Note = ({ frontMatter, source }) => (
<>
<NextSeo
title={frontMatter.title}
description={frontMatter.description}
canonical={`${config.baseUrl}/notes/${slug}/`}
canonical={frontMatter.permalink}
openGraph={{
title: frontMatter.title,
url: `${config.baseUrl}/notes/${slug}/`,
url: frontMatter.permalink,
type: "article",
article: {
publishedTime: frontMatter.date,
@ -47,7 +47,7 @@ const Note = ({ source, frontMatter, slug }) => (
}}
/>
<ArticleJsonLd
url={`${config.baseUrl}/notes/${slug}/`}
url={frontMatter.permalink}
title={frontMatter.title}
description={frontMatter.description}
datePublished={frontMatter.date}
@ -60,7 +60,7 @@ const Note = ({ source, frontMatter, slug }) => (
<Layout>
<Container>
<Meta {...frontMatter} slug={slug} />
<Meta {...frontMatter} />
<Content>
<div className="markdown">
<MDXRemote {...source} components={mdxComponents} />
@ -73,9 +73,8 @@ const Note = ({ source, frontMatter, slug }) => (
export const getStaticProps: GetStaticProps = async ({ params }) => {
const filePath = path.join(process.cwd(), config.NOTES_DIR, `${params.slug}.mdx`);
const source = fs.readFileSync(filePath);
const { content, data } = matter(source);
const rawSource = fs.readFileSync(filePath);
const { data, content } = matter(rawSource);
const mdxSource = await serialize(content, {
scope: data,
@ -97,10 +96,11 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
props: {
frontMatter: {
...data,
slug: params.slug,
permalink: `${config.baseUrl}/notes/${params.slug}/`,
date: parseISO(data.date).toISOString(), // validate/normalize the date string provided from front matter
},
source: mdxSource,
slug: params.slug,
},
};
};