diff --git a/packages/mdx/dev/content/rows.mdx b/packages/mdx/dev/content/rows.mdx new file mode 100644 index 00000000..2ee7962d --- /dev/null +++ b/packages/mdx/dev/content/rows.mdx @@ -0,0 +1,67 @@ + + +```js +console.log(2) +``` + + + + + +```js +console.log(1) +console.log(2) +console.log(3) +console.log(4) +console.log(5) +console.log(6) +console.log(7) +``` + + + + + +```js focus=3:4,6[1:11] +console.log(1) +console.log(2) +console.log(3) +console.log(4) +console.log(5) +console.log(6) +console.log(7) +``` + + + + + +```js +console.log(1) +console.log(2) +console.log(3) +console.log(4) +console.log(5) +// focus +console.log(600000) +console.log(7) +``` + + + + + +```js foo.js +console.log(1) +``` + +```js bar.js +console.log(1) +console.log(2) +console.log(3) +console.log(4) +console.log(5) +console.log(7) +``` + + diff --git a/packages/mdx/src/mdx-client/code.tsx b/packages/mdx/src/mdx-client/code.tsx index 23f456f3..604aca1e 100644 --- a/packages/mdx/src/mdx-client/code.tsx +++ b/packages/mdx/src/mdx-client/code.tsx @@ -55,6 +55,7 @@ export function mergeCodeConfig( showExpandButton == null ? props.codeConfig?.showExpandButton : showExpandButton, + rows: props.rows, debug: props.debug ?? props.codeConfig?.debug, } return { ...rest, codeConfig } diff --git a/packages/mdx/src/smooth-code/code-tween.tsx b/packages/mdx/src/smooth-code/code-tween.tsx index 03c75af2..0c98852a 100644 --- a/packages/mdx/src/smooth-code/code-tween.tsx +++ b/packages/mdx/src/smooth-code/code-tween.tsx @@ -48,6 +48,7 @@ export type CodeConfig = { showCopyButton?: boolean showExpandButton?: boolean staticMediaQuery?: string + rows?: number | "focus" debug?: boolean } @@ -85,6 +86,7 @@ export function CodeTween({ map(tween, tween => tween.focus), config.minColumns || DEFAULT_MIN_COLUMNS, config.lineNumbers || false, + config.rows, [config.parentHeight] ) diff --git a/packages/mdx/src/smooth-code/use-dimensions.tsx b/packages/mdx/src/smooth-code/use-dimensions.tsx index 217294d1..80679304 100644 --- a/packages/mdx/src/smooth-code/use-dimensions.tsx +++ b/packages/mdx/src/smooth-code/use-dimensions.tsx @@ -1,6 +1,7 @@ import React from "react" import { FocusString, + getFocusExtremes, getFocusIndexes, Tween, useLayoutEffect, @@ -38,6 +39,7 @@ function useDimensions( focus: Tween, minColumns: number, lineNumbers: boolean, + rows: number | "focus" | undefined, deps: React.DependencyList ): { element: React.ReactNode; dimensions: Dimensions } { const [dimensions, setDimensions] = @@ -61,7 +63,35 @@ function useDimensions( .trimEnd() .split(newlineRe) - const lineCount = lines.length + const originalLineCount = lines.length + + if (rows) { + // make the lines match the requested number of rows + const heightInLines = + rows === "focus" + ? focusHeightInLines(focus, lines) + : rows + let i = lines.length + + while (i < heightInLines) { + lines.push("") + i++ + } + + // remove extra lines to match the requested rows + while (i > heightInLines) { + lines.pop() + i-- + } + + // if we removed prevLongestLine, add it back + if ( + prevLongestLine && + !lines.includes(prevLongestLine) + ) { + lines[lines.length - 1] = prevLongestLine + } + } // avod setting the ref more than once https://github.com/code-hike/codehike/issues/232 let prevLineRefSet = false @@ -78,7 +108,7 @@ function useDimensions(
{lineNumbers ? ( - _{lineCount} + _{originalLineCount} ) : undefined}
, + lines: any[] +): number { + const [start, end] = getFocusExtremes(focus.prev, lines) + return end - start + 1 +} diff --git a/packages/mdx/src/utils/focus.ts b/packages/mdx/src/utils/focus.ts index 4c30a1f1..bf733366 100644 --- a/packages/mdx/src/utils/focus.ts +++ b/packages/mdx/src/utils/focus.ts @@ -102,7 +102,7 @@ export function parseExtremes(part: string) { /** * Return the first and last indexes to focus, both included */ -function getFocusExtremes( +export function getFocusExtremes( focus: FocusString, lines: any[] ): [number, number] {