Skip to content

Commit b5146c1

Browse files
feat(content-block): added mock markup for content-block-heading (#6349)
### Related Ticket(s) #6083 ### Description I've added some light dom rendering for content-block-heading and content-block-paragraph, so its content can be accessed and read by SEO tools. I've also made a utility that strips HTML from a string, as we needed that to get the content to be rendered.
1 parent e070fe3 commit b5146c1

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright IBM Corp. 2020, 2021
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { stripHTML } from '../';
9+
10+
describe('StripHTML utility', () => {
11+
const content = '<h1>Lorem ipsum dolor sit amet.</h1>';
12+
13+
it('should return a string without HTML tags', () => {
14+
const output = stripHTML(content);
15+
expect(output).toBe('Lorem ipsum dolor sit amet.');
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2021
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as stripHTML } from './stripHTML';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2021
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
/**
9+
* Utility returns the text stripping all html tags from it.
10+
*
11+
* @example
12+
* import { stripHTML } from '@carbon/ibmdotcom-utilities';
13+
*
14+
* content = stripHTML(this.innerHtml);
15+
*
16+
* @param {string} content with html tags
17+
* @returns {null} content without html tags
18+
*/
19+
const stripHTML = content => {
20+
const component = document.createElement('textarea');
21+
component.innerHTML = content;
22+
23+
return component.childNodes[0].nodeValue.replace(/(<([^>]+)>)/gi, '');
24+
};
25+
26+
export default stripHTML;

packages/web-components/src/components/content-block-simple/__stories__/content-block-simple.stories.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const Default = ({ parameters }) => {
7878
const href = 'https://www.example.com';
7979
return html`
8080
<dds-content-block-simple complementary-style-scheme="${ifNonNull(complementaryStyleScheme)}">
81-
<dds-content-block-heading>${heading}</dds-content-block-heading>
81+
<dds-content-block-heading><h2>${heading}</h2></dds-content-block-heading>
8282
<dds-content-block-copy size="${CONTENT_BLOCK_COPY_SIZE.SMALL}">${copy}</dds-content-block-copy>
8383
${ctaStyle === 'card'
8484
? html`
@@ -112,7 +112,7 @@ export const WithImage = ({ parameters }) => {
112112
const href = 'https://www.example.com';
113113
return html`
114114
<dds-content-block-simple complementary-style-scheme="${ifNonNull(complementaryStyleScheme)}">
115-
<dds-content-block-heading>${heading}</dds-content-block-heading>
115+
<dds-content-block-heading><h2>${heading}</h2></dds-content-block-heading>
116116
${image}
117117
<dds-content-block-copy size="${CONTENT_BLOCK_COPY_SIZE.SMALL}">${copy}</dds-content-block-copy>
118118
${ctaStyle === 'card'
@@ -151,7 +151,7 @@ export const WithVideo = ({ parameters }) => {
151151
const href = 'https://www.example.com';
152152
return html`
153153
<dds-content-block-simple complementary-style-scheme="${ifNonNull(complementaryStyleScheme)}">
154-
<dds-content-block-heading>${heading}</dds-content-block-heading>
154+
<dds-content-block-heading><h2>${heading}</h2></dds-content-block-heading>
155155
<dds-content-block-copy size="${CONTENT_BLOCK_COPY_SIZE.SMALL}">${copy}</dds-content-block-copy>
156156
<dds-video-player-container slot="media" video-id="1_9h94wo6b"></dds-video-player-container>
157157
${ctaStyle === 'card'
@@ -193,7 +193,7 @@ export const WithLinkList = ({ parameters }) => {
193193
const href = 'https://www.example.com';
194194
return html`
195195
<dds-content-block-simple complementary-style-scheme="${ifNonNull(complementaryStyleScheme)}">
196-
<dds-content-block-heading>${heading}</dds-content-block-heading>
196+
<dds-content-block-heading><h2>${heading}</h2></dds-content-block-heading>
197197
<dds-content-block-copy size="${CONTENT_BLOCK_COPY_SIZE.SMALL}">${copy}</dds-content-block-copy>
198198
${image}
199199
<dds-link-list type="default" slot="complementary">

packages/web-components/src/components/content-block/content-block-heading.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/**
22
* @license
33
*
4-
* Copyright IBM Corp. 2020
4+
* Copyright IBM Corp. 2020, 2021
55
*
66
* This source code is licensed under the Apache-2.0 license found in the
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, LitElement, internalProperty } from 'lit-element';
1111
import ddsSettings from '@carbon/ibmdotcom-utilities/es/utilities/settings/settings.js';
12+
import { stripHTML } from '@carbon/ibmdotcom-utilities/es/utilities/stripHTML/index.js';
13+
import { render } from 'lit-html';
1214
import StableSelectorMixin from '../../globals/mixins/stable-selector';
1315
import styles from './content-block.scss';
1416

@@ -27,6 +29,9 @@ class DDSContentBlockHeading extends StableSelectorMixin(LitElement) {
2729
@property({ reflect: true })
2830
slot = 'heading';
2931

32+
@internalProperty()
33+
content = '';
34+
3035
connectedCallback() {
3136
if (!this.hasAttribute('role')) {
3237
this.setAttribute('role', 'heading');
@@ -37,9 +42,19 @@ class DDSContentBlockHeading extends StableSelectorMixin(LitElement) {
3742
super.connectedCallback();
3843
}
3944

45+
firstUpdated() {
46+
this.content = stripHTML(this.innerHTML);
47+
render(
48+
html`
49+
${this.content}
50+
`,
51+
this
52+
);
53+
}
54+
4055
render() {
4156
return html`
42-
<slot></slot>
57+
${this.content}
4358
`;
4459
}
4560

packages/web-components/src/components/content-block/content-block-paragraph.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license
33
*
4-
* Copyright IBM Corp. 2020
4+
* Copyright IBM Corp. 2020, 2021
55
*
66
* This source code is licensed under the Apache-2.0 license found in the
77
* LICENSE file in the root directory of this source tree.

0 commit comments

Comments
 (0)