feat: auto excerpt

This commit is contained in:
EGOIST
2022-05-13 21:20:41 +08:00
parent 84f201d9e4
commit b8f5e6d934
9 changed files with 36 additions and 16 deletions
@@ -0,0 +1,9 @@
/*
Warnings:
- Added the required column `autoExcerpt` to the `pages` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "pages" ADD COLUMN "autoExcerpt" TEXT NOT NULL,
ALTER COLUMN "excerpt" DROP NOT NULL;
+2 -1
View File
@@ -111,7 +111,8 @@ model Page {
type PageType @default(POST)
title String
content String
excerpt String
excerpt String?
autoExcerpt String
format String @default("markdown")
slug String
site Site @relation(name: "SitePages", fields: [siteId], references: [id], onDelete: Cascade)
+8 -8
View File
@@ -13,14 +13,14 @@ export const SiteArchives: React.FC<{
<div className="mt-5">
{posts.map((post) => {
return (
<div key={post.id} className="flex">
<span className="text-zinc-400 mr-3">
{formatDate(post.publishedAt)}
</span>
<Link href={`/${post.slug}`}>
<a className="flex text-accent hover:underline">{post.title}</a>
</Link>
</div>
<Link key={post.id} href={`/${post.slug}`}>
<a className="flex justify-between items-center p-1 px-2 rounded-lg -mx-2 hover:bg-zinc-50">
<span className="text-accent">{post.title}</span>
<span className="text-zinc-400 mr-3 text-sm whitespace-nowrap">
{formatDate(post.publishedAt)}
</span>
</a>
</Link>
)
})}
</div>
+1 -1
View File
@@ -20,7 +20,7 @@ export const SiteHome: React.FC<{
{formatDate(post.publishedAt)}
</div>
<div className="mt-3 text-zinc-500">
{post.excerpt}
{post.excerpt || post.autoExcerpt}
{"..."}
</div>
</div>
+2 -2
View File
@@ -63,7 +63,7 @@ const stripHTML = (html: string) => {
return html.replace(/<(?:.|\n)*?>/gm, "")
}
export const getExcerpt = (content: string) => {
export const getAutoExcerpt = (content: string) => {
const indexOfMore = content.indexOf("<!--more-->")
const md = new Markdown({
html: false,
@@ -73,5 +73,5 @@ export const getExcerpt = (content: string) => {
content = content.substring(0, indexOfMore)
return stripHTML(md.render(content))
}
return stripHTML(md.render(content)).slice(0, 200)
return stripHTML(md.render(content)).slice(0, 400)
}
+2 -1
View File
@@ -28,7 +28,8 @@ export enum PageVisibilityEnum {
export type PostOnSiteHome = {
id: string
title: string
excerpt: string
excerpt?: string | null
autoExcerpt: string
slug: string
publishedAt: string | Date
}
+7 -2
View File
@@ -3,7 +3,7 @@ import { nanoid } from "nanoid"
import { prisma, Prisma } from "~/lib/db.server"
import { type Gate } from "~/lib/gate.server"
import { sendEmailForNewPost } from "~/lib/mailgun.server"
import { renderPageContent } from "~/lib/markdown.server"
import { getAutoExcerpt, renderPageContent } from "~/lib/markdown.server"
import { notFound } from "~/lib/server-side-props"
import { PageVisibilityEnum } from "~/lib/types"
import { isUUID } from "~/lib/uuid"
@@ -78,6 +78,7 @@ export async function createOrUpdatePage(
},
content: "",
excerpt: "",
autoExcerpt: "",
},
include: {
site: true,
@@ -95,6 +96,8 @@ export async function createOrUpdatePage(
const slug = input.slug || page.slug
await checkPageSlug({ slug, excludePage: page.id, siteId: page.siteId })
const autoExcerpt = input.content ? getAutoExcerpt(input.content) : undefined
const updated = await prisma.page.update({
where: {
id: page.id,
@@ -107,6 +110,7 @@ export async function createOrUpdatePage(
excerpt: input.excerpt,
slug,
type: input.isPost ? "POST" : "PAGE",
autoExcerpt,
},
})
@@ -132,6 +136,7 @@ export async function getPagesBySite(
visibility?: PageVisibilityEnum | null
take?: number | null
cursor?: string | null
autoExcerpt?: boolean
}
) {
const site = await getSite(input.site)
@@ -187,7 +192,7 @@ export async function getPagesBySite(
const hasMore = nodes.length > take
return {
nodes,
nodes: nodes,
total,
hasMore,
}
+1
View File
@@ -218,6 +218,7 @@ export async function createSite(
title: "About",
slug: "about",
excerpt: "",
autoExcerpt: "",
content: `My name is ${payload.name} and I'm a new site.`,
published: true,
publishedAt: new Date(),
+4 -1
View File
@@ -79,7 +79,8 @@ export const siteRouter = createRouter()
publishedAt: z.date().transform((v) => v.toISOString()),
published: z.boolean(),
slug: z.string(),
excerpt: z.string(),
excerpt: z.string().nullable(),
autoExcerpt: z.string(),
})
),
total: z.number(),
@@ -100,6 +101,8 @@ export const siteRouter = createRouter()
id: z.string(),
title: z.string(),
content: z.string(),
excerpt: z.string().nullable(),
autoExcerpt: z.string(),
published: z.boolean(),
publishedAt: z.date().transform((v) => v.toISOString()),
slug: z.string(),