Skip to content

Commit

Permalink
Don't create routes for drafts (#53)
Browse files Browse the repository at this point in the history
* Don't create routes for drafts

* Support 'false' for draft
  • Loading branch information
zephraph authored Sep 16, 2024
1 parent acb404f commit 28ee58e
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/pages/api/[...path].ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,31 @@ const app = new Hono<{ Bindings: AstroContext }>()
.post("/notes/:id", async (c) => {
const id = c.req.param("id");
const props: Record<string, any> = c.req.query();
let slug = props.slug || slugify(props.title);
if (isULID(slug)) {
throw new Error("Title or H1s should not be a ULID");
}
const draft = props.draft ? props.draft === "true" : false;
if (!draft && !props.homepage) {
let slug = props.slug || slugify(props.title);
if (isULID(slug)) {
throw new Error("Title or H1s should not be a ULID");
}

const primaryTag = props.tags?.split(",")[0];
// Set the prefix URL to the first tag (or it's mapped value) if it exists
if (primaryTag && primaryTag !== slug) {
slug = `${
tagMap[primaryTag as keyof typeof tagMap] ?? props.tags[0]
}/${slug}`;
}
await c.env.KV_MAPPINGS.get(slug).then((mappedID) => {
if (mappedID && mappedID !== id) {
throw new Error(`Slug ${slug} is already mapped to ${mappedID}`);
const primaryTag = props.tags?.split(",")[0];
// Set the prefix URL to the first tag (or it's mapped value) if it exists
if (primaryTag && primaryTag !== slug) {
slug = `${
tagMap[primaryTag as keyof typeof tagMap] ?? props.tags[0]
}/${slug}`;
}
return Promise.all([
c.env.KV_MAPPINGS.put(slug, id),
c.env.KV_MAPPINGS.put(id, slug),
]);
});
await c.env.KV_MAPPINGS.get(slug).then((mappedID) => {
if (mappedID && mappedID !== id) {
throw new Error(`Slug ${slug} is already mapped to ${mappedID}`);
}
return Promise.all([
c.env.KV_MAPPINGS.put(slug, id),
c.env.KV_MAPPINGS.put(id, slug),
]);
});
}

const result = await c.env.R2_BUCKET.put(id, await c.req.blob(), {
onlyIf: { etagDoesNotMatch: c.req.header("If-None-Match") },
});
Expand Down

0 comments on commit 28ee58e

Please sign in to comment.