Repository navigation
feat(blog): ✨ add blog post preview page and related functionality #914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import type { ContentFileDescriptor } from "./files"; | ||
| import type { ContentPublicPerson } from "./public-person"; | ||
|
|
||
| import { zodContentFileDescriptor } from "./files"; | ||
| import { zodContentPublicPerson } from "./public-person"; | ||
|
|
||
| /** | ||
| * The `type` of every live-preview `postMessage`. The preview page sends | ||
| * `ready` to its framing or opening editor when it loads; the editor answers | ||
| * with `values` on that and on every form change. | ||
| */ | ||
| export const CONTENT_LIVE_PREVIEW_MESSAGE = { | ||
| ready: "vitnode:content-live-preview:ready", | ||
| values: "vitnode:content-live-preview:values", | ||
| } as const; | ||
|
|
||
| /** | ||
| * The descriptors behind the identifiers in `values`, so a preview can render a | ||
| * cover image just uploaded or an author just picked without a lookup of its | ||
| * own. Each list holds every descriptor the editor knows, saved or not. | ||
| */ | ||
| export interface ContentLivePreviewReferences { | ||
| files: ContentFileDescriptor[]; | ||
| people: ContentPublicPerson[]; | ||
| } | ||
|
|
||
| /** One message from the editor: the unsaved form, as the form holds it. */ | ||
| export interface ContentLivePreviewPayload { | ||
| /** The language the editor is writing in. */ | ||
| locale: string; | ||
| references: ContentLivePreviewReferences; | ||
| /** | ||
| * Every field by name, in the form's own shapes: a file or user field holds | ||
| * identifiers (look them up in `references`), a localized field holds | ||
| * `{ languageCode, value }[]`. | ||
| */ | ||
| values: Record<string, unknown>; | ||
| } | ||
|
|
||
| const isRecord = (value: unknown): value is Record<string, unknown> => | ||
| typeof value === "object" && value !== null && !Array.isArray(value); | ||
|
|
||
| const zodReferences = z.object({ | ||
| files: z.array(zodContentFileDescriptor).catch([]), | ||
| people: z.array(zodContentPublicPerson).catch([]), | ||
| }); | ||
|
|
||
| /** | ||
| * Reads a `values` message (`event.data`) sent by the editor's | ||
| * `ContentLivePreview`. Universal and pure. | ||
| * | ||
| * Accepts `{ type: CONTENT_LIVE_PREVIEW_MESSAGE.values, locale: string, | ||
| * values: object, references?: { files, people } }`. Returns `null` for any | ||
| * other type, a missing `locale` or a `values` that is not a plain object. | ||
| * Invalid `references` lists become empty lists instead of rejecting the | ||
| * message. | ||
| * | ||
| * Only the envelope is checked: each entry in `values` stays `unknown`, so | ||
| * validate a field before you render it. It does not authenticate the | ||
| * message either - check `event.origin` and `event.source` first, as | ||
| * `useContentLivePreview` does. | ||
| */ | ||
| export const parseContentLivePreviewValues = ( | ||
| data: unknown, | ||
| ): ContentLivePreviewPayload | null => { | ||
| if (!isRecord(data) || data.type !== CONTENT_LIVE_PREVIEW_MESSAGE.values) { | ||
| return null; | ||
| } | ||
| if (typeof data.locale !== "string" || !isRecord(data.values)) return null; | ||
|
|
||
| return { | ||
| locale: data.locale, | ||
| references: zodReferences | ||
| .catch({ files: [], people: [] }) | ||
| .parse(data.references), | ||
| values: data.values, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Whether `event.data` is the `ready` message a preview page sends when it | ||
| * loads, telling the editor to send the current values. Universal and pure. | ||
| * | ||
| * Accepts any object whose `type` is `CONTENT_LIVE_PREVIEW_MESSAGE.ready`; | ||
| * other properties are ignored. It does not authenticate the message - check | ||
| * `event.origin` and that `event.source` is the preview frame or popup the | ||
| * editor opened before answering. | ||
| */ | ||
| export const isContentLivePreviewReady = (data: unknown): boolean => | ||
| isRecord(data) && data.type === CONTENT_LIVE_PREVIEW_MESSAGE.ready; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
parseContentLivePreviewValuesandisContentLivePreviewReadyhelpers are re-exported through@vitnode/core/content, but neither has declaration-level JSDoc. Document their universal runtime, accepted message shapes, return semantics, and validation limits—particularly that parsing does not authenticate the message's origin or source. The hook and component now have documentation, but these additional public protocol APIs still violate the declaration-documentation requirement inAGENTS.md.AGENTS.md reference: AGENTS.md:L134-L135
Useful? React with 👍 / 👎.