Skip to content

Commit 7b35d2d

Browse files
fix(card): ensure copy can render markdown (#7871)
### Related Ticket(s) #7481 ### Description This PR ensures that the copy elements in Card and Content Section can render markdown properly. The approach used in Card queries the slotted `p` tag and applies the markdown utility function. While a simpler method may be creating a `card-copy` component that extends `DDSMarkdown`, I refrained from doing so to avoid a breaking change for adopters using Card. Feel free to let me know what you think about doing that approach instead and simply mentioning it in the release notes. Note: Testing markdown in Storybook seems to only work once (i.e. inputting `*test*`, deleting it, and trying the same thing will result in the text being wrapped in asterisks without formatting. However, if an adopter uses markdown in the copy of the Card component, it will render properly. <img width="407" alt="Screen Shot 2021-12-13 at 3 10 55 PM" src="https://user-images.githubusercontent.com/24970122/145907648-f0c2d1d5-6ee5-4996-84aa-a3e33480bf0f.png"> ### Changelog **Changed** - Card copy can now use markdown - Content section copy now extends `DDSMarkdown` component <!-- React and Web Component deploy previews are enabled by default. --> <!-- To enable additional available deploy previews, apply the following --> <!-- labels for the corresponding package: --> <!-- *** "package: services": Services --> <!-- *** "package: utilities": Utilities --> <!-- *** "RTL": React / Web Components (RTL) --> <!-- *** "feature flag": React / Web Components (experimental) -->
1 parent 110d96a commit 7b35d2d

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

packages/utilities/src/utilities/markdownToHtml/markdownToHtml.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import settings from 'carbon-components/es/globals/js/settings';
1111
const { prefix } = settings;
1212

1313
const _htmlTagRegex = /<.*?>/g;
14-
const _cleanStringRegex = /\n|\s{2,}|&([a-zA-Z]+);/g;
14+
const _cleanStringRegex = /\n|\s{2,}|&;/g;
1515

1616
/**
1717
* Removes any html tags from a string and keeps inner text if any

packages/web-components/src/components/card-section-carousel/__stories__/card-section-carousel.stories.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export const Default = () => {
3636
return html`
3737
<dds-card-section-carousel>
3838
<dds-content-section-heading>Lorem ipsum dolor sit amet</dds-content-section-heading>
39-
<dds-content-section-copy>
40-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et ultricies est.
39+
<dds-content-section-copy
40+
>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et ultricies est.
4141
</dds-content-section-copy>
4242
<dds-link-with-icon slot="footer" href="${ifNonNull(hrefDefault)}">
4343
Link text ${ArrowRight20({ slot: 'icon' })}

packages/web-components/src/components/card/card.ts

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { html, property, internalProperty, customElement, TemplateResult, query
1111
import settings from 'carbon-components/es/globals/js/settings';
1212
import ddsSettings from '@carbon/ibmdotcom-utilities/es/utilities/settings/settings.js';
1313
import BXLink from 'carbon-web-components/es/components/link/link';
14+
import markdownToHtml from '@carbon/ibmdotcom-utilities/es/utilities/markdownToHtml/markdownToHtml.js';
1415
import { BASIC_COLOR_SCHEME } from '../../globals/defs';
1516
import StableSelectorMixin from '../../globals/mixins/stable-selector';
1617
import DDSCardFooter from './card-footer';
@@ -242,6 +243,12 @@ class DDSCard extends StableSelectorMixin(BXLink) {
242243
if (this._hasPictogram) {
243244
this.onclick = () => window.open(this.href, '_self');
244245
}
246+
247+
const copyElement = this.querySelector('p');
248+
if (this._hasCopy && copyElement?.innerText) {
249+
copyElement.innerHTML = `${markdownToHtml(copyElement?.innerText, { bold: false })}`;
250+
copyElement.firstElementChild?.setAttribute('style', 'all:unset;');
251+
}
245252
}
246253

247254
render() {

packages/web-components/src/components/content-section/content-section-copy.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
* LICENSE file in the root directory of this source tree.
88
*/
99

10-
import { html, property, customElement, LitElement } from 'lit-element';
10+
import { html, property, customElement } from 'lit-element';
1111
import ddsSettings from '@carbon/ibmdotcom-utilities/es/utilities/settings/settings.js';
1212
import styles from './content-section.scss';
1313
import StableSelectorMixin from '../../globals/mixins/stable-selector';
14+
import DDSMarkdown from '../markdown/markdown';
1415

1516
const { stablePrefix: ddsPrefix } = ddsSettings;
1617

@@ -20,7 +21,7 @@ const { stablePrefix: ddsPrefix } = ddsSettings;
2021
* @element dds-content-section-copy
2122
*/
2223
@customElement(`${ddsPrefix}-content-section-copy`)
23-
class DDSContentSectionCopy extends StableSelectorMixin(LitElement) {
24+
class DDSContentSectionCopy extends StableSelectorMixin(DDSMarkdown) {
2425
@property({ reflect: true })
2526
slot = 'copy';
2627

@@ -30,6 +31,10 @@ class DDSContentSectionCopy extends StableSelectorMixin(LitElement) {
3031
`;
3132
}
3233

34+
firstUpdated() {
35+
this.querySelector('p')?.setAttribute('style', 'all:unset;');
36+
}
37+
3338
static get stableSelector() {
3439
return `${ddsPrefix}--content-section-copy`;
3540
}

0 commit comments

Comments
 (0)