Commit fcb5f7d
authored
fix(toc): prevent misnamed mdx components from breaking TOC (#1242)
| [![PR App][icn]][demo] | Fix CX-2543 |
| :--------------------: | :---------: |
## 🧰 Changes
Discovered through [this
ticket](https://linear.app/readme-io/issue/CX-2543/tabapay-toc-does-not-respect-headers-nesting)
that the TOC generated by the MDX renderer would become broken with
miscalculated nesting depths and exposing `h3`s or other heading depths
that shouldn't be included.
This happened when a jsx element was embedded inside the MDX body and
the custom component name did not match the exported jsx elements.
For example:
<img width="2130" height="1016" alt="image"
src="https://github.com/user-attachments/assets/27d9cc8e-cfdd-4b9c-bd55-968df40624a8"
/>
I'm not entirely sure whether this is an edge-case we should be guarding
against from inside our custom component editor. Like, adding validation
to
ensure that the component name matches what's being exported from it. My
gut tells me it'd be really hard to enforce that.
So, the fix (for now) is to simply take this edge-case into account and
add
protections for the cases where this happens.
When a component name doesn't match the jsx element name, it previously
was making its way into the `plugin/toc` flow. But b/c it has no
`tagName`, the `getDepth()` calculation would break and return a `NaN`
instead of a valid integer. This would break the nesting logic in
`toctoHast()` and cause all headers to render at the first level.
Fixed this by:
1. filtering away these mismatched components that make it through
1. additionally updating the `getDepth()` function to be more fail-proof
and handle the case where `tagName` is undefined
| before | after |
|--------|--------|
| <img width="246" height="544" alt="image"
src="https://github.com/user-attachments/assets/d1fe2919-d0be-4c55-b829-44d89e035a6c"
/> | <img width="218" height="396" alt="image"
src="https://github.com/user-attachments/assets/53c6a079-7c41-4450-a428-a37fe5f87512"
/> |
## 🧬 QA & Testing
Tests should pass
- [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.svg1 parent ce55fd3 commit fcb5f7d
2 files changed
+59
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
178 | 214 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
62 | 78 | | |
63 | 79 | | |
64 | 80 | | |
65 | 81 | | |
66 | 82 | | |
67 | 83 | | |
68 | | - | |
| 84 | + | |
| 85 | + | |
69 | 86 | | |
70 | 87 | | |
71 | 88 | | |
| |||
105 | 122 | | |
106 | 123 | | |
107 | 124 | | |
108 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
109 | 129 | | |
110 | 130 | | |
111 | 131 | | |
| |||
0 commit comments