|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Plugin Name: Post Content to Markdown |
| 5 | + * Description: Exposes post content as Markdown via HTTP `Accept` headers. |
| 6 | + * Version: 1.0.0 |
| 7 | + * Author: roots.io |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PostContentToMarkdown; |
| 11 | + |
| 12 | +if (! defined('ABSPATH')) { |
| 13 | + exit; |
| 14 | +} |
| 15 | + |
| 16 | +// Load the appropriate autoloader |
| 17 | +if (file_exists(__DIR__.'/vendor/autoload.php')) { |
| 18 | + // Composer installation |
| 19 | + require_once __DIR__.'/vendor/autoload.php'; |
| 20 | +} else { |
| 21 | + // Standalone plugin with prefixed dependencies |
| 22 | + require_once __DIR__.'/vendor_prefixed/vendor/autoload.php'; |
| 23 | +} |
| 24 | + |
| 25 | +add_action('template_redirect', function () { |
| 26 | + if (! isset($_SERVER['HTTP_ACCEPT']) || ! str_contains($_SERVER['HTTP_ACCEPT'], 'text/markdown')) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + $post = get_queried_object(); |
| 31 | + if (! $post || ! is_a($post, 'WP_Post')) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + $allowed_post_types = apply_filters('post_content_to_markdown/post_types', ['post']); |
| 36 | + |
| 37 | + if (is_single() && in_array($post->post_type, $allowed_post_types, true)) { |
| 38 | + header('Content-Type: text/markdown'); |
| 39 | + echo '# '.strip_tags($post->post_title)."\n\n".contentToMarkdown($post->post_content); |
| 40 | + exit; |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * Converts an HTML string into Markdown using the league/html-to-markdown library. |
| 46 | + * |
| 47 | + * @param string $content The HTML content to convert. |
| 48 | + * @return string The converted Markdown. |
| 49 | + */ |
| 50 | +function contentToMarkdown($content) |
| 51 | +{ |
| 52 | + if (empty(trim($content))) { |
| 53 | + return ''; |
| 54 | + } |
| 55 | + |
| 56 | + $default_options = [ |
| 57 | + 'header_style' => 'atx', |
| 58 | + 'strip_tags' => true, // Remove HTML tags without markdown equivalents |
| 59 | + 'remove_nodes' => 'script style', // Remove script and style elements |
| 60 | + 'hard_break' => true, // Convert <br> to newlines |
| 61 | + ]; |
| 62 | + |
| 63 | + $options = apply_filters('post_content_to_markdown/converter_options', $default_options); |
| 64 | + |
| 65 | + // Determine which HtmlConverter class to use based on what's available |
| 66 | + if (class_exists('\League\HTMLToMarkdown\HtmlConverter')) { |
| 67 | + $converter = new \League\HTMLToMarkdown\HtmlConverter($options); |
| 68 | + } else { |
| 69 | + $converter = new \PostContentToMarkdown\Vendor\League\HTMLToMarkdown\HtmlConverter($options); |
| 70 | + } |
| 71 | + $markdown = $converter->convert($content); |
| 72 | + |
| 73 | + // Clean up excessive newlines (more than 2 consecutive) |
| 74 | + $markdown = preg_replace("/\n{3,}/", "\n\n", $markdown); |
| 75 | + |
| 76 | + return apply_filters('post_content_to_markdown/markdown_output', $markdown, $content); |
| 77 | +} |
0 commit comments