Convert Markdown to HTML by writing a custom extension for markdown-it-py. The extension should detect custom syntax like ::alert[text]:: and render it as a div with alert classes.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a complete markdown-it-py extension that parses ::alert[text]:: syntax and renders it as a <div class="alert">text</div> block with example usage included.
Below is a minimal example of how you can create a custom plugin for markdown-it-py to parse a syntax like ::alert[text]:: and render it as a <div> with alert classes. You can adapt it further (e.g., allow alert-info, alert-warning, etc.) according to your needs.
Code
python
from markdown_it import MarkdownIt
from markdown_it.token import Token
def alert_plugin(md: MarkdownIt):
"""
A markdown-it-py plugin to parse ::alert[text]:: and render it as:
<div class="alert alert-primary">text</div>
"""
def alert_rule(state, silent):
src = state.src
pos = state.pos
# Check for the starting pattern ::alert[
marker = "::alert["
if not src.startswith(marker, pos):
return False
# Find the closing pattern ]::
start = pos + len(marker)
end = src.find("]::", start)
if end < 0:
return False # no closing marker
# Extract the content inside the brackets
content = src[start:end]
# If we're in "silent" mode (e.g., for validation), don't actually parse
if silent:
return True
# Move the parser position to the end of this syntax
state.pos = end + 3 # length of "]::"
# Create tokens for the alert
# Opening token
token_open = state.push("alert_open", "div", 1)
token_open.attrs = {"class": "alert alert-primary"}
# Content token (plain text)
token_text = state.push("text", "", 0)
token_text.content = content
# Closing token
token_close = state.push("alert_close", "div", -1)
return True
# Insert our inline rule before emphasis (so it gets parsed first)
md.inline.ruler.before("emphasis", "alert_rule", alert_rule)
# Rendering rules
md.add_render_rule("alert_open", lambda tokens, idx, options, env: '<div class="alert alert-primary">')
md.add_render_rule("alert_close", lambda tokens, idx, options, env: '</div>')
# Example usage
if __name__ == "__main__":
md = MarkdownIt().use(alert_plugin)
markdown_text = "Some text before.\n\n::alert[This is an alert message]::\n\nSome text after."
html_output = md.render(markdown_text)
print(html_output)
What this does
Adds a custom inline rule (alert_rule) that looks for the exact pattern ::alert[ ... ]::.
When found, it creates three tokens:
alert_open (a <div> with class alert alert-primary)
text (the content inside the brackets)
alert_close (the closing </div>)
Registers rendering rules to turn those tokens into the desired HTML.
You can expand the logic to parse alert types (e.g. ::alert-warning[...]::) by tweaking the parsing of the bracketed content or adding additional markers.