@@ -149,3 +149,128 @@ def test_builder_no_title_no_topmatter(tmp_path):
149149
150150 index = (Path (tmp_path ) / "index.qmd" ).read_text ()
151151 assert not index .startswith ("---" )
152+
153+
154+ def test_builder_index_topmatter_custom (tmp_path ):
155+ """Test that index_topmatter allows custom frontmatter."""
156+ cfg = yaml .safe_load (
157+ """
158+ quartodoc:
159+ package: quartodoc.tests.example
160+ title: "This will be ignored"
161+ index-topmatter:
162+ title: "Custom API Reference"
163+ description: "API documentation for my package"
164+ author: "Test Author"
165+ date: "2024-01-01"
166+ sections:
167+ - title: first section
168+ contents: [a_func]
169+ """
170+ )
171+
172+ builder = Builder .from_quarto_config (cfg )
173+ builder .dir = str (tmp_path )
174+ bp = blueprint (builder .layout )
175+ builder .write_index (bp )
176+
177+ index = (Path (tmp_path ) / "index.qmd" ).read_text ()
178+ assert index .startswith ("---" )
179+ assert "title: Custom API Reference" in index
180+ assert "description: API documentation for my package" in index
181+ assert "author: Test Author" in index
182+ assert "This will be ignored" not in index # Original title not used
183+
184+
185+ def test_builder_index_topmatter_empty (tmp_path ):
186+ """Test that empty index_topmatter generates empty frontmatter."""
187+ cfg = yaml .safe_load (
188+ """
189+ quartodoc:
190+ package: quartodoc.tests.example
191+ title: "Will be ignored"
192+ index-topmatter: {}
193+ sections:
194+ - title: first section
195+ contents: [a_func]
196+ """
197+ )
198+
199+ builder = Builder .from_quarto_config (cfg )
200+ builder .dir = str (tmp_path )
201+ bp = blueprint (builder .layout )
202+ builder .write_index (bp )
203+
204+ index = (Path (tmp_path ) / "index.qmd" ).read_text ()
205+ assert index .startswith ("---\n {}\n \n ---" ) # Empty dict rendered as {} in YAML
206+
207+
208+ def test_builder_index_topmatter_fallback (tmp_path ):
209+ """Test that title is used when index_topmatter is not provided."""
210+ cfg = yaml .safe_load (
211+ """
212+ quartodoc:
213+ package: quartodoc.tests.example
214+ title: "Function Reference"
215+ sections:
216+ - title: first section
217+ contents: [a_func]
218+ """
219+ )
220+
221+ builder = Builder .from_quarto_config (cfg )
222+ builder .dir = str (tmp_path )
223+ bp = blueprint (builder .layout )
224+ builder .write_index (bp )
225+
226+ index = (Path (tmp_path ) / "index.qmd" ).read_text ()
227+ assert "title: Function Reference" in index
228+
229+
230+ def test_builder_index_topmatter_full_build (tmp_path ):
231+ """Test full build with index-topmatter configuration."""
232+ cfg = yaml .safe_load (
233+ """
234+ quartodoc:
235+ package: quartodoc.tests.example
236+ index-topmatter:
237+ title: "Complete API Reference"
238+ description: "Full documentation with custom frontmatter"
239+ author: "Test Suite"
240+ date-modified: "2024-12-04"
241+ toc: true
242+ toc-depth: 2
243+ sections:
244+ - title: Test Functions
245+ desc: "Testing the new index-topmatter feature"
246+ contents:
247+ - a_func
248+ - AClass
249+ """
250+ )
251+
252+ builder = Builder .from_quarto_config (cfg )
253+ builder .dir = str (tmp_path / "api" )
254+
255+ # Run the full build
256+ builder .build ()
257+
258+ # Check that the index file was created with correct frontmatter
259+ index_path = Path (tmp_path ) / "api" / "index.qmd"
260+ assert index_path .exists ()
261+
262+ index_content = index_path .read_text ()
263+ assert "title: Complete API Reference" in index_content
264+ assert "description: Full documentation with custom frontmatter" in index_content
265+ assert "author: Test Suite" in index_content
266+ assert "date-modified: '2024-12-04'" in index_content or "date-modified: 2024-12-04" in index_content
267+ assert "toc: true" in index_content
268+ assert "toc-depth: 2" in index_content
269+
270+ # Check that the section title is still rendered correctly
271+ assert "## Test Functions" in index_content
272+ assert "Testing the new index-topmatter feature" in index_content
273+
274+ # Check that individual pages were created
275+ assert (Path (tmp_path ) / "api" / "a_func.qmd" ).exists ()
276+ assert (Path (tmp_path ) / "api" / "AClass.qmd" ).exists ()
0 commit comments