Blocks: disable Enhanced rendering for untitled, below 3 lines

This commit is contained in:
Enrico Ros
2024-10-17 15:41:31 -07:00
parent 915bc6cc89
commit a525b7437a
6 changed files with 14 additions and 6 deletions
+1 -1
View File
@@ -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! 💫⭐
+5
View File
@@ -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:
+1 -1
View File
@@ -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) ? (
+2 -1
View File
@@ -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)
+4 -3
View File
@@ -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 = `<!DOCTYPE html>${match[1]}</html>`;
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;
}
+1
View File
@@ -17,6 +17,7 @@ type BlockInput = {
bkt: 'code-bk';
title: string;
code: string;
lines: number;
isPartial: boolean;
} | {
/* Rendered as HTML (dangerous) */