diff --git a/plugins/markdown-export/plugin.php b/plugins/markdown-export/plugin.php new file mode 100644 index 00000000..3b54a86b --- /dev/null +++ b/plugins/markdown-export/plugin.php @@ -0,0 +1,132 @@ +post_name ) . '.md' ); + header( "Content-Disposition: inline; filename*=UTF-8''" . $filename ); + + echo $markdown; + exit; + } +); + +/** + * Get a post by its path/slug. + * + * @param string $path The URL path to find the post for. + * @return WP_Post|null The post object or null if not found. + */ +function markdown_export_get_post_by_path( $path ) { + // Clean up the path + $path = trim( $path, '/' ); + + if ( empty( $path ) ) { + return null; + } + + // Try to get by path as a page (hierarchical) + $page = get_page_by_path( $path, OBJECT, array( 'page', 'post' ) ); + if ( $page ) { + return $page; + } + + // Try to find by slug for non-hierarchical post types + $posts = get_posts( + array( + 'name' => basename( $path ), + 'post_type' => array( 'post', 'page' ), + 'post_status' => 'publish', + 'posts_per_page' => 1, + ) + ); + + if ( ! empty( $posts ) ) { + return $posts[0]; + } + + return null; +} + +/** + * Convert a post to Markdown format using MarkdownProducer. + * + * @param WP_Post $post The post to convert. + * @return string The markdown representation of the post. + */ +function markdown_export_convert_post_to_markdown( $post ) { + // Prepare metadata for the markdown frontmatter + $metadata = array( + 'post_title' => array( $post->post_title ), + 'post_date' => array( $post->post_date ), + ); + + // Add excerpt if it has content + if ( '' !== $post->post_excerpt ) { + $metadata['post_excerpt'] = array( $post->post_excerpt ); + } + + // Create BlocksWithMetadata and convert to markdown + $blocks_with_metadata = new BlocksWithMetadata( + $post->post_content, + $metadata + ); + + $producer = new MarkdownProducer( $blocks_with_metadata ); + + return $producer->produce(); +}