@@ -314,14 +314,7 @@ private function write_post_file( object $post, bool $persist_auto_draft ): stri
314314 // Build frontmatter.
315315 $ frontmatter = $ this ->build_frontmatter ( $ post );
316316
317- // Build content.
318- $ content = $ post ->post_content ?? '' ;
319-
320- // Assemble the file.
321- $ output = "--- \n" ;
322- $ output .= WP_Markdown_Yaml ::encode_yaml ( $ frontmatter );
323- $ output .= "--- \n\n" ;
324- $ output .= $ content . "\n" ;
317+ $ output = $ this ->build_file_output ( $ post , $ frontmatter );
325318
326319 $ this ->observe_file_mutation ( $ file_path );
327320 $ result = file_put_contents ( $ file_path , $ output , LOCK_EX );
@@ -836,7 +829,7 @@ private function write_profile_post( object $post ): string|false {
836829 if ( null === $ this ->profile_absolute_path ( $ relative , true ) ) {
837830 return false ;
838831 }
839- $ output = " --- \n" . WP_Markdown_Yaml :: encode_yaml ( $ frontmatter ) . " --- \n\n" . ( $ post-> post_content ?? '' ) . "\n" ;
832+ $ output = $ this -> build_file_output ( $ post, $ frontmatter ) ;
840833 $ id = (int ) ( $ post ->ID ?? 0 );
841834 $ old_path = $ id > 0 ? $ this ->find_file_by_id ( $ id ) : null ;
842835 $ tmp_path = $ this ->stage_profile_write ( $ file_path , $ output );
@@ -1178,7 +1171,19 @@ private function find_file_by_id( int $post_id ): ?string {
11781171 $ this ->rebuild_index ();
11791172 }
11801173
1181- return $ this ->index [ $ post_id ] ?? null ;
1174+ if ( isset ( $ this ->index [ $ post_id ] ) ) {
1175+ return $ this ->index [ $ post_id ];
1176+ }
1177+ // Plain Markdown has no embedded ID. Resolve its canonical route from
1178+ // the imported post's source metadata via the layout profile instead.
1179+ if ( $ this ->profile_enumerates_sources () && function_exists ( 'get_post ' ) ) {
1180+ $ post = get_post ( $ post_id );
1181+ if ( is_object ( $ post ) ) {
1182+ $ relative = $ this ->profile_path_for_post ( $ post );
1183+ return false !== $ relative ? $ this ->profile_absolute_path ( $ relative ) : null ;
1184+ }
1185+ }
1186+ return null ;
11821187 }
11831188
11841189 /**
@@ -1375,52 +1380,63 @@ private function parse_file( string $file_path, bool $metadata_only = false ): ?
13751380 }
13761381 }
13771382
1383+ /** @var array<string,string> Files excluded by the active frontmatter policy. */
1384+ private array $ skipped_files = array ();
1385+
1386+ /** @return array<string,string> Absolute paths mapped to skip reasons. */
1387+ public function get_skipped_files (): array {
1388+ return $ this ->skipped_files ;
1389+ }
1390+
13781391 private function parse_post_file ( string $ file_path , bool $ metadata_only ): ?object {
1379- if ( $ metadata_only ) {
1392+ $ context = array (
1393+ 'operation ' => 'import ' ,
1394+ 'file_path ' => $ file_path ,
1395+ 'profile ' => $ this ->frontmatter_profile ,
1396+ );
1397+ $ selected = WP_Markdown_Frontmatter_Profiles ::resolve_for_import ( array (), '' , $ context );
1398+ $ body_metadata = 'optional ' === ( $ selected ['frontmatter ' ] ?? 'required ' );
1399+ if ( $ metadata_only && ! $ body_metadata ) {
13801400 // Fast path: read only enough of the file to get the frontmatter.
13811401 // Avoids reading potentially large content bodies during boot.
13821402 $ raw = $ this ->read_frontmatter_only ( $ file_path );
1403+ if ( false === $ raw ) {
1404+ $ raw = file_get_contents ( $ file_path );
1405+ }
13831406 } else {
13841407 $ raw = file_get_contents ( $ file_path );
13851408 }
13861409
1387- if ( false === $ raw || '' === $ raw ) {
1410+ if ( false === $ raw ) {
13881411 return null ;
13891412 }
13901413
1391- // Split frontmatter and content.
1392- if ( ! preg_match ( '/^---\n(.+?)\n---\n\n?(.*)/s ' , $ raw , $ m ) ) {
1393- return null ;
1394- }
1395-
1396- $ frontmatter = WP_Markdown_Yaml ::decode_yaml ( $ m [1 ] );
1397- // write_post() terminates every body with one LF; remove only that terminator.
1398- $ content = $ metadata_only ? '' : ( str_ends_with ( $ m [2 ], "\n" ) ? substr ( $ m [2 ], 0 , -1 ) : $ m [2 ] );
1399-
1400- if ( ! $ frontmatter ) {
1414+ // Missing and empty frontmatter are accepted only by an opted-in profile.
1415+ $ has_frontmatter = (bool ) preg_match ( '/\A---\n(.*?)^---\n\n?(.*)/ms ' , $ raw , $ m );
1416+ $ frontmatter = $ has_frontmatter ? (array ) WP_Markdown_Yaml ::decode_yaml ( $ m [1 ] ) : array ();
1417+ $ body = $ has_frontmatter ? $ m [2 ] : $ raw ;
1418+ // write_post() adds one LF; preserve all other body whitespace.
1419+ $ content = str_ends_with ( $ body , "\n" ) ? substr ( $ body , 0 , -1 ) : $ body ;
1420+ $ profile = WP_Markdown_Frontmatter_Profiles ::resolve_for_import ( $ frontmatter , $ content , $ context );
1421+ if ( ! $ frontmatter && 'optional ' !== ( $ profile ['frontmatter ' ] ?? 'required ' ) ) {
1422+ $ this ->skipped_files [ $ file_path ] = $ has_frontmatter ? 'empty_frontmatter ' : 'missing_frontmatter ' ;
14011423 return null ;
14021424 }
1403-
1404- $ context = array (
1405- 'operation ' => 'import ' ,
1406- 'file_path ' => $ file_path ,
1407- 'profile ' => $ this ->frontmatter_profile ,
1408- );
1409- $ profile = WP_Markdown_Frontmatter_Profiles ::resolve_for_import ( $ frontmatter , $ content , $ context );
1425+ unset( $ this ->skipped_files [ $ file_path ] );
14101426 $ data = array ();
14111427 if ( ! empty ( $ profile ['import_post_data ' ] ) && is_callable ( $ profile ['import_post_data ' ] ) ) {
14121428 $ data = (array ) call_user_func ( $ profile ['import_post_data ' ], $ frontmatter , $ content , array_merge ( $ context , array ( 'profile_id ' => $ profile ['id ' ] ?? '' ) ) );
14131429 }
14141430
14151431 $ post = (object ) $ data ;
1416- $ post ->post_content = (string ) ( $ post ->post_content ?? $ content );
1432+ $ post ->post_content = $ metadata_only ? '' : (string ) ( $ post ->post_content ?? $ content );
14171433 // Derive slug from the filename when the frontmatter has none.
14181434 // Supports the "just drop a file" workflow — an AI agent or git
14191435 // pull creating `wiki/my-topic.md` gets `my-topic` as post_name
14201436 // without needing a slug field. `index.md` files (used when a
14211437 // post has children) fall back to the parent directory name.
14221438 // See GitHub issue #42.
1423- if ( '' === $ post ->post_name && '' !== $ file_path ) {
1439+ if ( '' === ( $ post ->post_name ?? '' ) && '' !== $ file_path ) {
14241440 $ stem = basename ( $ file_path , '.md ' );
14251441 if ( 'index ' === $ stem ) {
14261442 $ stem = basename ( dirname ( $ file_path ) );
@@ -1542,6 +1558,16 @@ private function read_frontmatter_only( string $file_path ) {
15421558 * @param object $post A WordPress post row.
15431559 * @return array Frontmatter key-value pairs.
15441560 */
1561+ /** Assemble either a YAML document or a plain Markdown body. */
1562+ private function build_file_output ( object $ post , array $ frontmatter ): string {
1563+ $ context = array ( 'operation ' => 'export ' , 'profile ' => $ this ->frontmatter_profile );
1564+ $ profile = WP_Markdown_Frontmatter_Profiles ::resolve_for_export ( $ post , $ context );
1565+ $ body = is_callable ( $ profile ['export_body ' ] ?? null )
1566+ ? (string ) call_user_func ( $ profile ['export_body ' ], $ post , array_merge ( $ context , array ( 'profile_id ' => $ profile ['id ' ] ?? '' ) ) )
1567+ : (string ) ( $ post ->post_content ?? '' );
1568+ return ( $ frontmatter ? "--- \n" . WP_Markdown_Yaml ::encode_yaml ( $ frontmatter ) . "--- \n\n" : '' ) . $ body . "\n" ;
1569+ }
1570+
15451571 private function build_frontmatter ( object $ post ): array {
15461572 $ native_frontmatter = $ this ->build_native_frontmatter ( $ post );
15471573 $ context = array (
0 commit comments