Skip to content

Commit 2543e8c

Browse files
authored
ansi support for attachments (#223)
1 parent e59f0de commit 2543e8c

File tree

7 files changed

+83
-14
lines changed

7 files changed

+83
-14
lines changed

.pnp.cjs

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

packages/web-components/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"@rollup/plugin-alias": "^5.1.1",
7272
"@rollup/plugin-babel": "^6.0.4",
7373
"@rollup/plugin-commonjs": "^28.0.2",
74+
"@rollup/plugin-json": "^6.1.0",
7475
"@rollup/plugin-node-resolve": "^16.0.0",
7576
"@rollup/plugin-terser": "^0.4.4",
7677
"@rollup/plugin-typescript": "^12.1.2",
@@ -103,6 +104,7 @@
103104
"@vitest/snapshot": "^2.1.9",
104105
"allure-js-commons": "^3.0.9",
105106
"allure-vitest": "^3.0.9",
107+
"ansi-to-html": "^0.7.2",
106108
"autoprefixer": "^10.4.20",
107109
"babel-loader": "^9.2.1",
108110
"babel-plugin-prismjs": "^2.1.0",

packages/web-components/rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import alias from "@rollup/plugin-alias";
22
import { babel } from "@rollup/plugin-babel";
33
import commonjs from "@rollup/plugin-commonjs";
4+
import json from "@rollup/plugin-json";
45
import resolve from "@rollup/plugin-node-resolve";
56
import terser from "@rollup/plugin-terser";
67
import typescript from "@rollup/plugin-typescript";
@@ -29,6 +30,7 @@ export default defineConfig([
2930
],
3031
external: ["preact", "preact/hooks", "react", "react-dom"],
3132
plugins: [
33+
json(),
3234
alias({
3335
entries: [
3436
{
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { type AttachmentTestStepResult } from "@allurereport/core-api";
2+
import AnsiToHtml from "ansi-to-html";
23
import { type FunctionalComponent } from "preact";
34
import { useEffect } from "preact/hooks";
45
import Prism from "prismjs";
56
import "./code.scss";
67

8+
const ansiRegex = /\x1B\[[0-9;?]*[ -/]*[@-~]/g;
9+
10+
const ansiTrace = (text: string) =>
11+
new AnsiToHtml({
12+
fg: "var(--on-text-primary)",
13+
}).toHtml(text);
14+
15+
const isAnsi = (text?: string): boolean => typeof text === "string" && new RegExp(ansiRegex).test(text);
16+
717
export const AttachmentCode: FunctionalComponent<{
818
item: AttachmentTestStepResult;
919
attachment: { text?: string };
@@ -12,13 +22,27 @@ export const AttachmentCode: FunctionalComponent<{
1222
Prism.highlightAll();
1323
}, [attachment]);
1424

25+
const ext = item?.link?.ext?.replace(".", "") ?? "plaintext";
26+
const rawText = attachment.text ?? "";
27+
1528
return (
16-
<pre
17-
data-testid={"code-attachment-content"}
18-
key={item?.link?.id}
19-
className={`language-${item?.link?.ext?.replace(".", "")} line-numbers`}
20-
>
21-
<code>{attachment?.text}</code>
22-
</pre>
29+
<>
30+
{isAnsi(rawText) ? (
31+
<pre
32+
data-testid="code-attachment-content"
33+
key={item?.link?.id}
34+
className={`language-${ext} line-numbers`}
35+
dangerouslySetInnerHTML={{ __html: ansiTrace(rawText) }}
36+
/>
37+
) : (
38+
<pre
39+
data-testid={"code-attachment-content"}
40+
key={item?.link?.id}
41+
className={`language-${item?.link?.ext?.replace(".", "")} line-numbers`}
42+
>
43+
<code className={`language-${ext}`}>{rawText}</code>
44+
</pre>
45+
)}
46+
</>
2347
);
2448
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export { Attachment } from "@/components/Attachment/Attachment";
2-
export { AttachmentCode } from "@/components/Attachment/AttachmentCode";
3-
export { HtmlPreview } from "@/components/Attachment/HtmlPreview";
4-
export { AttachmentImage } from "@/components/Attachment/AttachmentImage";
5-
export { AttachmentVideo } from "@/components/Attachment/AttachmentVideo";
6-
export { AttachmentEmpty } from "@/components/Attachment/AttachmentEmpty";
7-
export { CodeViewer } from "@/components/Attachment/CodeViewer";
1+
export { Attachment } from "./Attachment";
2+
export { AttachmentCode } from "./AttachmentCode";
3+
export { HtmlPreview } from "./HtmlPreview";
4+
export { AttachmentImage } from "./AttachmentImage";
5+
export { AttachmentVideo } from "./AttachmentVideo";
6+
export { AttachmentEmpty } from "./AttachmentEmpty";
7+
export { CodeViewer } from "./CodeViewer";

yarn.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ __metadata:
919919
"@rollup/plugin-alias": "npm:^5.1.1"
920920
"@rollup/plugin-babel": "npm:^6.0.4"
921921
"@rollup/plugin-commonjs": "npm:^28.0.2"
922+
"@rollup/plugin-json": "npm:^6.1.0"
922923
"@rollup/plugin-node-resolve": "npm:^16.0.0"
923924
"@rollup/plugin-terser": "npm:^0.4.4"
924925
"@rollup/plugin-typescript": "npm:^12.1.2"
@@ -951,6 +952,7 @@ __metadata:
951952
"@vitest/snapshot": "npm:^2.1.9"
952953
allure-js-commons: "npm:^3.0.9"
953954
allure-vitest: "npm:^3.0.9"
955+
ansi-to-html: "npm:^0.7.2"
954956
autoprefixer: "npm:^10.4.20"
955957
babel-loader: "npm:^9.2.1"
956958
babel-plugin-prismjs: "npm:^2.1.0"
@@ -5395,6 +5397,20 @@ __metadata:
53955397
languageName: node
53965398
linkType: hard
53975399

5400+
"@rollup/plugin-json@npm:^6.1.0":
5401+
version: 6.1.0
5402+
resolution: "@rollup/plugin-json@npm:6.1.0"
5403+
dependencies:
5404+
"@rollup/pluginutils": "npm:^5.1.0"
5405+
peerDependencies:
5406+
rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0
5407+
peerDependenciesMeta:
5408+
rollup:
5409+
optional: true
5410+
checksum: 10/cc018d20c80242a2b8b44fae61a968049cf31bb8406218187cc7cda35747616594e79452dd65722e7da6dd825b392e90d4599d43cd4461a02fefa2865945164e
5411+
languageName: node
5412+
linkType: hard
5413+
53985414
"@rollup/plugin-node-resolve@npm:^16.0.0":
53995415
version: 16.0.0
54005416
resolution: "@rollup/plugin-node-resolve@npm:16.0.0"

0 commit comments

Comments
 (0)