Skip to content

Commit

Permalink
New byte syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Sep 18, 2023
1 parent cf6b9be commit 46c614e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
10 changes: 5 additions & 5 deletions app/api/bytes/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { PushEvent } from "@octokit/webhooks-types"
import { upsertByte } from "lib/api/bytes"
import { getByteSource } from "lib/api/github"
import { verifySignature } from "lib/api/signature"
import { toSlug } from "lib/parser"
import { toId } from "lib/parser"
import prisma from "lib/prisma"

async function upsert(file: string) {
const slug = toSlug(file)
return upsertByte(slug, await getByteSource(slug))
const id = toId(file)
return upsertByte(id, await getByteSource(id))
}

async function remove(files: string[]) {
const slugs = files.map(toSlug)
const ids = files.map(toId)

await prisma.byte.deleteMany({
where: { slug: { in: slugs } },
where: { id: { in: ids } },
})
}

Expand Down
18 changes: 9 additions & 9 deletions app/api/reindex/route.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NextResponse } from "next/server"
import { upsertByte } from "lib/api/bytes"
import { getByteSource, octokit } from "lib/api/github"
import { toSlug } from "lib/parser"
import { toId } from "lib/parser"
import prisma from "lib/prisma"

async function getAllByteSlugs() {
async function getAllByteIds() {
const path = "bytes"
const { data } = await octokit.repos.getContent({
owner: "mskelton",
Expand All @@ -18,7 +18,7 @@ async function getAllByteSlugs() {
)
}

return data.map((item) => toSlug(item.name))
return data.map((item) => toId(item.name))
}

export async function POST() {
Expand All @@ -29,17 +29,17 @@ export async function POST() {
)
}

// Prep the reindexing before clearing content. Make sure we get all the content
// from the GitHub API before we start clearing the database.
const slugs = await getAllByteSlugs()
const sources = await Promise.all(slugs.map(getByteSource))
// Prep the reindexing before clearing content. Make sure we get all the
// content from the GitHub API before we start clearing the database.
const ids = await getAllByteIds()
const sources = await Promise.all(ids.map(getByteSource))

// Clear all bytes from the database
await prisma.byte.deleteMany()

// Add all bytes to the database
for (let i = 0; i < slugs.length; i++) {
await upsertByte(slugs[i], sources[i])
for (let i = 0; i < ids.length; i++) {
await upsertByte(ids[i], sources[i])
}

return NextResponse.json({ message: "ok" })
Expand Down
20 changes: 14 additions & 6 deletions app/lib/api/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { parseDate } from "lib/date"
import { getFrontmatter, parseDescription } from "lib/parser"
import {
dateFromId,
getFrontmatter,
parseDescription,
slugify,
} from "lib/parser"
import prisma from "lib/prisma"

export async function upsertByte(slug: string, source: string) {
export async function upsertByte(id: string, source: string) {
const { content, meta } = getFrontmatter(source)

const data = {
content: Buffer.from(content, "utf-8"),
createdAt: parseDate(meta.date),
createdAt: dateFromId(id),
description: await parseDescription(content),
tags: {
connectOrCreate: meta.tags.map((tag) => ({
Expand All @@ -19,8 +23,12 @@ export async function upsertByte(slug: string, source: string) {
}

await prisma.byte.upsert({
create: { ...data, slug },
create: {
...data,
id,
slug: slugify(meta.title),
},
update: data,
where: { slug },
where: { id },
})
}
4 changes: 2 additions & 2 deletions app/lib/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ octokit.hook.error("request", async (error) => {
throw error
})

export async function getByteSource(slug: string) {
const path = `bytes/${slug}.md`
export async function getByteSource(id: string) {
const path = `bytes/${id}.md`
const { data } = await octokit.repos.getContent({
mediaType: { format: "raw" },
owner: "mskelton",
Expand Down
22 changes: 21 additions & 1 deletion app/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@ import { Plugin, unified } from "unified"
import { ByteMeta } from "(main)/bytes/types"
import remarkStringify from "../../config/remark-stringify.mjs"

export const toSlug = (file: string) =>
export const slugify = (str: string) =>
str
.toLowerCase()
.replace(/[^a-z0-9-]+/g, "-")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "")

export const toId = (file: string) =>
file.replace(/\.md$/, "").split("/").at(-1) ?? ""

export function dateFromId(id: string) {
return new Date(
Date.UTC(
+id.slice(0, 4),
+id.slice(4, 6) - 1,
+id.slice(6, 8),
+id.slice(8, 10),
+id.slice(10, 12),
+id.slice(12, 14),
),
)
}

export function getFrontmatter(source: string) {
const { content, data } = matter(source)
const meta = data as ByteMeta
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ generator client {
}

model Byte {
id Int @id @default(autoincrement())
id String @id
slug String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down

0 comments on commit 46c614e

Please sign in to comment.