Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3a84cdd

Browse filesBrowse files
author
Oleksandr Ivanov
authored
Merge pull request #38 from otovo/feature/render-text-custom-mark
bugfix: call custom mark render_text() on render
2 parents 5bf33f9 + e3ca1b1 commit 3a84cdd
Copy full SHA for 3a84cdd

File tree

Expand file treeCollapse file tree

3 files changed

+21
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+21
-3
lines changed

‎portabletext_html/renderer.py

Copy file name to clipboardExpand all lines: portabletext_html/renderer.py
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,19 @@ def _render_span(self, span: Span, block: Block) -> str:
150150
marker_callable = block.marker_definitions.get(mark, DefaultMarkerDefinition)()
151151
result += marker_callable.render_prefix(span, mark, block)
152152

153-
result += html.escape(span.text).replace('\n', '<br/>')
153+
# to avoid rendering the text multiple times,
154+
# only the first custom mark will be used
155+
custom_mark_text_rendered = False
156+
if sorted_marks:
157+
for mark in sorted_marks:
158+
if custom_mark_text_rendered or mark in prev_marks:
159+
continue
160+
marker_callable = block.marker_definitions.get(mark, DefaultMarkerDefinition)()
161+
result += marker_callable.render_text(span, mark, block)
162+
custom_mark_text_rendered = True
163+
164+
if not custom_mark_text_rendered:
165+
result += html.escape(span.text).replace('\n', '<br/>')
154166

155167
for mark in reversed(sorted_marks):
156168
if mark in next_marks:

‎portabletext_html/types.py

Copy file name to clipboardExpand all lines: portabletext_html/types.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _add_custom_marker_definitions(self) -> dict[str, Type[MarkerDefinition]]:
7373
if definition['_type'] in self.marker_definitions:
7474
marker = self.marker_definitions[definition['_type']]
7575
marker_definitions[definition['_key']] = marker
76-
del marker_definitions[definition['_type']]
76+
# del marker_definitions[definition['_type']]
7777
return marker_definitions
7878

7979
def get_node_siblings(self, node: Union[dict, Span]) -> Tuple[Optional[dict], Optional[dict]]:

‎tests/test_marker_definitions.py

Copy file name to clipboardExpand all lines: tests/test_marker_definitions.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ def render_prefix(cls: Type[MarkerDefinition], span: Span, marker: str, context:
8383
else:
8484
return super().render_prefix(span, marker, context)
8585

86+
@classmethod
87+
def render_text(cls: Type[MarkerDefinition], span: Span, marker: str, context: Block) -> str:
88+
marker_definition = next((md for md in context.markDefs if md['_key'] == marker), None)
89+
condition = marker_definition.get('cloudCondition', '')
90+
return span.text if not condition else ''
91+
8692
renderer = PortableTextRenderer(
87-
{
93+
blocks={
8894
'_type': 'block',
8995
'children': [{'_key': 'a1ph4', '_type': 'span', 'marks': ['some_id'], 'text': 'Sanity'}],
9096
'markDefs': [{'_key': 'some_id', '_type': 'contractConditional', 'cloudCondition': False}],

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.