@@ -70,6 +70,10 @@ def __post_init__(self):
7070 self .other_files = self .determine_other_files ()
7171 self .title = self .determine_title ()
7272
73+ @property
74+ def is_code (self ) -> bool :
75+ return self .main_file .suffix != ".md"
76+
7377 def determine_main_file (self ) -> Path :
7478 """
7579 Determines the main file in the given path.
@@ -101,6 +105,12 @@ def determine_other_files(self) -> list[Path]:
101105 return [file for file in self .path .rglob ("*" ) if is_other_file (file )]
102106
103107 def determine_title (self ) -> str :
108+ if not self .is_code :
109+ with open (self .main_file ) as f :
110+ first_line = f .readline ().strip ()
111+ match = re .match (r'^#\s+(?P<title>.+)$' , first_line )
112+ if match :
113+ return match .group ('title' )
104114 return fix_case (self .path .stem .replace ("_" , " " ).title ())
105115
106116 def generate (self ) -> str :
@@ -110,11 +120,13 @@ def generate(self) -> str:
110120 # Use long code fence to avoid issues with
111121 # included files containing code fences too
112122 code_fence = "``````"
113- is_code = self .main_file .suffix != ".md"
114- if is_code :
123+ # Skip the title from md snippets as it's been included above
124+ start_line = 2
125+ if self .is_code :
115126 content += f"{ code_fence } { self .main_file .suffix [1 :]} \n "
116- content += f'--8<-- "{ self .main_file } "\n '
117- if is_code :
127+ start_line = 1
128+ content += f'--8<-- "{ self .main_file } :{ start_line } "\n '
129+ if self .is_code :
118130 content += f"{ code_fence } \n "
119131 content += "\n "
120132
0 commit comments