From 52fb23f47e043b9621d37322c1ca72b40bf721f7 Mon Sep 17 00:00:00 2001 From: Harry Mellor <19981378+hmellor@users.noreply.github.com> Date: Fri, 23 May 2025 14:53:44 +0200 Subject: [PATCH] Fix examples with code blocks in docs (#18609) Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> --- docs/mkdocs/hooks/generate_examples.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/mkdocs/hooks/generate_examples.py b/docs/mkdocs/hooks/generate_examples.py index 9144f6824b09a..a2131c342e8ce 100644 --- a/docs/mkdocs/hooks/generate_examples.py +++ b/docs/mkdocs/hooks/generate_examples.py @@ -107,12 +107,15 @@ class Example: content = f"---\ntitle: {self.title}\n---\n\n" content += f"Source .\n\n" + # Use long code fence to avoid issues with + # included files containing code fences too + code_fence = "``````" is_code = self.main_file.suffix != ".md" if is_code: - content += f"```{self.main_file.suffix[1:]}\n" + content += f"{code_fence}{self.main_file.suffix[1:]}\n" content += f'--8<-- "{self.main_file}"\n' if is_code: - content += "```\n" + content += f"{code_fence}\n" content += "\n" if not self.other_files: @@ -122,10 +125,10 @@ class Example: for file in sorted(self.other_files): content += f'??? abstract "{file.relative_to(self.path)}"\n' if file.suffix != ".md": - content += f" ```{file.suffix[1:]}\n" + content += f" {code_fence}{file.suffix[1:]}\n" content += f' --8<-- "{file}"\n' if file.suffix != ".md": - content += " ```\n" + content += f" {code_fence}\n" return content