Skip to content

Commit

Permalink
Merge pull request #44 from otovo/espenra/custom-serializer-after-lis…
Browse files Browse the repository at this point in the history
…t-bug

Custom serializer + list bug
  • Loading branch information
Kakebake authored Jun 21, 2022
2 parents 31718a7 + 8e895e3 commit ce9ecbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion portabletext_html/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def render(self) -> str:

if list_nodes and not is_list(node):
tree = self._normalize_list_tree(list_nodes)
result += ''.join([self._render_node(n, Block(**node), list_item=True) for n in tree])
result += ''.join([self._render_node(n, list_item=True) for n in tree])
list_nodes = [] # reset list_nodes

if is_list(node):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = 'portabletext-html'
version = '1.1.2'
version = '1.1.3'
description = "HTML renderer for Sanity's Portable Text format"
homepage = 'https://github.com/otovo/python-sanity-html'
repository = 'https://github.com/otovo/python-sanity-html'
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/custom_serializer_node_after_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"_key": "e5b6e416e6e9",
"_type": "block",
"children": [
{ "_key": "3bbbff0f158b", "_type": "span", "marks": [], "text": "resers" }
],
"level": 1,
"listItem": "bullet",
"markDefs": [],
"style": "normal"
},
{
"_key": "73405dda68e0",
"_type": "extraInfoBlock",
"extraInfo": "This informations is not supported by Block",
"markDefs": [],
"style": "normal"
}
]
15 changes: 15 additions & 0 deletions tests/test_rendering.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import html
import json
from pathlib import Path
from typing import Optional

import pytest

from portabletext_html.renderer import MissingSerializerError, UnhandledNodeError, render
from portabletext_html.types import Block


def extraInfoSerializer(node: dict, context: Optional[Block], list_item: bool) -> str:
extraInfo = node.get('extraInfo')

return f'<p>{extraInfo}</p>'


def load_fixture(fixture_name) -> dict:
Expand Down Expand Up @@ -59,3 +67,10 @@ def test_invalid_node():
fixture = load_fixture('invalid_node.json')
with pytest.raises(UnhandledNodeError):
render(fixture)


def test_custom_serializer_node_after_list():
fixture = load_fixture('custom_serializer_node_after_list.json')
output = render(fixture, custom_serializers={'extraInfoBlock': extraInfoSerializer})

assert output == '<div><ul><li>resers</li></ul><p>This informations is not supported by Block</p></div>'

0 comments on commit ce9ecbb

Please sign in to comment.