Skip to content

Commit b0d3a7c

Browse files
authored
fix: protect backtick spans before stripping markdown italic in docstring generator (#175)
The _([^_]+)_ regex intended to strip _italic_ markdown was also matching underscores inside backtick-quoted identifiers (e.g. SERIAL_RX_BUFFER_SIZE → SERIALRXBUFFER_SIZE). Backtick spans are now saved and restored around the emphasis-stripping pass.
1 parent 9547c20 commit b0d3a7c

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

scripts/generate_apis.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,23 @@ def clean_docstring_text(self, text: str) -> str:
126126
import re
127127
text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)
128128

129-
# Remove markdown emphasis formatting (* and _)
130-
# Handle both single and double emphasis markers
129+
# Remove markdown emphasis formatting (* and _), but protect backtick-quoted
130+
# spans first so identifiers like SERIAL_RX_BUFFER_SIZE are not corrupted.
131+
placeholders = {}
132+
def save_backtick(m):
133+
key = f"\x00BACKTICK{len(placeholders)}\x00"
134+
placeholders[key] = m.group(0)
135+
return key
136+
text = re.sub(r'`[^`]*`', save_backtick, text)
137+
131138
text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text) # **bold** -> bold
132139
text = re.sub(r'\*([^*]+)\*', r'\1', text) # *italic* -> italic
133140
text = re.sub(r'__([^_]+)__', r'\1', text) # __bold__ -> bold
134141
text = re.sub(r'_([^_]+)_', r'\1', text) # _italic_ -> italic
135142

143+
for key, val in placeholders.items():
144+
text = text.replace(key, val)
145+
136146
# Collapse multiple spaces into single spaces
137147
text = re.sub(r'\s+', ' ', text)
138148

0 commit comments

Comments
 (0)