Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(main): allow code elements to work in h2/h3 headings with anchor links #7292

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/components/MDXComponents/MDXHeading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import slug from './utils/slug';

export const MDXHeading = (props) => {
const { level, children } = props;

let href = '';

/* Test if children element is not a string before creating the url slug */
if (children && typeof children != 'string') {
let newChildren = '';
for (let i = 0; i < children.length; i++) {
if (typeof children[i] == 'string') {
newChildren = newChildren + children[i];
} else if (typeof children[i] != 'string') {
newChildren = newChildren + children[i].props.children;
/* Test if child element is a single object */
if (typeof children == 'object' && children.props?.children) {
newChildren = children.props.children;
} else {
/* If not a single object, we expect an array of
elements and loop through them */
for (let i = 0; i < children.length; i++) {
if (typeof children[i] == 'string') {
newChildren = newChildren + children[i];
} else {
newChildren = newChildren + children[i].props.children;
}
}
}
href = `${slug(newChildren)}`;
} else {
/* If children element is a string, use that to create the url slug */
href = `${slug(children)}`;
}

Expand Down
82 changes: 82 additions & 0 deletions src/components/MDXComponents/__tests__/MDXHeading.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { render, screen } from '@testing-library/react';
import { MDXHeading } from '../MDXHeading';

describe('MDXHeading', () => {
it('should render H2 with string and anchor link', () => {
const props = {
level: 2,
children: 'Test heading'
};
render(<MDXHeading {...props} />);

const heading = screen.queryByRole('heading', { level: 2 });
const link = screen.queryByRole('link');
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(props.children);
expect(link).toHaveAttribute(
'href',
expect.stringMatching(/#test-heading/)
);
});

it('should render H3 with string and anchor link', () => {
const props = {
level: 3,
children: 'Test heading'
};
render(<MDXHeading {...props} />);

const heading = screen.queryByRole('heading', { level: 3 });
const link = screen.queryByRole('link');

expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(props.children);
expect(link).toHaveAttribute(
'href',
expect.stringMatching(/#test-heading/)
);
});

it('should render H4 with string and no anchor link', () => {
const props = {
level: 4,
children: 'Test heading'
};
render(<MDXHeading {...props} />);

const heading = screen.queryByRole('heading', { level: 4 });
const link = screen.queryByRole('link');
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent(props.children);
expect(link).not.toBeInTheDocument();
});

it('should render H2 with HTML code element and anchor link', () => {
const props = {
level: 2,
children: <code>runtime</code>
};
render(<MDXHeading {...props} />);

const heading = screen.queryByRole('heading', { level: 2 });
const link = screen.queryByRole('link', { name: 'runtime' });
expect(heading).toBeInTheDocument();
expect(link).toHaveAttribute('href', expect.stringMatching(/#runtime/));
});

it('should render H2 with mixed elements and anchor link', () => {
render(
<MDXHeading level={2}>
<code>runtime</code> and <code>test</code>
</MDXHeading>
);

const heading = screen.queryByRole('heading', { level: 2 });
const link = screen.queryByRole('link', { name: 'runtime and test' });
expect(heading).toBeInTheDocument();
expect(link).toHaveAttribute(
'href',
expect.stringMatching(/#runtime-and-test/)
);
});
});
Loading