Skip to content

Commit a49781e

Browse files
authored
Expand code block when printing (#3782)
1 parent 065a5e6 commit a49781e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

.changeset/frank-doors-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Printable collapsed code block

packages/gitbook/src/components/DocumentView/CodeBlock/ClientCodeBlock.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { plainHighlight } from './plain-highlight';
1919
type ClientBlockProps = Pick<BlockProps<DocumentBlockCode>, 'block' | 'style'> & {
2020
inlines: RenderedInline[];
2121
inlineExprVariables: InlineExpressionVariables;
22+
mode: BlockProps<DocumentBlockCode>['context']['mode'];
2223
};
2324

2425
export const CODE_BLOCK_DEFAULT_COLLAPSED_LINE_COUNT = 10;
@@ -28,7 +29,7 @@ export const CODE_BLOCK_DEFAULT_COLLAPSED_LINE_COUNT = 10;
2829
* It allows us to defer some load to avoid blocking the rendering of the whole page with block highlighting.
2930
*/
3031
export function ClientCodeBlock(props: ClientBlockProps) {
31-
const { block, style, inlines, inlineExprVariables } = props;
32+
const { block, mode, style, inlines, inlineExprVariables } = props;
3233
const blockRef = useRef<HTMLDivElement>(null);
3334
const isInViewportRef = useRef(false);
3435
const [isInViewport, setIsInViewport] = useState(false);
@@ -123,7 +124,9 @@ export function ClientCodeBlock(props: ClientBlockProps) {
123124
const numberOfLinesOfCode = lines?.length ?? plainLines.length;
124125
const collapsedLineCount =
125126
block.data.collapsedLineCount || CODE_BLOCK_DEFAULT_COLLAPSED_LINE_COUNT;
126-
const isExpandable = Boolean(expandable && numberOfLinesOfCode > collapsedLineCount);
127+
const isExpandable = Boolean(
128+
expandable && mode !== 'print' && numberOfLinesOfCode > collapsedLineCount
129+
);
127130

128131
const codeBlockBodyId = useId();
129132

packages/gitbook/src/components/DocumentView/CodeBlock/CodeBlock.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export async function CodeBlock(props: BlockProps<DocumentBlockCode>) {
7373
style={style}
7474
inlines={richInlines}
7575
inlineExprVariables={variables}
76+
mode={context.mode}
7677
/>
7778
</React.Suspense>
7879
);

0 commit comments

Comments
 (0)