Skip to content

Commit

Permalink
Remove start and end extra newlines in slack code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
aralyekta committed Feb 11, 2025
1 parent 2ef0db0 commit 28ee69f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/gurubase-backend/backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,8 +1921,36 @@ def convert_markdown_to_slack(content: str) -> str:
"""Convert Markdown formatting to Slack formatting."""
# Convert markdown code blocks to Slack code blocks by removing language specifiers
import re

# First remove language specifiers from code blocks
content = re.sub(r'```\w+', '```', content)

# Then remove empty lines at the start and end of code blocks
def trim_code_block(match):
code_block = match.group(0)
lines = code_block.split('\n')

# Find first and last non-empty lines (excluding ```)
start = 0
end = len(lines) - 1

# Find first non-empty line after opening ```
for i, line in enumerate(lines):
if line.strip() == '```':
start = i + 1
break

# Find last non-empty line before closing ```
for i in range(len(lines) - 1, -1, -1):
if line.strip() == '```':
end = i
break

# Keep all lines between start and end (inclusive)
return '```\n' + '\n'.join(lines[start:end]) + '\n```'

content = re.sub(r'```[\s\S]+?```', trim_code_block, content)

# Convert markdown links [text](url) to Slack format <url|text>
def replace_link(match):
text = match.group(1)
Expand Down

0 comments on commit 28ee69f

Please sign in to comment.