Skip to content

Commit 5f28c5e

Browse files
authored
feat(TableOfContents): add prop to control TOC heading label (#1207)
[![PR App][icn]][demo] | Fix RM-XYZ :-------------------:|:----------: ## 🧰 Changes Add `heading` prop to the `Content.Toc` component so we can control it in the React app for internationalization ## 🧬 QA & Testing - [Broken on production][prod]. - [Working in this PR app][demo]. [demo]: https://markdown-pr-PR_NUMBER.herokuapp.com [prod]: https://SUBDOMAIN.readme.io [icn]: https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
1 parent f60630e commit 5f28c5e

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

__tests__/components/TableOfContents.test.jsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const glossaryTerms = [
1616
},
1717
];
1818

19-
describe.skip('Table of Contents', () => {
19+
describe('Table of Contents', () => {
2020
it('should have a header', () => {
2121
const { container } = render(
2222
<TableOfContents>
@@ -27,7 +27,7 @@ describe.skip('Table of Contents', () => {
2727
expect(container.querySelectorAll('li')[0]).toHaveTextContent('Table of Contents');
2828
});
2929

30-
it('generates TOC from headings', () => {
30+
it.skip('generates TOC from headings', () => {
3131
const txt = '# Heading Zed\n\n# Heading One';
3232
const ast = reactProcessor().parse(txt);
3333
const toc = reactTOC(ast);
@@ -36,7 +36,7 @@ describe.skip('Table of Contents', () => {
3636
expect(container.querySelectorAll('li > a[href]:not([href="#"])')).toHaveLength(2);
3737
});
3838

39-
it('includes two heading levels', () => {
39+
it.skip('includes two heading levels', () => {
4040
const txt = '# Heading Zed\n\n## Subheading One\n\n### Deep Heading Two';
4141
const ast = reactProcessor().parse(txt);
4242
const toc = reactTOC(ast);
@@ -46,7 +46,7 @@ describe.skip('Table of Contents', () => {
4646
expect(container.innerHTML).toMatchSnapshot();
4747
});
4848

49-
it('normalizes root depth level', () => {
49+
it.skip('normalizes root depth level', () => {
5050
const txt = '##### Heading Zed\n\n###### Subheading Zed';
5151
const ast = reactProcessor().parse(txt);
5252
const toc = reactTOC(ast);
@@ -55,7 +55,7 @@ describe.skip('Table of Contents', () => {
5555
expect(container.querySelectorAll('li > a[href]:not([href="#"])')).toHaveLength(2);
5656
});
5757

58-
it('includes variables', () => {
58+
it.skip('includes variables', () => {
5959
const txt = '# Heading <<test>>';
6060
const ast = reactProcessor().parse(txt);
6161
const toc = reactTOC(ast);
@@ -64,7 +64,7 @@ describe.skip('Table of Contents', () => {
6464
expect(container.querySelector('li > a[href]:not([href="#"])')).toHaveTextContent(`Heading ${variables.user.test}`);
6565
});
6666

67-
it('includes glossary items', () => {
67+
it.skip('includes glossary items', () => {
6868
const txt = '# Heading <<glossary:demo>>';
6969
const ast = reactProcessor().parse(txt);
7070
const toc = reactTOC(ast);
@@ -74,4 +74,14 @@ describe.skip('Table of Contents', () => {
7474
`Heading ${glossaryTerms[0].term}`,
7575
);
7676
});
77+
78+
it('accepts custom heading', () => {
79+
const { container } = render(
80+
<TableOfContents heading="Custom Heading">
81+
<h1>Heading 1</h1>
82+
</TableOfContents>,
83+
);
84+
85+
expect(container.querySelectorAll('li')[0]).toHaveTextContent('Custom Heading');
86+
});
7787
});

components/TableOfContents/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react';
22

3-
function TableOfContents({ children }: React.PropsWithChildren) {
3+
function TableOfContents({ children, heading = 'Table of Contents' }: React.PropsWithChildren<{ heading?: string }>) {
44
return (
55
<nav aria-label="Table of contents" role="navigation">
66
<ul className="toc-list">
77
<li>
88
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
99
<a className="tocHeader" href="#">
1010
<i className="icon icon-text-align-left"></i>
11-
Table of Contents
11+
{heading}
1212
</a>
1313
</li>
1414
<li className="toc-children">{children}</li>

lib/run.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ const run = (string: string, _opts: RunOpts = {}) => {
7676
</Contexts>
7777
),
7878
toc,
79-
Toc: props =>
79+
Toc: (props: { heading?: string }) =>
8080
Toc ? (
81-
<Components.TableOfContents>
82-
<Toc {...props} />
81+
<Components.TableOfContents heading={props.heading}>
82+
<Toc />
8383
</Components.TableOfContents>
8484
) : null,
8585
stylesheet,

0 commit comments

Comments
 (0)