-
Notifications
You must be signed in to change notification settings - Fork 45
feat(code-editor): render HTML files with a preview/source toggle #2782
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
Open
k11kirky
wants to merge
2
commits into
main
Choose a base branch
from
posthog-code/html-file-preview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+202
−27
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getRenderableKind, isHtmlFile, isMarkdownFile } from "./fileKind"; | ||
|
|
||
| describe("isMarkdownFile", () => { | ||
| it.each([ | ||
| ["README.md", true], | ||
| ["notes.markdown", true], | ||
| ["UPPER.MD", true], | ||
| ["docs/guide.md", true], | ||
| ["index.html", false], | ||
| ["script.js", false], | ||
| ["no-extension", false], | ||
| // Dotfiles: the name after the leading dot is read as the extension, so a | ||
| // file literally named ".md" matches, while ".gitignore" does not. | ||
| [".md", true], | ||
| [".gitignore", false], | ||
| ["", false], | ||
| ])("%s -> %s", (filename, expected) => { | ||
| expect(isMarkdownFile(filename)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isHtmlFile", () => { | ||
| it.each([ | ||
| ["index.html", true], | ||
| ["page.htm", true], | ||
| ["INDEX.HTML", true], | ||
| ["build/report.html", true], | ||
| ["README.md", false], | ||
| ["styles.css", false], | ||
| ["no-extension", false], | ||
| // Dotfiles: ".html" reads as the html extension; ".htaccess" does not. | ||
| [".html", true], | ||
| [".htaccess", false], | ||
| ["", false], | ||
| ])("%s -> %s", (filename, expected) => { | ||
| expect(isHtmlFile(filename)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getRenderableKind", () => { | ||
| it.each([ | ||
| ["README.md", "markdown"], | ||
| ["notes.markdown", "markdown"], | ||
| ["index.html", "html"], | ||
| ["page.htm", "html"], | ||
| ["script.js", null], | ||
| ["styles.css", null], | ||
| ["no-extension", null], | ||
| [".gitignore", null], | ||
| [".htaccess", null], | ||
| ["", null], | ||
| ])("%s -> %s", (filename, expected) => { | ||
| expect(getRenderableKind(filename)).toBe(expected); | ||
| }); | ||
| }); | ||
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 |
|---|---|---|
| @@ -1,6 +1,29 @@ | ||
| import { getFileExtension } from "@posthog/shared"; | ||
|
|
||
| const MARKDOWN_EXTENSIONS = new Set(["md", "markdown"]); | ||
| const HTML_EXTENSIONS = new Set(["html", "htm"]); | ||
|
|
||
| export type RenderableKind = "markdown" | "html"; | ||
|
|
||
| export function isMarkdownFile(filename: string): boolean { | ||
| const ext = filename.split(".").pop()?.toLowerCase(); | ||
| return !!ext && MARKDOWN_EXTENSIONS.has(ext); | ||
| return MARKDOWN_EXTENSIONS.has(getFileExtension(filename)); | ||
| } | ||
|
|
||
| export function isHtmlFile(filename: string): boolean { | ||
| return HTML_EXTENSIONS.has(getFileExtension(filename)); | ||
| } | ||
|
|
||
| /** | ||
| * The inline preview a file supports when opened from the file tree, or null if | ||
| * it should open as plain source. Add a kind here (plus a renderer branch in | ||
| * CodeEditorPanel) to make another file type previewable. | ||
| */ | ||
| export function getRenderableKind(filename: string): RenderableKind | null { | ||
| if (isMarkdownFile(filename)) { | ||
| return "markdown"; | ||
| } | ||
| if (isHtmlFile(filename)) { | ||
| return "html"; | ||
| } | ||
| return null; | ||
| } |
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,46 @@ | ||
| import type { RenderableKind } from "@posthog/core/code-editor/fileKind"; | ||
| import { create } from "zustand"; | ||
| import { persist } from "zustand/middleware"; | ||
|
|
||
| // Per renderable file kind, whether files open as a rendered preview (vs raw | ||
| // source). The preference is global per kind and persisted, so a user who | ||
| // prefers source mode keeps it across files and sessions. | ||
| interface FilePreviewStoreState { | ||
| renderPreview: Record<RenderableKind, boolean>; | ||
| } | ||
|
|
||
| interface FilePreviewStoreActions { | ||
| toggleKind: (kind: RenderableKind) => void; | ||
| } | ||
|
|
||
| type FilePreviewStore = FilePreviewStoreState & FilePreviewStoreActions; | ||
|
|
||
| export const useFilePreviewStore = create<FilePreviewStore>()( | ||
| persist( | ||
| (set) => ({ | ||
| renderPreview: { markdown: true, html: true }, | ||
| toggleKind: (kind) => | ||
| set((s) => ({ | ||
| renderPreview: { ...s.renderPreview, [kind]: !s.renderPreview[kind] }, | ||
| })), | ||
| }), | ||
| { | ||
| name: "file-preview-storage", | ||
| // Deep-merge `renderPreview` so a kind added to the defaults later (e.g. | ||
| // svg) keeps its rendered-by-default value for users whose stored state | ||
| // predates it — a shallow merge would drop it and show source instead. | ||
| merge: (persisted, current) => { | ||
| const persistedState = persisted as { | ||
| renderPreview?: Partial<Record<RenderableKind, boolean>>; | ||
| }; | ||
| return { | ||
| ...current, | ||
| renderPreview: { | ||
| ...current.renderPreview, | ||
| ...persistedState.renderPreview, | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
|
k11kirky marked this conversation as resolved.
|
||
| ), | ||
| ); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.