Skip to content

add copy button for lstlistings#2629

Open
evoludolab wants to merge 5 commits into
brucemiller:masterfrom
evoludolab:lstlisting
Open

add copy button for lstlistings#2629
evoludolab wants to merge 5 commits into
brucemiller:masterfrom
evoludolab:lstlisting

Conversation

@evoludolab
Copy link
Copy Markdown
Contributor

This PR aims to address #2588.

The lstlisting.js adds a hook to replace all download links in HTML documents by a download button after the document has been loaded.

Following @dginev's encouragement, I polished my JavaScript and created my own icons to avoid any copyright issues. All SVG icons are in the CSS in order to minimize the number of files, while maintaining customizability.

I marked the PR as a draft because I'm struggling with

  • where to make changes or place the files,
  • how to best name things,
  • how to activate this JS extension during post-processing.

Currently, I pass a JS file to LaTeXML with --javascript=<custom.js>, which includes the code from lstlisting.js and the resulting buttons work as expected.

I'm looking forward to your feedback/guidance on this - or watch what changes you make to the code 😄.

@evoludolab evoludolab marked this pull request as draft September 6, 2025 23:06
@evoludolab
Copy link
Copy Markdown
Contributor Author

I had marked this PR as a draft only because I wasn't sure where the CSS and JS code for the copy button of lstlistings should go... Nothing has changed but I will now mark it as ready for review - just bear with me that the above will require some input and guidance from your side.

@evoludolab evoludolab marked this pull request as ready for review January 30, 2026 19:12
@evoludolab evoludolab changed the title [WIP] add copy button for lstlistings add copy button for lstlistings May 16, 2026
- fix 'Illegal character in scheme data: “<” is not allowed'
@brucemiller
Copy link
Copy Markdown
Owner

Oooh, I like it! I'm gonna want to use it myself!

But I'm a little concerned about naming & packaging. For those that use listing, but not the clipboard, the CSS load is a bit heavy. It would probably be good to separate that out and name both files ltx-listing-clipboard.(css|js); the user would have to add both:

--css=ltx-listing-clipboard.css --javascript=ltx-listing-clipboard.js

Kinda long. Or possibly even just ltx-clipboard.(css|js) if it seems that it might (eventually) be adaptable to other clipboardable data?

@brucemiller
Copy link
Copy Markdown
Owner

Oh, and whatever names, they'll need to be added to the MANIFEST file

@xworld21
Copy link
Copy Markdown
Contributor

Very quick comment, because I was looking into this just today as I'd personally like to have both copy & dowload buttons... The clipboard bits can be implemented directly into an onclick handler as

url=event.target.nextElementSibling.href;clipboard.writeText(atob(subtring(url.substring(url.indexOf(',')+1))))

assuming you add the button right in front of the existing download link (code may be slightly wrong). That's more robust than hiding the line numbers, selecting the text etc.

I also wanted to point out that the download button has its uses, e.g. when the file name is known (because it was inserted by reading a file, or by specifying the @name attribute). E.g. try the download button of https://vlmantova.github.io/bookml/get-started.html#get-started-on-overleaf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants