@@ -195,7 +195,12 @@ def strip(self) -> T:
195195
196196
197197def _tree_to_ir (root : Element ) -> SafeWrap [NotebookSerializationV1 ]:
198+ from marimo ._ast .app_config import _AppConfig
199+ from marimo ._utils import yaml
200+ from marimo ._utils .scripts import wrap_script_metadata
201+
198202 app_config = app_config_from_root (root )
203+ config_only = _AppConfig .sanitize (app_config )
199204
200205 sources : list [str ] = []
201206 names : list [str ] = []
@@ -208,14 +213,28 @@ def _tree_to_ir(root: Element) -> SafeWrap[NotebookSerializationV1]:
208213 )
209214 sources .append (get_source_from_tag (child ))
210215
211- from marimo ._utils .scripts import wrap_script_metadata
212-
213- header = root .get ("header" , None )
216+ header_str = root .get ("header" , None )
214217 pyproject = root .get ("pyproject" , None )
215- if pyproject and not header :
216- header = wrap_script_metadata (pyproject )
218+
219+ # Collect non-config frontmatter metadata
220+ frontmatter = {k : v for k , v in app_config .items () if k not in config_only }
221+ if pyproject :
222+ frontmatter ["pyproject" ] = pyproject
223+ if header_str :
224+ frontmatter ["header" ] = header_str
225+
226+ # Build header: frontmatter YAML for md, or script preamble
227+ if frontmatter :
228+ header_value = yaml .dump (frontmatter , sort_keys = False )
229+ elif pyproject and not header_str :
230+ header_value = wrap_script_metadata (pyproject )
231+ elif header_str :
232+ header_value = header_str
233+ else :
234+ header_value = None
235+
217236 notebook = NotebookSerializationV1 (
218- app = AppInstantiation (options = app_config ),
237+ app = AppInstantiation (options = config_only ),
219238 cells = [
220239 CellDef (
221240 name = name ,
@@ -224,7 +243,7 @@ def _tree_to_ir(root: Element) -> SafeWrap[NotebookSerializationV1]:
224243 )
225244 for name , source , config in zip (names , sources , cell_config )
226245 ],
227- header = Header (value = header ) if header else None ,
246+ header = Header (value = header_value ) if header_value else None ,
228247 )
229248 return SafeWrap (notebook )
230249
0 commit comments