Skip to content

fix: Syntax highlighting with long lines and untar content with emojis #5098

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 3 commits into from
Nov 15, 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
2 changes: 1 addition & 1 deletion site/js-untar.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module "js-untar" {
interface File {
name: string
readAsString: () => string
blob: Blob
}

const Untar: (buffer: ArrayBuffer) => {
Expand Down
3 changes: 3 additions & 0 deletions site/src/components/SyntaxHighlighter/SyntaxHighlighter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const useStyles = makeStyles((theme) => ({
background: theme.palette.background.paperLight,
borderRadius: theme.shape.borderRadius,
padding: theme.spacing(2, 3),
// Line breaks are broken when used with line numbers on react-syntax-highlighter
// https://github.com/react-syntax-highlighter/react-syntax-highlighter/pull/483
overflowX: "auto",

"& code": {
color: theme.palette.text.secondary,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ const TEMPLATE_NAME = "coder-ts"
const VERSION_NAME = "12345"
const TERRAFORM_FILENAME = "main.tf"
const README_FILENAME = "readme.md"
const GPG_FILENAME = "key.gpg"
const TEMPLATE_VERSION_FILES = {
[TERRAFORM_FILENAME]: "{}",
[README_FILENAME]: "Readme",
[GPG_FILENAME]: "Some sensitive info",
}

const setup = async () => {
Expand All @@ -38,10 +36,9 @@ const setup = async () => {
describe("TemplateVersionPage", () => {
beforeEach(setup)

it("shows the tf and md files only", () => {
it("shows files", () => {
expect(screen.queryByText(TERRAFORM_FILENAME)).toBeInTheDocument()
expect(screen.queryByText(README_FILENAME)).toBeInTheDocument()
Copy link
Member

Choose a reason for hiding this comment

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

Why did we end up changing this test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because now, the filtering happens inside of the "parsing" function. I made it to not have to parse 100 files to only get 2 of them after.

expect(screen.queryByText(GPG_FILENAME)).not.toBeInTheDocument()
})

it("shows the right content when click on the file name", async () => {
Expand Down
27 changes: 14 additions & 13 deletions site/src/util/templateVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ export type TemplateVersionFiles = Record<string, string>

export const getTemplateVersionFiles = async (
version: TemplateVersion,
allowedExtensions: string[],
): Promise<TemplateVersionFiles> => {
const files: TemplateVersionFiles = {}
const tarFile = await getFile(version.job.file_id)
const blobs: Record<string, Blob> = {}

await untar(tarFile).then(undefined, undefined, async (file) => {
const paths = file.name.split("/")
const filename = paths[paths.length - 1]
files[filename] = file.readAsString()
const [_, extension] = filename.split(".")

if (allowedExtensions.includes(extension)) {
blobs[filename] = file.blob
}
})
return files
}

export const filterTemplateFilesByExtension = (
files: TemplateVersionFiles,
extensions: string[],
): TemplateVersionFiles => {
return Object.keys(files).reduce((filteredFiles, filename) => {
const [_, extension] = filename.split(".")
await Promise.all(
Object.entries(blobs).map(async ([filename, blob]) => {
files[filename] = await blob.text()
}),
)

return extensions.includes(extension)
? { ...filteredFiles, [filename]: files[filename] }
: filteredFiles
}, {} as TemplateVersionFiles)
return files
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getTemplateVersionByName } from "api/api"
import { TemplateVersion } from "api/typesGenerated"
import {
filterTemplateFilesByExtension,
getTemplateVersionFiles,
TemplateVersionFiles,
} from "util/templateVersion"
Expand Down Expand Up @@ -86,10 +85,7 @@ export const templateVersionMachine = createMachine(
if (!version) {
throw new Error("Version is not defined")
}
return filterTemplateFilesByExtension(
await getTemplateVersionFiles(version),
["tf", "md"],
)
return getTemplateVersionFiles(version, ["tf", "md"])
},
},
},
Expand Down