Skip to content

refactor: Improve template README section #4794

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 6 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MacOS = "macOS"
[default.extend-words]
# do as sudo replacement
doas = "doas"
darcula = "darcula"

[files]
extend-exclude = [
Expand Down
84 changes: 64 additions & 20 deletions site/src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import Link from "@material-ui/core/Link"
import { makeStyles, Theme, useTheme } from "@material-ui/core/styles"
import { makeStyles } from "@material-ui/core/styles"
import Table from "@material-ui/core/Table"
import TableBody from "@material-ui/core/TableBody"
import TableCell from "@material-ui/core/TableCell"
import TableContainer from "@material-ui/core/TableContainer"
import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import { FC } from "react"
import React, { FC } from "react"
import ReactMarkdown from "react-markdown"
import SyntaxHighlighter from "react-syntax-highlighter"
import { dracula as dark } from "react-syntax-highlighter/dist/cjs/styles/hljs"
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"
import gfm from "remark-gfm"
import { colors } from "theme/colors"
import darcula from "react-syntax-highlighter/dist/cjs/styles/prism/darcula"

export interface MarkdownProps {
children: string
}

export const Markdown: FC<{ children: string }> = ({ children }) => {
const theme: Theme = useTheme()
const styles = useStyles()

return (
<ReactMarkdown
className={styles.markdown}
remarkPlugins={[gfm]}
components={{
a: ({ href, target, children }) => (
Expand All @@ -30,22 +31,27 @@ export const Markdown: FC<{ children: string }> = ({ children }) => {
</Link>
),

pre: ({ node, children }) => {
const firstChild = node.children[0]
// When pre is wrapping a code, the SyntaxHighlighter is already going
// to wrap it with a pre so we don't need it
if (firstChild.type === "element" && firstChild.tagName === "code") {
return <>{children}</>
}
return <pre>{children}</pre>
},

code: ({ node, inline, className, children, ...props }) => {
const match = /language-(\w+)/.exec(className || "")

return !inline && match ? (
<SyntaxHighlighter
// Custom style to match our main colors
style={{
...dark,
hljs: {
...dark.hljs,
background: theme.palette.background.default,
borderRadius: theme.shape.borderRadius,
color: theme.palette.text.primary,
},
}}
style={darcula}
language={match[1]}
PreTag="div"
useInlineStyles={false}
// Use inline styles does not work correctly
// https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/329
codeTagProps={{ style: {} }}
{...props}
>
{String(children).replace(/\n$/, "")}
Expand Down Expand Up @@ -91,12 +97,50 @@ export const Markdown: FC<{ children: string }> = ({ children }) => {
)
}

export const MemoizedMarkdown = React.memo(Markdown)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a memoized version so this is not going to be reloaded all the time since we have auto-update in the template page. I also memorized this to make my life easier on debug - the component was refreshing and losing the styles that applied in the browser every 2 seconds 😅


const useStyles = makeStyles((theme) => ({
markdown: {
fontSize: 16,
lineHeight: "24px",

"& h1, & h2, & h3, & h4, & h5, & h6": {
marginTop: theme.spacing(4),
marginBottom: theme.spacing(2),
lineHeight: "1.25",
},

"& p": {
marginTop: 0,
marginBottom: theme.spacing(2),
},

"& ul, & ol": {
display: "flex",
flexDirection: "column",
gap: theme.spacing(1),
},

"& .prismjs": {
background: theme.palette.background.paperLight,
borderRadius: theme.shape.borderRadius,
padding: theme.spacing(2, 3),

"& code": {
color: theme.palette.text.secondary,
},

"& .key, & .property": {
color: colors.turquoise[7],
},
},
},

codeWithoutLanguage: {
overflowX: "auto",
padding: "0.5em",
background: theme.palette.background.default,
borderRadius: theme.shape.borderRadius,
padding: theme.spacing(0.5, 1),
background: theme.palette.divider,
borderRadius: 4,
color: theme.palette.text.primary,
fontSize: 14,
},
}))
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ import {
WorkspaceResource,
} from "api/typesGenerated"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import { Markdown } from "components/Markdown/Markdown"
import { MemoizedMarkdown } from "components/Markdown/Markdown"
import { Stack } from "components/Stack/Stack"
import { TemplateResourcesTable } from "components/TemplateResourcesTable/TemplateResourcesTable"
import { TemplateStats } from "components/TemplateStats/TemplateStats"
import { VersionsTable } from "components/VersionsTable/VersionsTable"
import { WorkspaceSection } from "components/WorkspaceSection/WorkspaceSection"
import frontMatter from "front-matter"
import { FC } from "react"
import { DAUChart } from "./DAUChart"

const Language = {
readmeTitle: "README",
resourcesTitle: "Resources",
}

export interface TemplateSummaryPageViewProps {
template: Template
activeTemplateVersion: TemplateVersion
Expand Down Expand Up @@ -64,27 +58,38 @@ export const TemplateSummaryPageView: FC<
<TemplateResourcesTable
resources={getStartedResources(templateResources)}
/>
<WorkspaceSection
title={Language.readmeTitle}
contentsProps={{ className: styles.readmeContents }}
>

<div className={styles.markdownSection}>
<div className={styles.readmeLabel}>README.md</div>
<div className={styles.markdownWrapper}>
<Markdown>{readme.body}</Markdown>
<MemoizedMarkdown>{readme.body}</MemoizedMarkdown>
</div>
</WorkspaceSection>
</div>

<VersionsTable versions={templateVersions} />
</Stack>
)
}

export const useStyles = makeStyles((theme) => {
return {
readmeContents: {
margin: 0,
markdownSection: {
background: theme.palette.background.paper,
border: `1px solid ${theme.palette.divider}`,
borderRadius: theme.shape.borderRadius,
},

readmeLabel: {
color: theme.palette.text.secondary,
fontWeight: 600,
padding: theme.spacing(2, 3),
borderBottom: `1px solid ${theme.palette.divider}`,
},

markdownWrapper: {
background: theme.palette.background.paper,
padding: theme.spacing(3, 4),
padding: theme.spacing(0, 3, 5),
maxWidth: 800,
margin: "auto",
},
}
})