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

[Enhanced code blocks] Copy button should not omit lines with no text by default #222

Merged
merged 1 commit into from
Nov 9, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ const CONSOLE_COPY_LINES_MAP_FN = (line: HTMLElement) => {
* map/filter method to extract text from each line.
*
* @param codeblock The codeblock whose lines need to be copied
* @param mapFn (OPTIONAL) A method that extracts text from a given line HTMLElement
* @param mapFn (OPTIONAL) A method that extracts text from a given line
* HTMLElement. If the method returns `null`, the line is
* *omitted* (and a newline will not be copied).
*/
async function copyLines(
codeblock: HTMLElement,
Expand All @@ -97,7 +99,9 @@ async function copyLines(
const lines = codeblock.querySelectorAll(
`.${CODEBLOCK_LINE_CLASS}`,
) as NodeListOf<HTMLElement>;
const linesOfText = [...lines].map((line) => mapFn(line)).filter(Boolean);
const linesOfText = [...lines]
.map((line) => mapFn(line))
.filter((lineText) => lineText != null);
const text = linesOfText.join('\n');
await navigator.clipboard.writeText(text);
}