mirror of
https://github.com/enricoros/big-AGI.git
synced 2026-05-10 21:50:14 -07:00
Blocks: disable Enhanced rendering for untitled, below 3 lines
This commit is contained in:
@@ -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! 💫⭐
|
||||
@@ -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:
|
||||
|
||||
@@ -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,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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ type BlockInput = {
|
||||
bkt: 'code-bk';
|
||||
title: string;
|
||||
code: string;
|
||||
lines: number;
|
||||
isPartial: boolean;
|
||||
} | {
|
||||
/* Rendered as HTML (dangerous) */
|
||||
|
||||
Reference in New Issue
Block a user