|
| 1 | +import { collection, config, fields } from "@keystatic/core"; |
| 2 | + |
| 3 | +const isGitHubStorage = |
| 4 | + process.env.NEXT_PUBLIC_KEYSTATIC_STORAGE_KIND === "github" || |
| 5 | + Boolean(process.env.NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG); |
| 6 | + |
| 7 | +const categoryOptions = [ |
| 8 | + "Announcement", |
| 9 | + "Defense", |
| 10 | + "Education", |
| 11 | + "Presentation", |
| 12 | + "Publication", |
| 13 | + "Social", |
| 14 | + "Travel", |
| 15 | + "Video", |
| 16 | +].map((category) => ({ label: category, value: category })); |
| 17 | + |
| 18 | +function sanitizeAssetFilename(originalFilename: string): string { |
| 19 | + const extensionIndex = originalFilename.lastIndexOf("."); |
| 20 | + const hasExtension = extensionIndex > 0; |
| 21 | + const basename = hasExtension |
| 22 | + ? originalFilename.slice(0, extensionIndex) |
| 23 | + : originalFilename; |
| 24 | + const extension = hasExtension |
| 25 | + ? originalFilename.slice(extensionIndex).toLowerCase() |
| 26 | + : ""; |
| 27 | + const safeBasename = basename |
| 28 | + .normalize("NFKD") |
| 29 | + .replace(/[\u0300-\u036f]/g, "") |
| 30 | + .toLowerCase() |
| 31 | + .replace(/[^a-z0-9]+/g, "-") |
| 32 | + .replace(/^-+|-+$/g, ""); |
| 33 | + |
| 34 | + return `${safeBasename || "image"}${extension}`; |
| 35 | +} |
| 36 | + |
| 37 | +export default config({ |
| 38 | + storage: isGitHubStorage |
| 39 | + ? { |
| 40 | + kind: "github", |
| 41 | + repo: "PreferredAI/PreferredAI.github.io", |
| 42 | + } |
| 43 | + : { |
| 44 | + kind: "local", |
| 45 | + }, |
| 46 | + ui: { |
| 47 | + brand: { name: "Preferred.AI Publisher" }, |
| 48 | + navigation: ["posts"], |
| 49 | + }, |
| 50 | + collections: { |
| 51 | + posts: collection({ |
| 52 | + label: "Posts", |
| 53 | + slugField: "title", |
| 54 | + path: "content/posts/*", |
| 55 | + format: { contentField: "content" }, |
| 56 | + entryLayout: "content", |
| 57 | + columns: ["title", "date", "author"], |
| 58 | + previewUrl: "/blog/{slug}", |
| 59 | + schema: { |
| 60 | + title: fields.slug({ |
| 61 | + name: { |
| 62 | + label: "Title", |
| 63 | + validation: { isRequired: true }, |
| 64 | + }, |
| 65 | + }), |
| 66 | + date: fields.date({ |
| 67 | + label: "Publish date", |
| 68 | + defaultValue: { kind: "today" }, |
| 69 | + validation: { isRequired: true }, |
| 70 | + }), |
| 71 | + author: fields.text({ |
| 72 | + label: "Author", |
| 73 | + defaultValue: "Preferred.AI", |
| 74 | + validation: { isRequired: true }, |
| 75 | + }), |
| 76 | + excerpt: fields.text({ |
| 77 | + label: "Excerpt", |
| 78 | + multiline: true, |
| 79 | + description: |
| 80 | + "A one or two sentence summary used in post listings and as the default SEO description.", |
| 81 | + validation: { isRequired: true }, |
| 82 | + }), |
| 83 | + cover: fields.image({ |
| 84 | + label: "Featured image", |
| 85 | + description: |
| 86 | + "Choose or drop an image. Keystatic stores it in public/uploads/<post-slug>/cover.<ext>.", |
| 87 | + directory: "public/uploads", |
| 88 | + publicPath: "/uploads", |
| 89 | + }), |
| 90 | + featuredImage: fields.text({ |
| 91 | + label: "Existing featured image path (legacy)", |
| 92 | + description: |
| 93 | + "Existing posts may use an /uploads/YYYY/MM/... path. Leave this empty for new posts and use the image picker above.", |
| 94 | + }), |
| 95 | + categories: fields.multiselect({ |
| 96 | + label: "Categories", |
| 97 | + description: "Choose every category that applies.", |
| 98 | + options: categoryOptions, |
| 99 | + defaultValue: ["Education"], |
| 100 | + }), |
| 101 | + tags: fields.array(fields.text({ label: "Tag" }), { |
| 102 | + label: "Tags", |
| 103 | + description: "Optional keywords for this post.", |
| 104 | + itemLabel: (props) => props.value, |
| 105 | + }), |
| 106 | + seoTitle: fields.text({ |
| 107 | + label: "SEO Title", |
| 108 | + description: "Optional. Defaults to the post title when left empty.", |
| 109 | + }), |
| 110 | + seoDescription: fields.text({ |
| 111 | + label: "SEO Description", |
| 112 | + multiline: true, |
| 113 | + description: "Optional. Defaults to the excerpt when left empty.", |
| 114 | + }), |
| 115 | + content: fields.markdoc({ |
| 116 | + label: "Content", |
| 117 | + description: |
| 118 | + "Use the image toolbar button, paste, or drag and drop. Add meaningful alt text when prompted.", |
| 119 | + extension: "md", |
| 120 | + options: { |
| 121 | + image: { |
| 122 | + directory: "public/uploads", |
| 123 | + publicPath: "/uploads", |
| 124 | + transformFilename: sanitizeAssetFilename, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }), |
| 128 | + }, |
| 129 | + }), |
| 130 | + }, |
| 131 | +}); |
0 commit comments