Skip to content

Commit 3c88021

Browse files
feat: add decoration to style-attribute with style type and tests (#74)
* fix: add decoration to style-attribute * fix: use CSS property type this commit uses a CSSProperties type from feedback provided in #61 it also adds a tests to test the functionality. closes #61 Co-authored-by: vmik7 <[email protected]>
1 parent deb6520 commit 3c88021

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

__tests__/index.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,20 @@ describe("Ansi", () => {
305305
'<code><span class="ansi-green-fg">this is a link: <a href="https://nteract.io/" target="_blank">https://nteract.io/</a></span></code>'
306306
);
307307
});
308+
309+
test("can add text decoration styles", () => {
310+
const el = shallow(
311+
React.createElement(
312+
Ansi,
313+
{},
314+
`hello ${GREEN_FG}${BOLD}world${RESET}!`
315+
)
316+
);
317+
expect(el).not.toBeNull();
318+
expect(el.text()).toBe("hello world!");
319+
expect(el.html()).toBe(
320+
'<code><span>hello </span><span style="color:rgb(0, 187, 0);font-weight:bold">world</span><span>!</span></code>'
321+
);
322+
});
308323
});
309324
});

src/index.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,46 @@ function createClass(bundle: AnserJsonEntry): string | null {
5151
return classNames;
5252
}
5353

54-
interface Colors {
55-
color?: string;
56-
backgroundColor?: string;
57-
}
58-
5954
/**
6055
* Create the style attribute.
6156
* @name createStyle
6257
* @function
6358
* @param {AnserJsonEntry} bundle
6459
* @return {Object} returns the style object
6560
*/
66-
function createStyle(bundle: AnserJsonEntry): Colors {
67-
const style: Colors = {};
61+
function createStyle(bundle: AnserJsonEntry): React.CSSProperties {
62+
const style: React.CSSProperties = {};
6863
if (bundle.bg) {
6964
style.backgroundColor = `rgb(${bundle.bg})`;
7065
}
7166
if (bundle.fg) {
7267
style.color = `rgb(${bundle.fg})`;
7368
}
74-
69+
switch (bundle.decoration) {
70+
case 'bold':
71+
style.fontWeight = 'bold';
72+
break;
73+
case 'dim':
74+
style.opacity = '0.5';
75+
break;
76+
case 'italic':
77+
style.fontStyle = 'italic';
78+
break;
79+
case 'hidden':
80+
style.visibility = 'hidden';
81+
break;
82+
case 'strikethrough':
83+
style.textDecoration = 'line-through';
84+
break;
85+
case 'underline':
86+
style.textDecoration = 'underline';
87+
break;
88+
case 'blink':
89+
style.textDecoration = 'blink';
90+
break;
91+
default:
92+
break;
93+
}
7594
return style;
7695
}
7796

0 commit comments

Comments
 (0)