FIX: whitespaces when copying code blocks (#12730)

This ensures the copied text always has proper white space characters.
This commit is contained in:
Régis Hanol 2021-04-16 10:28:47 +02:00 committed by GitHub
parent c43a7bb23b
commit 2aed82e646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,8 +7,8 @@ import { withPluginApi } from "discourse/lib/plugin-api";
// http://github.com/feross/clipboard-copy
function clipboardCopy(text) {
// Use the Async Clipboard API when available. Requires a secure browsing
// context (i.e. HTTPS)
// Use the Async Clipboard API when available.
// Requires a secure browsing context (i.e. HTTPS)
if (navigator.clipboard) {
return navigator.clipboard.writeText(text).catch(function (err) {
throw err !== undefined
@ -88,7 +88,15 @@ export default {
const code = button.nextSibling;
if (code) {
clipboardCopy(code.innerText.trim()).then(() => {
// replace any weird whitespace characters with a proper '\u20' whitespace
const text = code.innerText
.replace(
/[\f\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/g,
" "
)
.trim();
clipboardCopy(text).then(() => {
button.classList.add("copied");
const state = button.innerHTML;
button.innerHTML = I18n.t("copy_codeblock.copied");