> Could you also add a record to etc/ORG-NEWS? attached an updated patch
- catsup
From 152d2f6d4dd04ffbbd228c1807e690b8ca4aa576 Mon Sep 17 00:00:00 2001 From: Catsup4 <[email protected]> Date: Thu, 9 Apr 2026 15:18:55 +0700 Subject: [PATCH] ox-html.el: support line numbers as a :before element in code blocks * lisp/ox-html.el (org-html-do-format-code): When supported, display the line number of a code block in a ::before pseudo-element so that it isn't included in copy/paste actions. * etc/ORG-NEWS: added a news entry for this change Previously, HTML export of code blocks that included line numbers would prepend the numbers to the same HTML element as the line of code (wrapped in a <span>). Therefore, if you highlighted the line in a browser to copy/paste the code block, the line numbers were also highlighted and copied. Now, the line number is still prepended and wrapped in a <span> in addition to being added to a data attribute, data-linenr, on that <span>. The default CSS has been modified so that: 1. If the browser supports the the attr() CSS function (e.g. firefox), then the inlined line number is hidden and the line number in the data attribute is used to display the number in ::before element on the <span>. 2. If the browser does not support this CSS (e.g. eww), the previously existing behavior is used. This means you can now highlight and copy/paste multiple lines from a code block without having the line numbers included. TINYCHANGE --- etc/ORG-NEWS | 14 ++++++++++++++ lisp/ox-html.el | 12 +++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index c5792cf58..c7e970aa0 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -149,6 +149,20 @@ header argument will be updated. This allows for the cache feature to still work when a block is evaluated indirectly to resolve a reference in another block. +*** ox-html: When able, prevent code block line numbers from being included in copy/paste actions + +Previously, HTML export of code blocks that included line numbers +would prepend the numbers to the same html element as the line of code +(wrapped in a ~<span>~). Therefore, if you highlighted the line in a +browser to copy/paste the code block, the line numbers were also +highlighted and copied. + +Now, the line number is still prepended and wrapped in a ~<span>~ in +addition to being added to a data attribute, ~data-linenr~, on that +~<span>~. The default CSS has been modified so that you can now +highlight and copy/paste multiple lines from a code block without +having the line numbers included. + ** New functions and changes in function arguments # This also includes changes in function behavior from Elisp perspective. diff --git a/lisp/ox-html.el b/lisp/ox-html.el index 71766b7e9..1c7897344 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -441,7 +441,13 @@ This affects IDs that are determined from the ID property.") #org-div-home-and-up { text-align: right; font-size: 70%; white-space: nowrap; } textarea { overflow-x: auto; } - .linenr { font-size: smaller } + .linenr { + font-size: smaller; + @supports (content: attr(data-linenr)) { + visibility: hidden; + &::before { content: attr(data-linenr); visibility: visible; } + } + } .code-highlighted { background-color: #ffff00; } .org-info-js_info-navigation { border-style: none; } #org-info-js_console-label @@ -2473,8 +2479,8 @@ wrapped in code elements." (concat ;; Add line number, if needed. (when num-start - (format "<span class=\"linenr\">%s</span>" - (format num-fmt line-num))) + (let ((ln (format num-fmt line-num))) + (format "<span data-linenr=\"%s\" class=\"linenr\">%s</span>" ln ln))) ;; Transcoded src line. (if wrap-lines (format "<code%s>%s</code>" -- 2.53.0
