mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-06 20:15:17 +02:00
refactor(editor): remove truncation logic and related properties from editor content response
This commit is contained in:
parent
640238e1af
commit
f19f31e51c
4 changed files with 1 additions and 22 deletions
|
|
@ -41,21 +41,10 @@ router = APIRouter()
|
||||||
EDITOR_PLATE_MAX_BYTES = 5 * 1024 * 1024
|
EDITOR_PLATE_MAX_BYTES = 5 * 1024 * 1024
|
||||||
|
|
||||||
|
|
||||||
def _truncate_utf8(text: str, max_bytes: int) -> str:
|
|
||||||
encoded = text.encode("utf-8")
|
|
||||||
if len(encoded) <= max_bytes:
|
|
||||||
return text
|
|
||||||
return encoded[:max_bytes].decode("utf-8", errors="ignore")
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/search-spaces/{search_space_id}/documents/{document_id}/editor-content")
|
@router.get("/search-spaces/{search_space_id}/documents/{document_id}/editor-content")
|
||||||
async def get_editor_content(
|
async def get_editor_content(
|
||||||
search_space_id: int,
|
search_space_id: int,
|
||||||
document_id: int,
|
document_id: int,
|
||||||
max_length: int | None = Query(
|
|
||||||
None,
|
|
||||||
description="Truncate source_markdown to this many UTF-8 bytes. Defaults to the Plate editor byte limit.",
|
|
||||||
),
|
|
||||||
session: AsyncSession = Depends(get_async_session),
|
session: AsyncSession = Depends(get_async_session),
|
||||||
user: User = Depends(current_active_user),
|
user: User = Depends(current_active_user),
|
||||||
):
|
):
|
||||||
|
|
@ -95,20 +84,13 @@ async def get_editor_content(
|
||||||
def _build_response(md: str) -> dict:
|
def _build_response(md: str) -> dict:
|
||||||
size_bytes = len(md.encode("utf-8"))
|
size_bytes = len(md.encode("utf-8"))
|
||||||
viewer_mode = "monaco" if size_bytes > EDITOR_PLATE_MAX_BYTES else "plate"
|
viewer_mode = "monaco" if size_bytes > EDITOR_PLATE_MAX_BYTES else "plate"
|
||||||
content_limit = max_length if max_length is not None else EDITOR_PLATE_MAX_BYTES
|
|
||||||
truncated = False
|
|
||||||
output_md = md
|
|
||||||
if size_bytes > content_limit:
|
|
||||||
output_md = _truncate_utf8(md, content_limit)
|
|
||||||
truncated = True
|
|
||||||
return {
|
return {
|
||||||
"document_id": document.id,
|
"document_id": document.id,
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"document_type": document.document_type.value,
|
"document_type": document.document_type.value,
|
||||||
"source_markdown": output_md,
|
"source_markdown": md,
|
||||||
"content_size_bytes": size_bytes,
|
"content_size_bytes": size_bytes,
|
||||||
"chunk_count": chunk_count,
|
"chunk_count": chunk_count,
|
||||||
"truncated": truncated,
|
|
||||||
"viewer_mode": viewer_mode,
|
"viewer_mode": viewer_mode,
|
||||||
"editor_plate_max_bytes": EDITOR_PLATE_MAX_BYTES,
|
"editor_plate_max_bytes": EDITOR_PLATE_MAX_BYTES,
|
||||||
"updated_at": document.updated_at.isoformat()
|
"updated_at": document.updated_at.isoformat()
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,6 @@ interface EditorContent {
|
||||||
source_markdown: string;
|
source_markdown: string;
|
||||||
content_size_bytes?: number;
|
content_size_bytes?: number;
|
||||||
chunk_count?: number;
|
chunk_count?: number;
|
||||||
truncated?: boolean;
|
|
||||||
viewer_mode?: ViewerMode;
|
viewer_mode?: ViewerMode;
|
||||||
editor_plate_max_bytes?: number;
|
editor_plate_max_bytes?: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@ interface DocumentContent {
|
||||||
source_markdown: string;
|
source_markdown: string;
|
||||||
content_size_bytes?: number;
|
content_size_bytes?: number;
|
||||||
chunk_count?: number;
|
chunk_count?: number;
|
||||||
truncated?: boolean;
|
|
||||||
viewer_mode?: ViewerMode;
|
viewer_mode?: ViewerMode;
|
||||||
editor_plate_max_bytes?: number;
|
editor_plate_max_bytes?: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ export type EditorContent = {
|
||||||
source_markdown: string;
|
source_markdown: string;
|
||||||
content_size_bytes: number;
|
content_size_bytes: number;
|
||||||
chunk_count: number;
|
chunk_count: number;
|
||||||
truncated: boolean;
|
|
||||||
viewer_mode?: "plate" | "monaco";
|
viewer_mode?: "plate" | "monaco";
|
||||||
editor_plate_max_bytes?: number;
|
editor_plate_max_bytes?: number;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue