render
This commit is contained in:
@@ -93,3 +93,36 @@ def collect_css(context):
|
|||||||
css_files.append(css)
|
css_files.append(css)
|
||||||
|
|
||||||
return css_files
|
return css_files
|
||||||
|
|
||||||
|
def generate_preview(content, max_length=200):
|
||||||
|
if isinstance(content, str):
|
||||||
|
# old system
|
||||||
|
text = re.sub(r"<[^>]+>", "", content) # strip HTML
|
||||||
|
return text[:max_length]
|
||||||
|
|
||||||
|
elif isinstance(content, list):
|
||||||
|
# new system
|
||||||
|
for block in content:
|
||||||
|
if block.get("type") == "text":
|
||||||
|
text = re.sub(r"<[^>]+>", "", block.get("value", ""))
|
||||||
|
return text[:max_length]
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
# optinal, for images
|
||||||
|
def generate_preview_html(content, context=None):
|
||||||
|
if isinstance(content, list):
|
||||||
|
for block in content:
|
||||||
|
if block.get("type") == "text":
|
||||||
|
return f"<div class='blog-preview'>{block.get('value')}</div>"
|
||||||
|
|
||||||
|
elif block.get("type") in COMPONENTS:
|
||||||
|
# optional: allow image preview
|
||||||
|
render_func = COMPONENTS[block["type"]]["render"]
|
||||||
|
kwargs = {k: v for k, v in block.items() if k != "type"}
|
||||||
|
return render_func(**kwargs, context=context)
|
||||||
|
|
||||||
|
elif isinstance(content, str):
|
||||||
|
return content[:200]
|
||||||
|
|
||||||
|
return ""
|
||||||
Reference in New Issue
Block a user