Skip to content

Commit 4249e35

Browse files
authored
DEV-2453: Admonitions Processing (#642)
1 parent a6da47e commit 4249e35

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

scripts/docs-collator/AbstractRenderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def _post_rendering_fixes(self, repo, readme_md_file, submodule_dir=""):
3939
repo, content, submodule_dir
4040
)
4141
content = rendering.fix_mdx_format(content)
42+
content = rendering.reformat_admonitions(content)
4243
io.save_string_to_file(readme_md_file, content)
4344

4445
def _copy_extra_resources_for_docs(self, module_download_dir, module_docs_dir):

scripts/docs-collator/ComponentRenderer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def __render_doc(self, component, file):
6565
content = rendering.fix_self_non_closing_br_tags(content)
6666
content = rendering.fix_custom_non_self_closing_tags_in_pre(content)
6767
content = rendering.remove_logo_from_the_bottom(content)
68+
content = rendering.reformat_admonitions(content)
6869
content = rendering.fix_mdx_format(content)
6970

7071
change_log_file = os.path.join(os.path.dirname(file), CHANGELOG_MD)
@@ -73,6 +74,7 @@ def __render_doc(self, component, file):
7374
if os.path.exists(change_log_file)
7475
else ""
7576
)
77+
change_log_content = rendering.reformat_admonitions(change_log_content)
7678
change_log_content = rendering.shift_headings(change_log_content)
7779

7880
relative_path = os.path.relpath(file, module_download_dir)

scripts/docs-collator/ModuleRenderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,5 @@ def __post_rendering_fixes_for_submodule(self, readme_md_file):
205205
content = rendering.fix_self_non_closing_br_tags(content)
206206
content = rendering.fix_custom_non_self_closing_tags_in_pre(content)
207207
content = rendering.fix_mdx_format(content)
208+
content = rendering.reformat_admonitions(content)
208209
io.save_string_to_file(readme_md_file, content)

scripts/docs-collator/utils/rendering.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,53 @@ def strip_frontmatter(content):
257257
frontmatter = []
258258

259259
return "\n".join(content_lines), "\n".join(frontmatter)
260+
261+
def reformat_admonitions(content):
262+
"""
263+
Reformat admonitions to be compatible with Docusaurus.
264+
"""
265+
266+
admonition_map = {
267+
"NOTE": "note",
268+
"TIP": "tip",
269+
"IMPORTANT": "important",
270+
"WARNING": "warning",
271+
"CAUTION": "danger",
272+
}
273+
274+
# Split the content into lines for processing
275+
lines = content.split('\n')
276+
result = [] # Initialize a list to hold the result
277+
in_admonition = False # Flag to indicate if we're inside an admonition block
278+
admonition_type = "" # Variable to store the current admonition type
279+
280+
for line in lines:
281+
# Check if the line marks the start of an admonition
282+
admonition_start = re.match(r'> \[\!(TIP|WARNING|NOTE|IMPORTANT|CAUTION|DANGER)\]', line)
283+
284+
if admonition_start:
285+
# Set the flag to indicate we're inside an admonition
286+
in_admonition = True
287+
# Get the type of admonition, convert to lowercase, and store it
288+
original_admonition_type = admonition_start.group(1)
289+
admonition_type = admonition_map.get(original_admonition_type, original_admonition_type.lower())
290+
# Add the opening Docusaurus admonition tag with a newline
291+
result.append(f":::{admonition_type}")
292+
continue
293+
294+
if in_admonition:
295+
# Process lines that are part of the admonition content
296+
if line.startswith('>'):
297+
# Remove the '>' prefix and add the line to the result
298+
# (But keep the second '>' in the case of '> >' for nested blockquotes)
299+
result.append(line[1:].strip())
300+
else:
301+
# Add the closing Docusaurus admonition tag and reset the flag
302+
result.append("\n:::")
303+
result.append(line)
304+
in_admonition = False
305+
else:
306+
# Add lines outside admonition blocks as they are
307+
result.append(line)
308+
309+
return '\n'.join(result)

0 commit comments

Comments
 (0)