Skip to content

Strip $ symbol from code block text #1110

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ window.onunload = function () { };
// Global variable, shared between modules
function playpen_text(playpen) {
let code_block = playpen.querySelector("code");
let regex = /^\$(?: )/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a more explicit variable name for this regex.

Suggested change
let regex = /^\$(?: )/;
let regex_leading_dollar_space = /^\$(?: )/;


if (window.ace && code_block.classList.contains("editable")) {
let editor = window.ace.edit(code_block);
return editor.getValue();
} else {
return code_block.textContent;
// It is common for code blocks to contain $ in the text. If they do, clipboard will copy that symbol and the ensuing paste into terminal will fail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is great because it explains the motivation. I would specify that the dollar sign convention is common for bash / shell specifically, not all code blocks. AndI would expand this comment to mention briefly how this is solved (stripping dollar signs)

if (regex.test(code_block.textContent)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if necessary? If we do a replace and nothing matches we are going to retrieve the input string at the output, no?

let clean_string = code_block.textContent.replace(regex, "")
return clean_string;
} else {
return code_block.textContent;
}
}
}

Expand Down