Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14bddb8

Browse files
committed
Sort mark rendering by total frequency in the block.
Ensures that we prioritize starting with the "broadest" mark capturing the most content. I suspect there are lurking bugs here on huge blocks, but this makes the test suite pass for now
1 parent 4340c11 commit 14bddb8

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

sanity_html/dataclasses.py

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Block:
4545
children: list[dict] = field(default_factory=list)
4646
markDefs: list[dict] = field(default_factory=list)
4747
marker_definitions: dict[str, Type[MarkerDefinition]] = field(init=False)
48+
marker_frequencies: dict[str, int] = field(init=False)
4849

4950
def __post_init__(self) -> None:
5051
"""
@@ -54,6 +55,17 @@ def __post_init__(self) -> None:
5455
we can directly look up both annotation marks or decorator marks.
5556
"""
5657
self.marker_definitions = get_marker_definitions(self.markDefs)
58+
self.marker_frequencies = self._compute_marker_frequencies()
59+
60+
def _compute_marker_frequencies(self) -> dict[str, int]:
61+
counts: dict[str, int] = {}
62+
for child in self.children:
63+
for mark in child.get('marks', []):
64+
if mark in counts:
65+
counts[mark] += 1
66+
else:
67+
counts[mark] = 0
68+
return counts
5769

5870
def get_node_siblings(self, node: Union[dict, Span]) -> Tuple[Optional[dict], Optional[dict]]:
5971
"""Return the sibling nodes (prev, next) to the given node."""

sanity_html/renderer.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,16 @@ def _render_span(self, span: Span, block: Block) -> str:
102102
prev_marks = prev_node.get('marks', []) if prev_node else []
103103
next_marks = next_node.get('marks', []) if next_node else []
104104

105-
for mark in span.marks:
105+
sorted_marks = sorted(span.marks, key=lambda x: -block.marker_frequencies[x])
106+
for mark in sorted_marks:
106107
if mark in prev_marks:
107108
continue
108109
marker_callable = block.marker_definitions[mark]()
109110
result += marker_callable.render_prefix(span, mark, block)
110111

111112
result += html.escape(span.text)
112113

113-
for mark in reversed(span.marks):
114+
for mark in reversed(sorted_marks):
114115
if mark in next_marks:
115116
continue
116117

Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
{"input":{"_type":"block","children":[{"_key":"a1ph4","_type":"span","marks":["zomgLink"],"text":"Sanity"},{"_key":"b374","_type":"span","marks":[],"text":" can be used to power almost any "},{"_key":"ch4r1i3","_type":"span","marks":["zomgLink","strong","em"],"text":"app"},{"_key":"d3174","_type":"span","marks":["em","zomgLink"],"text":" or website"},{"_key":"ech0","_type":"span","marks":[],"text":"."}],"markDefs":[{"_key":"zomgLink","_type":"link","href":"https://sanity.io/"}],"style":"blockquote"},"output":"<blockquote><a href=\"https://sanity.io/\">Sanity</a> can be used to power almost any <a href=\"https://sanity.io/\"><em><strong>app</strong> or website</em></a>.</blockquote>"}
1+
{
2+
"input": {
3+
"_type": "block",
4+
"children": [
5+
{
6+
"_key": "a1ph4",
7+
"_type": "span",
8+
"marks": ["zomgLink"],
9+
"text": "Sanity"
10+
},
11+
{
12+
"_key": "b374",
13+
"_type": "span",
14+
"marks": [],
15+
"text": " can be used to power almost any "
16+
},
17+
{
18+
"_key": "ch4r1i3",
19+
"_type": "span",
20+
"marks": ["zomgLink", "strong", "em"],
21+
"text": "app"
22+
},
23+
{
24+
"_key": "d3174",
25+
"_type": "span",
26+
"marks": ["em", "zomgLink"],
27+
"text": " or website"
28+
},
29+
{ "_key": "ech0", "_type": "span", "marks": [], "text": "." }
30+
],
31+
"markDefs": [
32+
{ "_key": "zomgLink", "_type": "link", "href": "https://sanity.io/" }
33+
],
34+
"style": "blockquote"
35+
},
36+
"output": "<blockquote><a href=\"https://sanity.io/\">Sanity</a> can be used to power almost any <a href=\"https://sanity.io/\"><em><strong>app</strong> or website</em></a>.</blockquote>"
37+
}

0 commit comments

Comments
 (0)