Skip to content

Commit 2e73da9

Browse files
committed
fix: Removable heaingIds and fixed tests
1 parent 026e4f1 commit 2e73da9

File tree

5 files changed

+315
-56
lines changed

5 files changed

+315
-56
lines changed

src/Markdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
MarkdownCodeProvider,
99
MarkdownRenderers,
1010
MarkdownRenderersProvider,
11-
MarkedOptions,
11+
MarkdownOptions,
1212
TokensRenderer,
1313
ValidHighlightCodeOptions,
1414
} from "./renderers";
@@ -42,7 +42,7 @@ export interface BaseMarkdownProps extends HighlightCodeOptions {
4242
* In addition, the `mangle` option is set to `false` by default since it
4343
* would prevent emails from being displayed correctly.
4444
*/
45-
options?: Readonly<MarkedOptions>;
45+
options?: Readonly<MarkdownOptions>;
4646

4747
/**
4848
* An optional slugger to provide that generates unique ids for different

src/__tests__/Markdown.tsx

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import "prismjs/components/prism-typescript";
1717
import { renderers } from "../../components/renderers";
1818
import { DEFAULT_MARKDOWN } from "../../constants";
1919
import { Markdown } from "../Markdown";
20-
import type { DangerouslyHighlightCode, GetCodeLanguage } from "../renderers";
20+
import {
21+
DangerouslyHighlightCode,
22+
DEFAULT_MARKDOWN_OPTIONS,
23+
GetCodeLanguage,
24+
MarkdownOptions,
25+
} from "../renderers";
2126

2227
const HEADING_MARKDOWN = `
2328
# Heading 1
@@ -85,15 +90,31 @@ _Using Single Underscore_
8590
`;
8691

8792
const BOLD_TEXT_MARKDOWN = `
88-
___Using Triple Underscore____
93+
__Using Double Underscore___
8994
90-
***Using Triple Asterisk***
95+
**Using Double Asterisk**
9196
`;
9297

9398
const STRIKETHROUGH_TEXT_MARKDOWN = `
9499
~~This text has strikethroughs~~
95100
`;
96101

102+
const EMPHASIS_BOLD_STRIKETHROUGH_TEXT_MARKDOWN = `
103+
___Should be emphasis and bold.___
104+
105+
***Should be emphasis and bold.***
106+
107+
*__Should be emphasis and bold.__*
108+
109+
__*Should be emphasis and bold.*__
110+
111+
_**Should be emphasis and bold.**_
112+
113+
**_Should be emphasis and bold._**
114+
115+
~~**_Should be emphasis, bold, and strikethrough._**~~
116+
`;
117+
97118
const ESCAPED_MARKDOWN = `
98119
Here is some \\* escaped \\* stuff.
99120
`;
@@ -181,6 +202,20 @@ Content Cell | Content Cell
181202
| Content Cell | Content Cell |
182203
`;
183204

205+
// https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables
206+
const TABLE_COMPLEX_MARKDOWN = `
207+
| Left-aligned | Center-aligned | Right-aligned |
208+
| :--- | :---: | ---: |
209+
| \`git status\` | List all *new or modified* files |
210+
| \`git diff\` | Show file differences that **haven't been** staged |
211+
212+
213+
| Name | Character |
214+
| --- | --- |
215+
| Backtick | \` |
216+
| Pipe | \\| |
217+
`;
218+
184219
const FOLDABLE_TEXT_MARKDOWN = `
185220
<details>
186221
<summary>Title 1</summary>
@@ -193,8 +228,8 @@ const FOLDABLE_TEXT_MARKDOWN = `
193228
`;
194229

195230
describe("Markdown", () => {
196-
it("should be able to render all six heading types", () => {
197-
const { container, getByRole } = render(
231+
it("should be able to render all six heading types with or without ids", () => {
232+
const { container, getByRole, rerender } = render(
198233
<Markdown markdown={HEADING_MARKDOWN} />
199234
);
200235
const heading1 = getByRole("heading", { name: "Heading 1" });
@@ -205,16 +240,40 @@ describe("Markdown", () => {
205240
const heading6 = getByRole("heading", { name: "Heading 6" });
206241

207242
expect(heading1.tagName).toBe("H1");
243+
expect(heading1).toHaveAttribute("id", "heading-1");
208244
expect(heading2.tagName).toBe("H2");
245+
expect(heading2).toHaveAttribute("id", "heading-2");
209246
expect(heading3.tagName).toBe("H3");
247+
expect(heading3).toHaveAttribute("id", "heading-3");
210248
expect(heading4.tagName).toBe("H4");
249+
expect(heading4).toHaveAttribute("id", "heading-4");
211250
expect(heading5.tagName).toBe("H5");
251+
expect(heading5).toHaveAttribute("id", "heading-5");
212252
expect(heading6.tagName).toBe("H6");
253+
expect(heading6).toHaveAttribute("id", "heading-6");
254+
expect(container).toMatchSnapshot();
213255

256+
const options: MarkdownOptions = {
257+
...DEFAULT_MARKDOWN_OPTIONS,
258+
headerIds: false,
259+
};
260+
rerender(<Markdown options={options} markdown={HEADING_MARKDOWN} />);
261+
expect(heading1.tagName).toBe("H1");
262+
expect(heading1).not.toHaveAttribute("id");
263+
expect(heading2.tagName).toBe("H2");
264+
expect(heading2).not.toHaveAttribute("id");
265+
expect(heading3.tagName).toBe("H3");
266+
expect(heading3).not.toHaveAttribute("id");
267+
expect(heading4.tagName).toBe("H4");
268+
expect(heading4).not.toHaveAttribute("id");
269+
expect(heading5.tagName).toBe("H5");
270+
expect(heading5).not.toHaveAttribute("id");
271+
expect(heading6.tagName).toBe("H6");
272+
expect(heading6).not.toHaveAttribute("id");
214273
expect(container).toMatchSnapshot();
215274
});
216275

217-
it("should be able to rendering h1 elements with equal signs", () => {
276+
it("should be able to render h1 elements with equal signs", () => {
218277
const { container, getByRole } = render(
219278
<Markdown markdown={HEADING_1_WITH_EQUALS_MARKDOWN} />
220279
);
@@ -229,7 +288,7 @@ describe("Markdown", () => {
229288
expect(container).toMatchSnapshot();
230289
});
231290

232-
it("should be able to rendering h1 elements with hyphens", () => {
291+
it("should be able to render h2 elements with hyphens", () => {
233292
const { container, getByRole } = render(
234293
<Markdown markdown={HEADING_2_WITH_HYPHENS_MARKDOWN} />
235294
);
@@ -298,15 +357,18 @@ describe("Markdown", () => {
298357

299358
it("should be able to render blockquotes", () => {
300359
const { container } = render(<Markdown markdown={BLOCKQUOTE_MARKDOWN} />);
301-
expect(container).toMatchSnapshot();
302360
expect(document.querySelector("blockquote")).toBeInTheDocument();
361+
expect(container).toMatchSnapshot();
303362
});
304363

305364
it("should be able to render nested blockquotes", () => {
306365
const { container } = render(
307366
<Markdown markdown={NESTED_BLOCKQUOTE_MARKDOWN} />
308367
);
309368
expect(document.querySelectorAll("blockquote")?.length).toBe(2);
369+
expect(
370+
document.querySelector("blockquote")?.querySelector("blockquote")
371+
).not.toBe(null);
310372

311373
expect(container).toMatchSnapshot();
312374
});
@@ -336,6 +398,13 @@ describe("Markdown", () => {
336398
expect(container).toMatchSnapshot();
337399
});
338400

401+
it("should be able to render text that combines emphasis, strong, and strikethrough text", () => {
402+
const { container } = render(
403+
<Markdown markdown={EMPHASIS_BOLD_STRIKETHROUGH_TEXT_MARKDOWN} />
404+
);
405+
expect(container).toMatchSnapshot();
406+
});
407+
339408
it("should be able to render escaped (backslash-prefixed) text", () => {
340409
const { container } = render(<Markdown markdown={ESCAPED_MARKDOWN} />);
341410

@@ -504,6 +573,13 @@ describe("Markdown", () => {
504573
const { container } = render(<Markdown markdown={TABLE_MARKDOWN} />);
505574
expect(container).toMatchSnapshot();
506575
});
576+
577+
it("should be able to render tables with complex markdown", () => {
578+
const { container } = render(
579+
<Markdown markdown={TABLE_COMPLEX_MARKDOWN} />
580+
);
581+
expect(container).toMatchSnapshot();
582+
});
507583
});
508584

509585
// TODO: Eventually support this out of the box. It is unsupported right now

0 commit comments

Comments
 (0)