复制粘贴到支持彩色文本的地方时,语法高亮不会被复制

当我从 Discourse 复制以下内容到我的电子邮件客户端时:

@@ -1,11 +1,11 @@
-青年志愿者奖——表彰那些在本地社区中付出额外努力、展现我们的价值观以及连接与行动重要性的 16 至 24 岁青年志愿者。
+青年志愿者奖旨在表彰那些在本地社区中付出额外努力、展现我们的价值观以及连接与行动重要性的 16 至 24 岁青年志愿者。
 
 您是否认识一位年轻人,他:
 
-曾参与邻里守望志愿服务,并为社区带来积极影响。
+• 曾参与邻里守望志愿服务,并为社区带来积极影响?
 
-牺牲自己的时间支持本地社区(食品银行、社区中心和团体、无家可归者、宗教团体)
+• 牺牲自己的时间支持本地社区(食品银行、社区中心和团体、无家可归者、宗教团体)?
 
-与学校或学院一起组织筹款活动,或组织过捡垃圾或其他本地活动。
+• 与学校或学院一起组织过筹款活动,或组织过捡垃圾或其他本地活动?
 
-您认为这位青年志愿者值得因其辛勤工作和对社区的承诺而获得认可。
+如果是这样,您是否认为这位青年志愿者值得因其辛勤工作和对社区的承诺而获得认可?

……颜色并未被复制。

你用过 diff 吗?也许你的邮件客户端不支持它?

- 这里是一些文本
+ 这里是一些更多的文本

@Lilly,是的。在 Discourse 上,若要正确渲染语法高亮,必须指定 diff

  1. ```
    @@ -1,11 +1,11 @@
    -Young volunteer award – celebrating young volunteers, aged 16-24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.
    +The Young Volunteer Award celebrates young volunteers, aged 16–24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.
     
     Do you know a young person who:
     
    -Has been volunteering with Neighbourhood Watch and making a positive impact in their community.
    +• Has been volunteering with Neighbourhood Watch and making a positive impact in their community?
     
    -Gives up their time to support their local community (foodbanks, community hubs and groups, homeless, faith groups)
    +• Gives up their time to support their local community (foodbanks, community hubs and groups, homeless, faith groups)?
     
    -Held fundraising activities with their school or college or have organised litter picks or other local events.
    +• Held fundraising activities with their school or college or have organised litter picks or other local events?
     
    -You think this young volunteer deserves to be recognised for their hard work and commitment to their community.
    +If so, do you think this young volunteer deserves to be recognised for their hard work and commitment to their community?
    ```
    
  2. @@ -1,11 +1,11 @@
    -Young volunteer award – celebrating young volunteers, aged 16-24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.
    +The Young Volunteer Award celebrates young volunteers, aged 16–24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.
     
     Do you know a young person who:
     
    -Has been volunteering with Neighbourhood Watch and making a positive impact in their community.
    +• Has been volunteering with Neighbourhood Watch and making a positive impact in their community?
     
    -Gives up their time to support their local community (foodbanks, community hubs and groups, homeless, faith groups)
    +• Gives up their time to support their local community (foodbanks, community hubs and groups, homeless, faith groups)?
     
    -Held fundraising activities with their school or college or have organised litter picks or other local events.
    +• Held fundraising activities with their school or college or have organised litter picks or other local events?
     
    -You think this young volunteer deserves to be recognised for their hard work and commitment to their community.
    +If so, do you think this young volunteer deserves to be recognised for their hard work and commitment to their community?
    

你指的是我在我的客户端中编写 CommonMark 吗?

如果是这样,那属于其他地方的讨论范围:

相反,我指的是直接从此处渲染后的标记中复制内容,例如:

是的,我当然知道这一点,所以我才问你。

@Lilly,不过你可以在 /1 中看到我已经使用了它。

哦,抱歉,我一开始以为那是截图。

1 个赞

今天,这取决于你如何复制。

使用复制按钮:

只会将文本版本(以及以下内容)放入剪贴板:

TIMESTAMP,TARGETS,SAVE_TARGETS,MULTIPLE,UTF8_STRING,STRING,TEXT,text/plain,text/plain;charset=utf-8,chromium/x-internal-source-rfh-token,chromium/x-source-url

但直接复制实际文本则有效:

因为此时你是通过浏览器复制渲染后的 HTML,而不是让 Discourse 与剪贴板交互,因此文本/HTML 格式对剪贴板使用者可用:

<span class="hljs-meta" style="color: rgb(133, 133, 133); font-style: italic; font-family: &quot;JetBrains Mono&quot;, Consolas, Monaco, monospace; font-size: 15.75px; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@@ -1,11 +1,11 @@</span><span style="color: rgb(199, 199, 199); font-family: &quot;JetBrains Mono&quot;, Consolas, Monaco, monospace; font-size: 15.75px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgba(0, 0, 0, 0.25); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">\n</span><span class="hljs-deletion" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(79, 24, 11); color: rgb(222, 113, 118); font-family: &quot;JetBrains Mono&quot;, Consolas, Monaco, monospace; font-size: 15.75px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">-Young volunteer award – celebrating young volunteers, aged 16-24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.</span><span style="color: rgb(199, 199, 199); font-family: &quot;JetBrains Mono&quot;, Consolas, Monaco, monospace; font-size: 15.75px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgba(0, 0, 0, 0.25); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">\n</span><span class="hljs-addition" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(11, 66, 32); color: rgb(118, 196, 144); font-family: &quot;JetBrains Mono&quot;, Consolas, Monaco, monospace; font-size: 15.75px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">+The Young Volunteer Award celebrates young volunteers, aged 16–24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.</span><span style="color: rgb(199, 199, 199); font-family: &quot;JetBrains Mono&quot;, Consolas, Monaco, monospace; font-size: 15.75px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgba(0, 0, 0, 0.25); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">\n</span><br class="Apple-interchange-newline">

若要通过复制按钮支持此功能,我们需要添加一项能力:获取渲染后的带色版本(因为颜色是由某个库生成的),并以 text/html 格式将其发送到剪贴板。这很可能是可行的(甚至可能通过主题组件实现),但在此之前,你始终可以选择手动选中文本进行复制。

3 个赞

@supermathie,谢谢。那么,问题似乎出在我的客户端(protonMail/webClients,5.0.115.3 β):

Screencast20260522180439

不过,颜色高亮也无法复制到 thunderbird-149.0.1-2.fc44 中,尽管它似乎对您有效:

您是否知道有什么不同的操作方式(如果有的话)?

当你复制到 ProtonMail 时,由于它是一个 Web 客户端,看起来似乎使用的是纯文本版本。请注意缺乏格式区分,不过由于你覆盖了字体,所以很难判断。

对比一下粘贴到 Thunderbird 的情况:

它捕获了部分格式,但并非全部。

如果我复制,它会捕获所有内容

我想要解决这个问题,你需要区分是:

  • 并非所有样式都被复制
  • 并非所有样式都被粘贴

从一个没有扩展程序的干净浏览器开始可能会有所帮助?例如,如果我使用一个没有扩展程序的隐私浏览器,我会得到:

我编写的这个 脚本 也能让你看到剪贴板的内容,或许会有帮助。

(或者也许是 Arch » Fedora :face_with_tongue:

1 个赞

@supermathie是因为 ProtonMail 的默认样式表糟糕透顶。

根据你的指导,我已经确认是 Firefox 本身(甚至不是我的配置文件,因为在新创建的 about:profiles 配置中也会复现此问题)无法复制它:

非常感谢!

没错!我也遇到了同样的行为。

严格来说,这并非一个缺陷。Firefox 只是完全按照请求复制了 HTML:

<meta http-equiv="content-type" content="text/html; charset=utf-8">
<p dir="auto">当我从 Discourse 复制以下内容到我的邮件客户端时:</p>
<pre data-code-wrap="diff" dir="auto" class="codeblock-buttons">
		<div class="codeblock-button-wrapper" style="right: 0px;"></div>
		<code class="lang-diff hljs language-diff" data-highlighted="yes">
			<span class="hljs-meta">@@ -1,11 +1,11 @@</span>
			<span class="hljs-deletion">-Young volunteer award – celebrating young volunteers, aged 16-24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.</span>
			<span class="hljs-addition">+The Young Volunteer Award celebrates young volunteers, aged 16–24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.</span>
		</code>
	</pre>

粘贴后产生的差异源于 Chromium 将样式内联,而 Firefox 则没有。将其简化为单行可以突显[1] 正在发生的情况:

<span
  class="hljs-deletion">
-Young volunteer award – celebrating young volunteers, aged 16-24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.
</span>
<span
  class="hljs-deletion"
  style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 224, 224); color: rgb(192, 45, 46);">
-Young volunteer award – celebrating young volunteers, aged 16-24, who go the extra mile in their local communities, demonstrating our values and the importance of connection and action.
</span>

(已添加换行符)

Chromium 会将来自 class="hljs-deletion" 的内联 CSS style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 224, 224); color: rgb(192, 45, 46);" 内联到 span 标签中,因为它粘贴到的目标可能没有该样式。


  1. 哈哈 ↩︎