feat(code-editor): render HTML files with a preview/source toggle#2782
Open
k11kirky wants to merge 2 commits into
Open
feat(code-editor): render HTML files with a preview/source toggle#2782k11kirky wants to merge 2 commits into
k11kirky wants to merge 2 commits into
Conversation
Clicking an HTML file in the file tree now renders it, like markdown, with a toggle to switch between the rendered preview and the raw source. - Add `getRenderableKind()` to core, a single discriminator over the previewable file kinds (markdown, HTML), reusing the shared `getFileExtension` helper. - HTML renders in a null-origin sandboxed iframe (`allow-scripts` without `allow-same-origin`), the same isolation pattern used by the freeform canvas. - Persist the rendered-vs-source preference per kind in a keyed `renderPreview` record so adding a future previewable type is a one-line change. - A header toggle (eye/code) flips between preview and CodeMirror source for any renderable file. Generated-By: PostHog Code Task-Id: f9f523bf-9c44-41e8-a1ed-dd6bc9d52140
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
Contributor
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
packages/ui/src/features/code-editor/filePreviewStore.ts:27-29
The `persist` middleware performs a shallow merge at the top level, so `renderPreview` from localStorage completely replaces the initial object. Any future `RenderableKind` added to the defaults (e.g. `svg: true`) will be absent from the hydrated state for existing users, making `s.renderPreview[newKind]` return `undefined` (falsy) — they'd see source view instead of the rendered preview default. Adding a `version` and a `merge` strategy protects against this.
```suggestion
{
name: "file-preview-storage",
version: 1,
merge: (persisted, current) => ({
...current,
...(persisted as Partial<FilePreviewStore>),
renderPreview: {
...(current as FilePreviewStore).renderPreview,
...((persisted as Partial<FilePreviewStore>)?.renderPreview ?? {}),
},
}),
},
```
### Issue 2 of 2
packages/core/src/code-editor/fileKind.test.ts:1-47
Missing dotfile coverage despite "dotfile-robust" claim
The PR description says `getFileExtension` is "dotfile-robust", but no test cases exercise dotfiles like `.html` or `.md`. Looking at the implementation, `getFileExtension(".html")` returns `"html"` (because `lastDot = 0 >= 0`), so `isHtmlFile(".html")` returns `true`. Whether that is the intended behavior should be documented with an explicit test case — both to pin the behavior and to clarify the contract for future readers.
Reviews (1): Last reviewed commit: "feat(code-editor): render HTML files wit..." | Re-trigger Greptile |
…behavior
Address Greptile review on the file preview feature:
- filePreviewStore: add a `merge` strategy so a `RenderableKind` added to the
defaults later survives hydration for users with older stored state (a shallow
merge would drop it and fall back to source view).
- fileKind.test: pin dotfile behavior (".md"/".html" read as their extension;
".gitignore"/".htaccess" are not renderable).
Generated-By: PostHog Code
Task-Id: f9f523bf-9c44-41e8-a1ed-dd6bc9d52140
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Clicking a
.html/.htmfile in the file tree now renders it as a preview — the same way.mdfiles already render markdown — with a toggle to switch between the rendered preview and the raw source. The toggle also applies to markdown.Why
HTML files (generated reports, coverage output, exported pages, etc.) were only viewable as raw source. Rendering them inline makes the file viewer useful for these, and the toggle keeps the source one click away.
How
@posthog/core–fileKind.ts: addedgetRenderableKind(filename): "markdown" | "html" | null, a single discriminator over previewable file kinds. The existingisMarkdownFile/isHtmlFilepredicates now reuse the shared, dotfile-robustgetFileExtensionfrom@posthog/sharedinstead of inlining extension parsing.filePreviewStore.ts(new): a persisted Zustand store holdingrenderPreview: Record<RenderableKind, boolean>(defaults to rendered) plus atoggleKindaction. Keyed by kind so adding a future previewable type (SVG, CSV, …) is a one-line default + one render branch, not a new pair of fields/toggles.CodeEditorPanel.tsx: derives a singlerenderableKindand routes to the markdown renderer, the newHtmlFilePreview, or the CodeMirror source view. A header toggle (eye / code icons) flips preview ↔ source.HtmlFilePreview: renders the file in a null-origin sandboxed iframe (sandbox="allow-scripts", noallow-same-origin) — the same isolation pattern already used by the freeform canvas, so a viewed HTML file can't reach the host renderer's DOM/storage.Notes / trade-offs
srcDoc— the expected limitation for a safe inline preview.Testing
pnpm --filter @posthog/core typecheckandpnpm --filter @posthog/ui typecheck— clean.fileKind.test.tscoveringisMarkdownFile/isHtmlFile/getRenderableKind— 24 cases pass.🤖 Generated with Claude Code