@@ -70,6 +70,10 @@ def __post_init__(self):
70
70
self .other_files = self .determine_other_files ()
71
71
self .title = self .determine_title ()
72
72
73
+ @property
74
+ def is_code (self ) -> bool :
75
+ return self .main_file .suffix != ".md"
76
+
73
77
def determine_main_file (self ) -> Path :
74
78
"""
75
79
Determines the main file in the given path.
@@ -101,6 +105,12 @@ def determine_other_files(self) -> list[Path]:
101
105
return [file for file in self .path .rglob ("*" ) if is_other_file (file )]
102
106
103
107
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' )
104
114
return fix_case (self .path .stem .replace ("_" , " " ).title ())
105
115
106
116
def generate (self ) -> str :
@@ -110,11 +120,13 @@ def generate(self) -> str:
110
120
# Use long code fence to avoid issues with
111
121
# included files containing code fences too
112
122
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 :
115
126
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 :
118
130
content += f"{ code_fence } \n "
119
131
content += "\n "
120
132
0 commit comments