Skip to content

Commit 7a88542

Browse files
committed
Fix issue with apparent codeblock within other codeblock
If there is a triple quoted string inside a codeblock that that appears invalid the current implementation will trigger the the "Bat ticks message". This is confusing to the user and generally wrong because that happens to be correct. What needs to happen is that the we should isolate the codeblocks and make sure that the violating codeblock is not contained within a codeblock. This change handles that scenario by checking all of the invalid codeblocks to generate the correct message based on the invalid codeblock. If a message contains multiple invalid codeblocks and there is at least one that is using invalid ticks the invalid ticks message will be shown. If there is no codeblock with invalid ticks the missing language and no language logic will be followed without change.
1 parent 8c6d683 commit 7a88542

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

bot/exts/info/codeblock/_instructions.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
"""This module generates and formats instructional messages about fixing Markdown code blocks."""
22

3-
43
from bot.exts.info.codeblock import _parsing
54
from bot.log import get_logger
65

76
log = get_logger(__name__)
87

9-
_EXAMPLE_PY = "{lang}\nprint('Hello, world!')" # Make sure to escape any Markdown symbols here.
10-
_EXAMPLE_CODE_BLOCKS = (
11-
"\\`\\`\\`{content}\n\\`\\`\\`\n\n"
12-
"**This will result in the following:**\n"
13-
"```{content}```"
14-
)
8+
# Make sure to escape any Markdown symbols here.
9+
_EXAMPLE_PY = "{lang}\nprint('Hello, world!')"
10+
_EXAMPLE_CODE_BLOCKS = "\\`\\`\\`{content}\n\\`\\`\\`\n\n**This will result in the following:**\n```{content}```"
1511

1612

1713
def _get_example(language: str) -> str:
@@ -37,8 +33,7 @@ def _get_bad_ticks_message(code_block: _parsing.CodeBlock) -> str | None:
3733

3834
valid_ticks = f"\\{_parsing.BACKTICK}" * 3
3935
instructions = (
40-
"You are using the wrong character instead of backticks. "
41-
f"Use {valid_ticks}, not `{code_block.ticks}`."
36+
f"You are using the wrong character instead of backticks. Use {valid_ticks}, not `{code_block.ticks}`."
4237
)
4338

4439
log.trace("Check if the bad ticks code block also has issues with the language specifier.")
@@ -148,7 +143,23 @@ def get_instructions(content: str) -> str | None:
148143
instructions = _get_no_ticks_message(content)
149144
else:
150145
log.trace("Searching results for a code block with invalid ticks.")
151-
block = next((block for block in blocks if block.tick != _parsing.BACKTICK), None)
146+
bad_ticks = [block for block in blocks if block.tick != _parsing.BACKTICK]
147+
block = None
148+
if bad_ticks:
149+
block = next(
150+
(
151+
bad_tick
152+
for bad_tick in bad_ticks
153+
if any(
154+
block
155+
for block in blocks
156+
if block != bad_tick
157+
and bad_tick.content != block.content
158+
and bad_tick.content not in block.content
159+
)
160+
),
161+
None,
162+
)
152163

153164
if block:
154165
log.trace("A code block exists but has invalid ticks.")

0 commit comments

Comments
 (0)