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

fix: set default metastring to the title #762

Merged
merged 2 commits into from
May 1, 2024
Merged
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
19 changes: 16 additions & 3 deletions packages/ui/app/src/mdx/plugins/rehypeFernCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,21 @@ function maybeParseInt(str: string | null | undefined): number | undefined {
}

export function parseBlockMetaString(element: Element, defaultFallback: string): FernCodeMeta {
let meta: string = unknownToString(element.data?.meta ?? element.properties?.metastring ?? "");
const originalMeta: string = unknownToString(element.data?.meta ?? element.properties?.metastring ?? "").trim();
let meta = originalMeta;

const titleMatch = meta.match(/title="([^"]*)"/);
const title = titleMatch?.[1];
const titleMatch = meta.match(/title="((?:[^"\\]|\\.)*)"/);
let title = titleMatch?.[1];
meta = meta.replace(titleMatch?.[0] ?? "", "");

const fileName = meta.match(/fileName="((?:[^"\\]|\\.)*)"/);
title = title ?? fileName?.[1];
meta = meta.replace(fileName?.[0] ?? "", "");

const filename = meta.match(/filename="((?:[^"\\]|\\.)*)"/);
title = title ?? filename?.[1];
meta = meta.replace(filename?.[0] ?? "", "");

// i.e. maxLines=20 (default is 20)
const maxLinesMatch = meta.match(/maxLines=(\d+)/);
let maxLines: number | undefined = maybeParseInt(maxLinesMatch?.[1]) ?? 20;
Expand All @@ -217,6 +226,10 @@ export function parseBlockMetaString(element: Element, defaultFallback: string):
meta = meta.replace(focused[0], "");
}

if (originalMeta === meta && meta.length > 0) {
title = meta;
}

let lang = defaultFallback;
if (
element.properties &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const FernSyntaxHighlighter = forwardRef<HTMLPreElement, FernSyntaxHighli
const lines = code.split("\n").length;

const TokenRenderer =
(maxLines != null && lines <= maxLines + 100) || lines <= 500
(maxLines != null && lines <= maxLines + 100) || lines <= 500 || maxLines == null
? FernSyntaxHighlighterTokens
: FernSyntaxHighlighterTokensVirtualized;

Expand Down
Loading