Files
lasuite-docs/src/backend/core/utils/yjs.py
T
Manuel RaynaudandAnthony LC d398fb6d5c ✨(backend) duplicate a document through the collaboration server
its stateis fetched from yhub and seeded into the copy instead
of being copied from the content stored by Django
2026-09-09 17:43:59 +02:00

49 lines
1.2 KiB
Python

"""Yjs document conversion utilities."""
import base64
import re
import pycrdt
from bs4 import BeautifulSoup
from core import enums
def yjs_to_xml(update):
"""Extract xml from a raw yjs update."""
doc = pycrdt.Doc()
doc.apply_update(update)
return str(doc.get("document-store", type=pycrdt.XmlFragment))
def base64_yjs_to_xml(base64_string):
"""Extract xml from base64 yjs document."""
return yjs_to_xml(base64.b64decode(base64_string))
def base64_yjs_to_text(base64_string):
"""Extract text from base64 yjs document."""
blocknote_structure = base64_yjs_to_xml(base64_string)
soup = BeautifulSoup(blocknote_structure, "lxml-xml")
return soup.get_text(separator=" ", strip=True)
def extract_attachments(content):
"""Helper method to extract media paths from a document's content."""
if not content:
return []
xml_content = base64_yjs_to_xml(content)
return re.findall(enums.MEDIA_STORAGE_URL_EXTRACT, xml_content)
def extract_attachments_from_update(update):
"""Helper method to extract media paths from a raw yjs update."""
if not update:
return []
return re.findall(enums.MEDIA_STORAGE_URL_EXTRACT, yjs_to_xml(update))