fix(theme): Fix code block text selection copy on Firefox? (#11565)
Some checks failed
Argos CI / take-screenshots (push) Has been cancelled
Build Hash Router / Build Hash Router (push) Has been cancelled
Canary Release / Publish Canary (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Continuous Releases / Continuous Releases (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (20) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (20.0) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (22) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (24) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (25.1) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 Windows (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (node-modules, -s) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (node-modules, -st) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (pnp, -s) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (pnp, -st) (push) Has been cancelled
E2E Tests / E2E — npm (push) Has been cancelled
E2E Tests / E2E — pnpm (push) Has been cancelled

This commit is contained in:
Sébastien Lorber 2025-11-21 19:19:55 +01:00 committed by GitHub
parent f13adecec0
commit d6cbf6f9e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,16 +14,23 @@ import styles from './styles.module.css';
type Token = Props['line'][number];
// Replaces '\n' by ''
// Historical code, not sure why we even need this :/
// This <br/ seems useful when the line has no content to prevent collapsing.
// For code blocks with "diff" languages, this makes the empty lines collapse to
// zero height lines, which is undesirable.
// See also https://github.com/facebook/docusaurus/pull/11565
function LineBreak() {
return <br />;
}
// Replaces single lines with '\n' by '' so that we don't end up with
// duplicate line breaks (the '\n' + the artificial <br/> above)
// see also https://github.com/facebook/docusaurus/pull/11565
function fixLineBreak(line: Token[]) {
const singleLineBreakToken =
line.length === 1 && line[0]!.content === '\n' ? line[0] : undefined;
if (singleLineBreakToken) {
return [{...singleLineBreakToken, content: ''}];
}
return line;
}
@ -35,7 +42,6 @@ export default function CodeBlockLine({
getTokenProps,
}: Props): ReactNode {
const line = fixLineBreak(lineProp);
const lineProps = getLineProps({
line,
className: clsx(classNames, showLineNumbers && styles.codeLine),
@ -51,7 +57,7 @@ export default function CodeBlockLine({
});
return (
<span {...lineProps}>
<div {...lineProps}>
{showLineNumbers ? (
<>
<span className={styles.codeLineNumber} />
@ -60,7 +66,7 @@ export default function CodeBlockLine({
) : (
lineTokens
)}
<br />
</span>
<LineBreak />
</div>
);
}