From f088944c3de5c2e29b2a48d9e1a7eee48e97f922 Mon Sep 17 00:00:00 2001 From: Kristian Samuelsson Date: Mon, 19 Feb 2024 20:47:40 +0000 Subject: [PATCH] feat: add option to disable syntax highlighting of large payloads --- docs/usage/configuration.md | 10 ++++- src/core/components/highlight-code.jsx | 16 ++++---- test/unit/components/highlight-code.jsx | 49 +++++++++++++++++++++---- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 640820ade30d..41b5b3d1d858 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -226,13 +226,21 @@ Parameter name | Docker variable | Description - syntaxHighlight.activate + syntaxHighlight.activated Unavailable Boolean=true. Whether syntax highlighting should be activated or not. + + syntaxHighlight.sizeThreshold + + Unavailable + The maximum size (in bytes) of a payload to syntax highlight. + Any payload above this will not be syntax highlighted. Use this to avoid long delays caused by larged payloads. + + syntaxHighlight.theme diff --git a/src/core/components/highlight-code.jsx b/src/core/components/highlight-code.jsx index 8487f8079f0e..9549fe48a53b 100644 --- a/src/core/components/highlight-code.jsx +++ b/src/core/components/highlight-code.jsx @@ -9,8 +9,9 @@ import { CopyToClipboard } from "react-copy-to-clipboard" const HighlightCode = ({value, fileName = "response.txt", className, downloadable, getConfigs, canCopy, language}) => { const config = isFunction(getConfigs) ? getConfigs() : null - const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) const renderSizeThreshold = get(config, "renderSizeThreshold") + const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) + const syntaxHighlightSizeThreshold = canSyntaxHighlight ? get(config, "syntaxHighlight.sizeThreshold", undefined) : undefined const rootRef = useRef(null) useEffect(() => { @@ -45,14 +46,15 @@ const HighlightCode = ({value, fileName = "response.txt", className, downloadabl } const getRenderValues = () => { - if (renderSizeThreshold) { + if (renderSizeThreshold || syntaxHighlightSizeThreshold) { const valueSizeInBytes = (new TextEncoder().encode(value)).byteLength - const shouldRenderValue = valueSizeInBytes < renderSizeThreshold - return [shouldRenderValue, valueSizeInBytes] + const shouldRenderValue = renderSizeThreshold ? valueSizeInBytes < renderSizeThreshold : true + const shouldSyntaxHighlight = syntaxHighlightSizeThreshold ? valueSizeInBytes < syntaxHighlightSizeThreshold : true + return [shouldRenderValue, shouldSyntaxHighlight, valueSizeInBytes] } - return [true, undefined] + return [true, true, undefined] } - const [shouldRenderValue, valueSizeInBytes] = getRenderValues() + const [shouldRenderValue, shouldSyntaxHighlight, valueSizeInBytes] = getRenderValues() return (
@@ -75,7 +77,7 @@ const HighlightCode = ({value, fileName = "response.txt", className, downloadabl } {shouldRenderValue && ( - canSyntaxHighlight + canSyntaxHighlight && shouldSyntaxHighlight ? ( +const defaultSyntaxHighlightConfig = { + activated: true, + theme: "agate" +} + +const fakeGetConfigs = ( + renderSizeThreshold = undefined, + syntaxHighlight = defaultSyntaxHighlightConfig) => ( { renderSizeThreshold: renderSizeThreshold, - syntaxHighlight: { - activated: true, - theme: "agate" - } + syntaxHighlight: syntaxHighlight }) describe("", () => { @@ -27,14 +31,29 @@ describe("", () => { it("should render values in a preformatted element", () => { const value = "test text" - const props = { value: value, getConfigs: fakeGetConfigs } + const syntaxHighlightConfig = { + activated: false + } + const props = { value: value, getConfigs: () => fakeGetConfigs(undefined, syntaxHighlightConfig) } const wrapper = mount() - const preTag = wrapper.find("pre") + const highlighterTag = wrapper.find("SyntaxHighlighter") + expect(highlighterTag.length).toEqual(0) + const preTag = wrapper.find("pre") expect(preTag.length).toEqual(1) expect(preTag.text()).toEqual(value) }) + it("should render values in a syntax highlighted element", () => { + const value = "test text" + const props = { value: value, getConfigs: fakeGetConfigs } + const wrapper = mount() + const syntaxHighlighterTag = wrapper.find("SyntaxHighlighter") + + expect(syntaxHighlighterTag.length).toEqual(1) + expect(syntaxHighlighterTag.text()).toEqual(value) + }) + it("should not render values larger than threshold", () => { const value = "aaaaaaaa" //8 bytes const props = { value: value, getConfigs: () => fakeGetConfigs(7) } @@ -43,4 +62,20 @@ describe("", () => { expect(infoTag.text()).toEqual("Value is too large (8 bytes), rendering disabled.") }) + + it("should not highlight values larger larger than syntax highlight threshold", () => { + const value = "aaaaaaaa" //8 bytes + const syntaxHighlightConfig = { + defaultSyntaxHighlightConfig, + sizeThreshold: 8 + } + const props = { value: value, getConfigs: () => fakeGetConfigs(undefined, syntaxHighlightConfig) } + const wrapper = mount() + const highlighterTag = wrapper.find("SyntaxHighlighter") + expect(highlighterTag.length).toEqual(0) + + const preTag = wrapper.find("pre") + expect(preTag.length).toEqual(1) + expect(preTag.text()).toEqual(value) + }) })