feat: compatible with old non-standard slugs

This commit is contained in:
DIYgod 2023-05-16 12:43:05 +01:00
parent d3ced8be45
commit 095257ad5a
No known key found for this signature in database
5 changed files with 13 additions and 25 deletions

View File

@ -7,7 +7,7 @@ const execSync = require("child_process").execSync
const cache = require("@ducanh2912/next-pwa").runtimeCaching
const withPWA = require("@ducanh2912/next-pwa").default({
// disable: process.env.NODE_ENV === "development",
disable: process.env.NODE_ENV === "development",
dest: "public",
publicExcludes: ["*"],
buildExcludes: [

View File

@ -2,8 +2,7 @@ import { NoteEntity } from "crossbell.js"
import { redirect } from "next/navigation"
import { IS_VERCEL_PREVIEW } from "~/lib/constants"
import { getDefaultSlug } from "~/lib/default-slug"
import { getSiteLink } from "~/lib/helpers"
import { getNoteSlug, getSiteLink } from "~/lib/helpers"
import { NextServerResponse, getQuery } from "~/lib/server-helper"
import { checkDomainServer } from "~/models/site.model"
@ -63,14 +62,7 @@ export async function GET(req: Request): Promise<Response> {
})
if (note) {
const slug =
note?.metadata?.content?.attributes?.find(
(a: any) => a.trait_type === "xlog_slug",
)?.value ||
getDefaultSlug(
note?.metadata?.content?.title || "",
`${characterId}-${noteId}`,
)
const slug = getNoteSlug(note)
link += `/${encodeURIComponent(slug)}`

View File

@ -1,4 +1,4 @@
import { getSiteLink } from "~/lib/helpers"
import { getNoteSlug, getSiteLink } from "~/lib/helpers"
import { NextServerResponse } from "~/lib/server-helper"
import { getCommentsBySite, getSite } from "~/models/site.model"
@ -49,11 +49,7 @@ export async function GET(
id: comment.characterId + "-" + comment.noteId,
title: `${name}: ${comment.metadata?.content?.content}`,
content_html: `${name} commented on ${type} ${toTitle}: ${comment.metadata?.content?.content}`,
url: `${link}/${
comment.toNote?.metadata?.content?.attributes?.find(
(attribute: any) => attribute.trait_type === "xlog_slug",
)?.value || comment.toNote?.characterId + "-" + comment.toNote?.noteId
}`,
url: `${link}/${comment.toNote ? getNoteSlug(comment.toNote) : ""}`,
date_published: comment.createdAt,
date_modified: comment.updatedAt,
}

View File

@ -5,6 +5,8 @@ import { SCORE_API_DOMAIN, SITE_URL } from "~/lib/env"
import { toCid, toGateway } from "~/lib/ipfs-parser"
import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
import { getNoteSlug } from "./helpers"
export const expandCrossbellNote = async (
note: NoteEntity,
useStat?: boolean,
@ -44,11 +46,7 @@ export const expandCrossbellNote = async (
expandedNote.metadata.content.audio = rendered.audio
expandedNote.metadata.content.frontMatter = rendered.frontMatter
}
expandedNote.metadata.content.slug = encodeURIComponent(
expandedNote.metadata.content.attributes?.find(
(a) => a.trait_type === "xlog_slug",
)?.value || "",
)
expandedNote.metadata.content.slug = getNoteSlug(expandedNote)
if (useStat) {
const stat = await (

View File

@ -42,12 +42,14 @@ export const getSlugUrl = (slug: string) => {
}
export const getNoteSlug = (note: NoteEntity) => {
return (
return encodeURIComponent(
note.metadata?.content?.attributes?.find(
(a) => a?.trait_type === "xlog_slug",
)?.value ||
(note.metadata?.content as any)?._xlog_slug ||
(note.metadata?.content as any)?._crosslog_slug
(note.metadata?.content as any)?._xlog_slug ||
(note.metadata?.content as any)?._crosslog_slug ||
note.metadata?.content?.title ||
`${note.characterId}-${note.noteId}`,
)?.toLowerCase?.()
}