Skip to content

Commit

Permalink
fix: Syntax highlighting with long lines and untar content with emojis (
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoQuaresma committed Nov 15, 2022
1 parent 44d3225 commit 2a46702
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
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()
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

0 comments on commit 2a46702

Please sign in to comment.