Adds a copy-to-clipboard button to code blocks, plus an optional language label. Ported to Flarum 2.0 from ffans/clipboardjs.
composer require linkrobins/clipboard
Then enable it in your forum's admin panel under Extensions.
Adds a copy-to-clipboard button to every code block in posts, plus an optional language label in the top-right. Seven visual themes to choose from.
Ported to Flarum 2.0 from ffans/clipboardjs (MIT). Credit to Golden for the original.
Two features:
<pre> in a post, a small "copy" button appears. Click it; the code is on their clipboard. The button briefly flips to a checkmark to confirm. Seven button styles to match the look of GitHub, CSDN, Jianshu, etc.<code> with a language-X class, the extension hoists that class onto the parent <pre> and the CSS shows the language name in the top-right of the block.Both features have independent toggles in the admin settings; you can use one without the other.
The extension extends CommentPost.oncreate and, after each post renders, walks the post DOM for <pre> elements. For each one not already decorated, it inserts a themed button. Click handlers use the modern native navigator.clipboard.writeText() API to copy code text, with a hidden-textarea + document.execCommand('copy') fallback for older browsers or non-secure contexts.
The language detection sets up a MutationObserver once per page that watches <code> elements for a language-X class. When one appears (after a syntax highlighter runs), the class is mirrored onto the parent <pre> so the CSS selectors in _languages.less can display the language name.
flarum/core ^2.0linkrobins-clipboard.*clipboard npm dependency — modern browsers have a native clipboard API, and the original library is now obsoleteSaveSettings listener — it used Flarum\Api\Event\Serializing which was removed in Flarum 2.0, and its work is redundant with the serializeToForum extender calls already in extend.phpnode_modulesapp.registry.for('linkrobins-clipboard') (the 2.0 name) instead of app.extensionData.for(...)switch → boolean (both work; boolean is the modern name)LinkRobinsClipboard (the .clipboard button class stays the same so themes from upstream still apply)