Skip to content

Commit 358dd15

Browse files
committed
Add index indicators for pretty lists
1 parent ea97287 commit 358dd15

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

djangocms_rest/plugin_rendering.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ def escapestr(s: str) -> str:
8888
return escape(json.dumps(s)[1:-1]) # Remove quotes added by json.dumps
8989

9090

91+
def numbered_join(
92+
items: Iterable[str],
93+
separator: str = ", ",
94+
start: int = 0,
95+
) -> str:
96+
"""
97+
Join items with a separator, numbering them starting from `start`.
98+
"""
99+
return separator.join(
100+
f'{item}<span data-i="{i + start}" class="list-item"></span>'
101+
for i, item in enumerate(items)
102+
)
103+
104+
91105
def highlight_data(json_data: Any, drop_frame: bool = False) -> str:
92106
"""
93107
Highlight single JSON data element.
@@ -127,7 +141,7 @@ def highlight_list(json_data: list) -> dict[str, str]:
127141
return {
128142
"open": "[",
129143
"close": "]",
130-
"value": ",<br>".join(items),
144+
"value": numbered_join(items, ",<br>"),
131145
}
132146

133147

@@ -171,7 +185,7 @@ def highlight_json(
171185
items.append(
172186
DETAILS_TEMPLATE.format(
173187
key=escape(field),
174-
value=",".join(children) + marker,
188+
value=numbered_join(children, ",") + marker,
175189
open="[",
176190
close="]",
177191
)

djangocms_rest/static/djangocms_rest/highlight.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@
6363
cursor: pointer;
6464
}
6565
}
66+
67+
span.list-item:before {
68+
content: "[" attr(data-i) "]";
69+
color: var(--dca-gray-light, #999);
70+
font-weight: bold;
71+
font-size: 70%;
72+
display: inline-block;
73+
margin-inline-start: 0.5em;
74+
}

0 commit comments

Comments
 (0)