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

feat: define prompt lines for bash code #2248

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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 @@ -7,7 +7,7 @@ import { FernSyntaxHighlighterTokensVirtualized } from "./FernSyntaxHighlighterT
import { createRawTokens, highlightTokens, useHighlighter } from "./fernShiki";

// [number, number] is a range of lines to highlight
type HighlightLine = number | [number, number];
type LineNumber = number | [number, number];

export interface FernSyntaxHighlighterProps {
className?: string;
Expand All @@ -16,12 +16,13 @@ export interface FernSyntaxHighlighterProps {
code: string;
language: string;
fontSize?: "sm" | "base" | "lg";
highlightLines?: HighlightLine[];
highlightLines?: LineNumber[];
highlightStyle?: "highlight" | "focus";
viewportRef?: React.RefObject<ScrollToHandle>;
maxLines?: number;
wordWrap?: boolean;
matchLanguage?: string;
promptLines?: LineNumber[];
}

export const FernSyntaxHighlighter = forwardRef<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import type { Element } from "hast";
import { forwardRef, memo, useImperativeHandle, useMemo, useRef } from "react";
import { HastToJSX } from "./HastToJsx";
import { HighlightedTokens } from "./fernShiki";
import {
flattenHighlightLines,
getMaxHeight,
type HighlightLine,
} from "./utils";
import { flattenLineNumbers, getMaxHeight, type LineNumber } from "./utils";

export interface ScrollToHandle {
scrollTo: (options: ScrollToOptions) => void;
Expand All @@ -22,14 +18,15 @@ export interface ScrollToHandle {
export interface FernSyntaxHighlighterTokensProps {
tokens: HighlightedTokens;
fontSize?: "sm" | "base" | "lg";
highlightLines?: HighlightLine[];
highlightLines?: LineNumber[];
highlightStyle?: "highlight" | "focus";

className?: string;
style?: React.CSSProperties;
viewportRef?: React.RefObject<ScrollToHandle>;
maxLines?: number;
wordWrap?: boolean;
promptLines?: LineNumber[];
}

export function fernSyntaxHighlighterTokenPropsAreEqual(
Expand All @@ -38,6 +35,7 @@ export function fernSyntaxHighlighterTokenPropsAreEqual(
): boolean {
return (
isEqual(prevProps.highlightLines, nextProps.highlightLines) &&
isEqual(prevProps.promptLines, nextProps.promptLines) &&
isEqual(prevProps.style, nextProps.style) &&
prevProps.fontSize === nextProps.fontSize &&
prevProps.highlightStyle === nextProps.highlightStyle &&
Expand All @@ -60,6 +58,7 @@ export const FernSyntaxHighlighterTokens = memo(
tokens,
maxLines,
wordWrap,
promptLines,
} = props;
const scrollAreaRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -104,9 +103,13 @@ export const FernSyntaxHighlighterTokens = memo(
}, [tokens.hast]);

const highlightedLines = useMemo(
() => flattenHighlightLines(highlightLines ?? []),
() => flattenLineNumbers(highlightLines ?? []),
[highlightLines]
);
const promptLineNumbers = useMemo(
() => flattenLineNumbers(promptLines ?? [0]),
[promptLines]
);
const lines = useMemo(() => {
const lines: Element[] = [];
visit(tokens.hast, "element", (node) => {
Expand Down Expand Up @@ -172,7 +175,7 @@ export const FernSyntaxHighlighterTokens = memo(
<td className="code-block-line-gutter">
<span>
{gutterCli
? lineNumber === 0
? promptLineNumbers.includes(lineNumber)
? "$"
: ">"
: lineNumber + 1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import {
fernSyntaxHighlighterTokenPropsAreEqual,
} from "./FernSyntaxHighlighterTokens";
import { HastToJSX } from "./HastToJsx";
import { flattenHighlightLines, getLineHeight, getMaxHeight } from "./utils";
import { flattenLineNumbers, getLineHeight, getMaxHeight } from "./utils";

interface CodeBlockContext {
fontSize: "sm" | "base" | "lg";
highlightStyle: "highlight" | "focus" | undefined;
highlightedLines: number[];
promptLineNumbers: number[];
lang: string;
wordWrap?: boolean;
}
Expand Down Expand Up @@ -98,6 +99,7 @@ export const FernSyntaxHighlighterTokensVirtualized = memo(
tokens,
maxLines,
wordWrap,
promptLines,
} = props;

const virtuosoRef = useRef<TableVirtuosoHandle>(null);
Expand Down Expand Up @@ -166,10 +168,18 @@ export const FernSyntaxHighlighterTokensVirtualized = memo(
fontSize,
lang: tokens.lang,
highlightStyle,
highlightedLines: flattenHighlightLines(highlightLines ?? []),
highlightedLines: flattenLineNumbers(highlightLines ?? []),
wordWrap,
promptLineNumbers: flattenLineNumbers(promptLines ?? [0]),
}),
[fontSize, highlightLines, highlightStyle, tokens.lang, wordWrap]
[
fontSize,
highlightLines,
highlightStyle,
tokens.lang,
wordWrap,
promptLines,
]
);

const lang = tokens.lang;
Expand All @@ -185,7 +195,11 @@ export const FernSyntaxHighlighterTokensVirtualized = memo(
{!plaintext && (
<td className="code-block-line-gutter">
<span>
{gutterCli ? (lineNumber === 0 ? "$" : ">") : lineNumber + 1}
{gutterCli
? context.promptLineNumbers.includes(lineNumber)
? "$"
: ">"
: lineNumber + 1}
</span>
</td>
)}
Expand All @@ -194,7 +208,7 @@ export const FernSyntaxHighlighterTokensVirtualized = memo(
</td>
</>
),
[gutterCli, plaintext]
[gutterCli, plaintext, context.promptLineNumbers]
);

return (
Expand Down
8 changes: 3 additions & 5 deletions packages/fern-docs/syntax-highlighter/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// [number, number] is a range of lines to highlight
export type HighlightLine = number | [number, number];
export type LineNumber = number | [number, number];

export function getLineHeight(fontSize: "sm" | "base" | "lg"): number {
return 1.625 * (fontSize === "sm" ? 12 : fontSize === "base" ? 14 : 16);
Expand All @@ -18,10 +18,8 @@ export function getMaxHeight(
return maxLines * lineHeight + (fontSize === "sm" ? 8 : 12) * 2;
}

export function flattenHighlightLines(
highlightLines: HighlightLine[]
): number[] {
return highlightLines.flatMap((lineNumber) => {
export function flattenLineNumbers(lineNumbers: LineNumber[]): number[] {
return lineNumbers.flatMap((lineNumber) => {
if (Array.isArray(lineNumber)) {
const [start, end] = lineNumber;
return Array.from({ length: end - start + 1 }, (_, i) => start + i - 1);
Expand Down
58 changes: 58 additions & 0 deletions packages/fern-docs/ui/src/mdx/__test__/rehypeFernCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,64 @@ describe("parseBlockMetaString", () => {
expect(meta.wordWrap).not.toBe(true);
expect(meta.title).toBe("wordWrap");
});

it("should parse promptLines meta with single value", () => {
const node = createElement("promptLines={1}");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1]);
});

it("should parse promptLines meta with multiple values", () => {
const node = createElement("promptLines={1,2,3}");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1, 2, 3]);
});

it("should parse promptLines meta with spaces", () => {
const node = createElement("promptLines={1, 2, 3}");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1, 2, 3]);
});

it("should parse promptLines meta along with other properties", () => {
const node = createElement("title='Example' promptLines={1,2} focused");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1, 2]);
expect(meta.title).toEqual("Example");
expect(meta.focused).toBe(true);
});

it("should handle empty promptLines", () => {
const node = createElement("promptLines={}");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1]);
});

it("should parse both promptLines and highlights", () => {
const node = createElement("promptLines={1,2} {3,4,5}");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1, 2]);
expect(meta.highlights).toEqual([3, 4, 5]);
});

it("should parse both promptLines and highlights in the reverse order", () => {
const node = createElement("{3,4,5} promptLines={1,2}");
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1, 2]);
expect(meta.highlights).toEqual([3, 4, 5]);
});

it("should parse promptLines and highlights with other properties", () => {
const node = createElement(
"title='Example' promptLines={1,2} {3,4} focused wordWrap"
);
const meta = parseBlockMetaString(node);
expect(meta.promptLines).toEqual([1, 2]);
expect(meta.highlights).toEqual([3, 4]);
expect(meta.title).toEqual("Example");
expect(meta.focused).toBe(true);
expect(meta.wordWrap).toBe(true);
});
});

function createElement(metastring: string, lang = "plaintext"): Hast.Element {
Expand Down
14 changes: 14 additions & 0 deletions packages/fern-docs/ui/src/mdx/plugins/rehypeFernCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function rehypeFernCode(): (tree: Hast.Root) => void {
maxLines: meta.maxLines,
wordWrap: meta.wordWrap,
matchLanguage: meta.matchLanguage,
promptLines: meta.promptLines,
};
if (meta.title == null) {
parent?.children.splice(index, 1, {
Expand Down Expand Up @@ -135,6 +136,7 @@ interface CodeBlockItem {
title: string | undefined;
wordWrap: boolean | undefined;
matchLanguage: string | undefined;
promptLines: number[];
}

function visitCodeBlockNodes(nodeToVisit: MdxJsxElementHast) {
Expand Down Expand Up @@ -166,6 +168,7 @@ function visitCodeBlockNodes(nodeToVisit: MdxJsxElementHast) {
: undefined),
wordWrap: meta.wordWrap,
matchLanguage: meta.matchLanguage,
promptLines: meta.promptLines,
});
}
}
Expand All @@ -191,6 +194,7 @@ function visitCodeBlockNodes(nodeToVisit: MdxJsxElementHast) {
title: meta.title,
wordWrap: meta.wordWrap,
matchLanguage: meta.matchLanguage,
promptLines: meta.promptLines,
});
}
}
Expand Down Expand Up @@ -226,6 +230,7 @@ interface FernCodeMeta {
wordWrap?: boolean;
highlights: number[];
matchLanguage: string | undefined;
promptLines: number[];
}

function maybeParseInt(str: string | null | undefined): number | undefined {
Expand Down Expand Up @@ -290,6 +295,14 @@ export function parseBlockMetaString(
const match = matchLanguage?.[1] ?? matchLanguage?.[2];
meta = meta.replace(matchLanguage?.[0] ?? "", "");

const promptLinesMatch = meta.match(/promptLines=\{(.*?)\}/);
const promptLines = promptLinesMatch?.[1]
? rangeParser(promptLinesMatch[1])
: [1];
meta = meta
.replace(promptLinesMatch?.[0] ?? "", "")
.replace(promptLinesMatch?.[1] ?? "", "");

const [highlights, strippedMeta] = parseHighlightedLineNumbers(meta);
meta = strippedMeta;

Expand Down Expand Up @@ -318,6 +331,7 @@ export function parseBlockMetaString(
wordWrap: wordWrap != null,
highlights,
matchLanguage: match,
promptLines,
};
}

Expand Down