Skip to content

Commit c038dec

Browse files
committed
Improve has_markdown/gist_block logic
1 parent 6e87ff6 commit c038dec

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

tbx/core/templatetags/util_tags.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,18 @@ def has_gist_block(value):
9696
if not isinstance(value, StreamValue):
9797
return False
9898

99-
for block in value._raw_data:
100-
# special case for work page section block as the streamfields are nested within sections
101-
if block["type"] == "section":
102-
try:
103-
for sub_block in block["value"]["content"]:
104-
if (
105-
sub_block["type"] == "raw_html"
106-
and "https://gist.github.com" in sub_block["value"]
107-
):
108-
return True
109-
except (KeyError, TypeError):
110-
pass
111-
112-
if block["type"] == "raw_html" and "https://gist.github.com" in block["value"]:
99+
for block in value.blocks_by_name(block_name="raw_html"):
100+
if "https://gist.github.com" in block.value:
113101
return True
102+
103+
# Special case for work page section block as the StreamField blockss are nested within sections
104+
for block in value.blocks_by_name(block_name="section"):
105+
if "content" not in block.value:
106+
continue
107+
for sub_block in block.value["content"].blocks_by_name("raw_html"):
108+
if "https://gist.github.com" in sub_block.value:
109+
return True
110+
114111
return False
115112

116113

@@ -119,15 +116,14 @@ def has_markdown_block(value):
119116
if not isinstance(value, StreamValue):
120117
return False
121118

122-
for block in value._raw_data:
123-
if block["type"] == "section":
124-
try:
125-
for sub_block in block["value"]["content"]:
126-
if sub_block["type"] == "markdown":
127-
return True
128-
except (KeyError, TypeError):
129-
pass
130-
if block["type"] == "markdown":
119+
if len(value.blocks_by_name(block_name="markdown")):
120+
return True
121+
122+
# Special case for work page section block as the StreamField blockss are nested within sections
123+
for block in value.blocks_by_name(block_name="section"):
124+
if "content" not in block.value:
125+
continue
126+
if len(block.value["content"].blocks_by_name("markdown")):
131127
return True
132128

133129
return False

0 commit comments

Comments
 (0)