(block: HTMLPreElement, labels: CopyLabels)
| 201 | } |
| 202 | |
| 203 | function ensureCodeWrapper(block: HTMLPreElement, labels: CopyLabels) { |
| 204 | const parent = block.parentElement |
| 205 | if (!parent) return |
| 206 | const wrapped = parent.getAttribute("data-component") === "markdown-code" |
| 207 | if (!wrapped) { |
| 208 | const wrapper = document.createElement("div") |
| 209 | wrapper.setAttribute("data-component", "markdown-code") |
| 210 | applyCodeMetadata(wrapper, codeLanguage(block)) |
| 211 | parent.replaceChild(wrapper, block) |
| 212 | wrapper.appendChild(block) |
| 213 | wrapper.appendChild(createCopyButton(labels)) |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | applyCodeMetadata(parent, codeLanguage(block)) |
| 218 | |
| 219 | const buttons = Array.from(parent.querySelectorAll('[data-slot="markdown-copy-button"]')).filter( |
| 220 | (el): el is HTMLButtonElement => el instanceof HTMLButtonElement, |
| 221 | ) |
| 222 | |
| 223 | if (buttons.length === 0) { |
| 224 | parent.appendChild(createCopyButton(labels)) |
| 225 | return |
| 226 | } |
| 227 | |
| 228 | for (const button of buttons.slice(1)) { |
| 229 | disposeCopyButton(button) |
| 230 | button.remove() |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | function markCodeLinks(root: HTMLDivElement) { |
| 235 | const codeNodes = Array.from(root.querySelectorAll(":not(pre) > code")) |
no test coverage detected