Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codeblock copy button copies the syntax name instead of just the code #1284

Open
TFWol opened this issue Dec 24, 2024 · 4 comments
Open

codeblock copy button copies the syntax name instead of just the code #1284

TFWol opened this issue Dec 24, 2024 · 4 comments

Comments

@TFWol
Copy link

TFWol commented Dec 24, 2024

Describe the Issue
If you have a named codeblock, like batch, it gets placed inside the code and ends up getting copied along with the code which can cause syntax errors if you aren't watching for it.

It should be a label or something outside the box instead.

image

image

image

@Luro223
Copy link

Luro223 commented Dec 25, 2024

@TFWol Does this happen with KoboldAI only? If yes then check Repetition penalty and DRY.

@LostRuins
Copy link
Owner

This has got nothing to do with rep pen. It's a UI behavior.

@TFWol currently the copy button does not check for syntax and just copies the entirety of the contents of a code block. Some models generate labels and some do not, so it's hard to correctly match them all. For now i'd recommend manually ensuring that unwanted text is excluded.

@TFWol
Copy link
Author

TFWol commented Jan 3, 2025

When there isn't a label, the element starts with a <br>, otherwise it's the syntax label.
A simple solution could be to have the copy button skip the first line of the codeblock.

I tried to make a userscript, but it didn't work out so great.

✅ Successfully got userscript snippet working without any visible changes to the UI:

(function() {
    'use strict';

    function modifyCopyCode() {
        // Check if the function already exists to avoid conflicts
        if (typeof copyMarkdownCode === 'function') {

          const originalCopyMarkdownCode = copyMarkdownCode;

          copyMarkdownCode = function(btn) {
              const codeContainer = btn.parentElement.querySelector('pre code');
              if (codeContainer) {
                  let codeText = codeContainer.innerText;
                  let codeLines = codeText.split('\n');
                  codeLines.shift(); // This gets the job done by removing the first line
                  let modifiedCodeText = codeLines.join('\n');
                  navigator.clipboard.writeText(modifiedCodeText);
              }
            }
        } else {
          console.error("copyMarkdownCode function not found. Make sure the script loads after the target page.");
        }
    }

    // Call the modifyCopyCode function when the page loads
    window.addEventListener('load', modifyCopyCode);
})();

@TFWol
Copy link
Author

TFWol commented Jan 7, 2025

@LostRuins Could you replace
https://github.com/LostRuins/koboldcpp/blob/9b324820897fe08d47b4bfec36d3648a14164ed7/klite.embd#L4958-L4963

function copyMarkdownCode(btn)
{
	const codeContainer = btn.parentElement.querySelector('pre code');
	//selectElementContents(codeContainer);
	navigator.clipboard.writeText(codeContainer.innerText);
}

with:

function copyMarkdownCode(btn)
{
	const codeContainer = btn.parentElement.querySelector('pre code');
	//selectElementContents(codeContainer);
	if (codeContainer) 
	{
		let codeText = codeContainer.innerText;
		let codeLines = codeText.split('\n');
		codeLines.shift(); // Remove the first line
		let modifiedCodeText = codeLines.join('\n');
		navigator.clipboard.writeText(modifiedCodeText);
	}
}

I'd do a pull request, but I'm not familiar with the warning on the file:

image

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

No branches or pull requests

3 participants