diff --git a/README.md b/README.md index cb1deeb1d..1a842bc02 100644 --- a/README.md +++ b/README.md @@ -229,4 +229,4 @@ the [Third-Party Notices](src/modules/3rdparty/THIRD_PARTY_NOTICES.md). --- -2023-2024 · Enrico Ros x [big-AGI](https://big-agi.com) · License: [MIT](LICENSE) · Made with 💙 +2023-2024 · Enrico Ros x [Big-AGI](https://big-agi.com) · Like this project? Leave a star! 💫⭐ \ No newline at end of file diff --git a/src/common/util/textUtils.ts b/src/common/util/textUtils.ts index 840570749..a0674fd57 100644 --- a/src/common/util/textUtils.ts +++ b/src/common/util/textUtils.ts @@ -9,6 +9,11 @@ export function countWords(text: string) { return trimmedText.split(/\s+/).length; } +export function countLines(text?: string) { + if (!text) return 0; + return text.split('\n').length; +} + /** * Convert a string (e.g., a web URL or file name) to a human-readable hyphenated format. * This function: diff --git a/src/modules/blocks/AutoBlocksRenderer.tsx b/src/modules/blocks/AutoBlocksRenderer.tsx index 960acaa00..664137615 100644 --- a/src/modules/blocks/AutoBlocksRenderer.tsx +++ b/src/modules/blocks/AutoBlocksRenderer.tsx @@ -144,7 +144,7 @@ export function AutoBlocksRenderer(props: { const RenderCodeMemoOrNot = renderCodeMemoOrNot(optimizeMemoBeforeLastBlock); // Custom handling for some of our blocks - let disableEnhancedRender = bkInput.isPartial; + let disableEnhancedRender = bkInput.isPartial || (!bkInput.title && bkInput.lines <= 3); let enhancedStartCollapsed = false; return (props.codeRenderVariant === 'enhanced' && !disableEnhancedRender) ? ( diff --git a/src/modules/blocks/blocks.hooks.ts b/src/modules/blocks/blocks.hooks.ts index 7eb4f2dae..fb889b5a0 100644 --- a/src/modules/blocks/blocks.hooks.ts +++ b/src/modules/blocks/blocks.hooks.ts @@ -2,6 +2,7 @@ import * as React from 'react'; import type { Diff as SanityTextDiff } from '@sanity/diff-match-patch'; import { agiId } from '~/common/util/idUtils'; +import { countLines } from '~/common/util/textUtils'; import { shallowEquals } from '~/common/util/hooks/useShallowObject'; import type { RenderBlockInputs } from './blocks.types'; @@ -70,7 +71,7 @@ export function useAutoBlocksMemoSemiStable(text: string, forceCodeWithTitle: st return React.useMemo(() => { let newBlocks: RenderBlockInputs; if (forceCodeWithTitle !== undefined) - newBlocks = [{ bkt: 'code-bk', title: forceCodeWithTitle, code: text, isPartial: false }]; + newBlocks = [{ bkt: 'code-bk', title: forceCodeWithTitle, code: text, lines: countLines(text), isPartial: false }]; else if (forceMarkdown) newBlocks = [{ bkt: 'md-bk', content: text }]; else if (forceSanityTextDiffs && forceSanityTextDiffs.length >= 1) diff --git a/src/modules/blocks/blocks.textparser.ts b/src/modules/blocks/blocks.textparser.ts index ef8eb79c2..e7a5852d9 100644 --- a/src/modules/blocks/blocks.textparser.ts +++ b/src/modules/blocks/blocks.textparser.ts @@ -1,6 +1,7 @@ import type { RenderBlockInputs } from './blocks.types'; import { heuristicAllMarkdownImageReferences } from './image/RenderImageURL'; import { heuristicIsBlockPureHTML } from './danger-html/RenderDangerousHtml'; +import { countLines } from '~/common/util/textUtils'; export function parseBlocksFromText(text: string): RenderBlockInputs { @@ -60,16 +61,16 @@ export function parseBlocksFromText(text: string): RenderBlockInputs { // note: we don't trim blockCode to preserve leading spaces, however if the last line is only made of spaces or tabs, we trim that const blockCode: string = match[2].replace(/[\t ]+$/, ''); const blockEnd: string = match[3]; - blocks.push({ bkt: 'code-bk', title: blockTitle, code: blockCode, isPartial: !blockEnd.startsWith('```') }); + blocks.push({ bkt: 'code-bk', title: blockTitle, code: blockCode, lines: countLines(blockCode), isPartial: !blockEnd.startsWith('```') }); break; case 'htmlCodeBlock': const preMatchHtml: string = `${match[1]}`; - blocks.push({ bkt: 'code-bk', title: 'html', code: preMatchHtml, isPartial: false }); + blocks.push({ bkt: 'code-bk', title: 'html', code: preMatchHtml, lines: countLines(preMatchHtml), isPartial: false }); break; case 'svgBlock': - blocks.push({ bkt: 'code-bk', title: 'svg', code: match[0], isPartial: false }); + blocks.push({ bkt: 'code-bk', title: 'svg', code: match[0], lines: countLines(match[0]), isPartial: false }); break; } diff --git a/src/modules/blocks/blocks.types.ts b/src/modules/blocks/blocks.types.ts index e02d1150e..7dceb822f 100644 --- a/src/modules/blocks/blocks.types.ts +++ b/src/modules/blocks/blocks.types.ts @@ -17,6 +17,7 @@ type BlockInput = { bkt: 'code-bk'; title: string; code: string; + lines: number; isPartial: boolean; } | { /* Rendered as HTML (dangerous) */