Skip to content

Commit

Permalink
fix: set default metastring to the title (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored May 1, 2024
1 parent ccefdba commit c14f713
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit c14f713

Please sign in to comment.