From 12424139bd02ee13b213e49f165961239798a519 Mon Sep 17 00:00:00 2001 From: Rafe Goldberg Date: Fri, 11 Oct 2024 11:45:48 -0400 Subject: [PATCH] fix: respect the top-level `copyButtons` option (#990) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit | 🎫 Resolves RM-10635 | 🐙 Closes #964 | | :------------------: | :------------: | ## 🧰 Changes The main `RMDX.compile()` method should respect the `copyButtons` param when rendering our code block components. - [x] **Accept the `copyButtons` option**—we used to set this param directly on our custom Unified `processor`, but since Remark actually freeze's it's processor instance we can achieve a similar effect by [passing it directly to the `CodeTabsTransformer` plugin as a configuration option](https://github.com/readmeio/markdown/blob/96f9644f04e6d8e3ffff6f9c014432f901c0b804/lib/compile.ts#L28): https://github.com/readmeio/markdown/blob/96f9644f04e6d8e3ffff6f9c014432f901c0b804/lib/compile.ts#L28 - [x] Add a **Copy Buttons** toggle to the dev playground.[^1] - [x] Miscellaneous detailing and minor cleanup. ## 🧬 QA & Testing Pull this PR down, run the `start` script, and [navigate to the “Code Blocks” example](http://localhost:9966/#/codeBlockTests?copyButtons=true "View local dev playground→"). Try hovering over a code block to verify that the copy button is only shown if/when the **Copy Buttons** toggle is checked. [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]: @kellyjosephprice—do we even respect those other two global config options? I think `safeMode` and `lazyImages` both have toggles in the playground, but [there are a bunch of others which I'm not sure are still valid](https://github.com/readmeio/markdown/blob/b9502adb306f099cd91e005df17c0be252019814/options.js#L1-L22) either? Anyways, just an aside, but it would be nice to clean this all up at some point! --------- Co-authored-by: Trisha Le --- components/Code/index.tsx | 6 +- example/Doc.tsx | 6 +- example/Form.tsx | 9 +++ lib/compile.ts | 15 +++-- processor/transform/code-tabs.ts | 99 ++++++++++++++++---------------- processor/transform/index.ts | 13 ++++- 6 files changed, 87 insertions(+), 61 deletions(-) diff --git a/components/Code/index.tsx b/components/Code/index.tsx index 1add62749..e489094cf 100644 --- a/components/Code/index.tsx +++ b/components/Code/index.tsx @@ -16,20 +16,20 @@ if (typeof window !== 'undefined') { function CopyCode({ codeRef, rootClass = 'rdmd-code-copy', className = '' }) { const copyClass = `${rootClass}_copied`; - const button = createRef(); + const buttonRef = createRef(); const copier = () => { const code = codeRef.current.textContent; if (copy(code)) { - const el = button.current; + const el = buttonRef.current; el.classList.add(copyClass); setTimeout(() => el.classList.remove(copyClass), 1500); } }; - return