post_content );
$url = get_permalink( $post->ID );
$md = "---\n";
$md .= 'title: "' . self::escape_yaml( $title ) . '"' . "\n";
$md .= 'subtitle: "' . self::escape_yaml( $excerpt ) . '"' . "\n";
$md .= 'source: "' . esc_url( $url ) . '"' . "\n";
$md .= "---\n\n";
$md .= '# ' . $title . "\n\n";
$md .= self::html_to_markdown( $post->post_content );
return $md;
}
/**
* Convert raw HTML / WP content to Markdown.
*
* @param string $content Raw HTML content.
* @return string Markdown string.
*/
public static function html_to_markdown( $content ) {
$content = do_shortcode( $content );
$content = apply_filters( 'the_content', $content );
// Strip scripts and styles.
$content = preg_replace( '##is', '', $content );
$content = preg_replace( '##is', '', $content );
// Figures — keep alt text.
$content = preg_replace_callback(
'#]*>(.*?)#is',
function ( $m ) {
preg_match( '/alt=["\']([^"\']*)["\']/', $m[1], $alt );
return ( isset( $alt[1] ) && $alt[1] ) ? '[Image: ' . $alt[1] . ']' . "\n\n" : '';
},
$content
);
// Headings H1–H6.
$content = preg_replace_callback(
'#]*>(.*?)#is',
function ( $m ) {
$level = (int) $m[1];
$text = wp_strip_all_tags( $m[2] );
return str_repeat( '#', $level ) . ' ' . trim( $text ) . "\n\n";
},
$content
);
// Bold / strong.
$content = preg_replace( '#]*>(.*?)#is', '**$1**', $content );
$content = preg_replace( '#]*>(.*?)#is', '**$1**', $content );
// Italic / em.
$content = preg_replace( '#]*>(.*?)#is', '_$1_', $content );
$content = preg_replace( '#]*>(.*?)#is', '_$1_', $content );
// Inline code.
$content = preg_replace( '#]*>(.*?)#is', '`$1`', $content );
// Code blocks.
$content = preg_replace_callback(
'#
]*>(.*?)
#is',
function ( $m ) {
$code = wp_strip_all_tags( $m[1] );
$code = html_entity_decode( $code, ENT_QUOTES | ENT_HTML5 );
return "```\n" . trim( $code ) . "\n```\n\n";
},
$content
);
// Blockquotes.
$content = preg_replace_callback(
'#]*>(.*?)
#is',
function ( $m ) {
$inner = wp_strip_all_tags( $m[1] );
$lines = explode( "\n", trim( $inner ) );
return implode( "\n", array_map( fn( $l ) => '> ' . $l, $lines ) ) . "\n\n";
},
$content
);
// Unordered lists.
$content = preg_replace_callback(
'##is',
function ( $m ) {
preg_match_all( '#]*>(.*?)#is', $m[1], $items );
$out = '';
foreach ( $items[1] as $item ) {
$out .= '- ' . trim( wp_strip_all_tags( $item ) ) . "\n";
}
return $out . "\n";
},
$content
);
// Ordered lists.
$content = preg_replace_callback(
'#]*>(.*?)
#is',
function ( $m ) {
preg_match_all( '#]*>(.*?)#is', $m[1], $items );
$out = '';
$i = 1;
foreach ( $items[1] as $item ) {
$out .= $i . '. ' . trim( wp_strip_all_tags( $item ) ) . "\n";
$i++;
}
return $out . "\n";
},
$content
);
// Links — convert before stripping tags.
$content = preg_replace_callback(
'#]+href=["\']([^"\']*)["\'][^>]*>(.*?)#is',
function ( $m ) {
$url = esc_url_raw( $m[1] );
$text = trim( wp_strip_all_tags( $m[2] ) );
if ( empty( $text ) ) {
return $url;
}
return '[' . $text . '](' . $url . ')';
},
$content
);
// Horizontal rules.
$content = preg_replace( '#
]*/?>#is', "\n---\n\n", $content );
// Paragraph / div / section breaks.
$content = preg_replace( '##i', "\n\n", $content );
$content = preg_replace( '#
#i', "\n", $content );
$content = preg_replace( '##i', "\n", $content );
$content = preg_replace( '##i',"\n\n", $content );
// Strip remaining tags.
$content = wp_strip_all_tags( $content );
// Decode HTML entities.
$content = html_entity_decode( $content, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
// Clean up excessive blank lines (max 2 consecutive).
$content = preg_replace( '/\n{3,}/', "\n\n", $content );
return trim( $content );
}
/**
* Generate a short auto-excerpt from content.
*
* @param string $content Raw content.
* @param int $length Max character length.
* @return string
*/
private static function auto_excerpt( $content, $length = 160 ) {
$text = wp_strip_all_tags( $content );
$text = preg_replace( '/\s+/', ' ', $text );
$text = trim( $text );
if ( strlen( $text ) > $length ) {
$text = substr( $text, 0, $length );
$text = substr( $text, 0, strrpos( $text, ' ' ) ) . '...';
}
return $text;
}
/**
* Escape a string for use inside YAML front-matter double quotes.
*
* @param string $str Input string.
* @return string
*/
private static function escape_yaml( $str ) {
return str_replace( '"', '\\"', $str );
}
/**
* Get stored Markdown for a post, generating and saving it if empty.
*
* @param int $post_id Post ID.
* @return string|false
*/
public static function get_or_generate( $post_id ) {
$stored = get_post_meta( $post_id, '_ajwa_md_content', true );
if ( ! empty( $stored ) ) {
return $stored;
}
$md = self::post_to_markdown( $post_id );
if ( $md ) {
update_post_meta( $post_id, '_ajwa_md_content', $md );
$post = get_post( $post_id );
update_post_meta( $post_id, '_ajwa_content_hash', md5( $post->post_content ) );
}
return $md;
}
/**
* Regenerate Markdown if post content changed since last save.
* Respects manual edits — only regenerates if content hash changed.
*
* @param int $post_id Post ID.
*/
public static function maybe_regenerate_on_save( $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
return;
}
$stored_md = get_post_meta( $post_id, '_ajwa_md_content', true );
if ( empty( $stored_md ) ) {
return;
}
$stored_hash = get_post_meta( $post_id, '_ajwa_content_hash', true );
$current_hash = md5( $post->post_content );
if ( $stored_hash !== $current_hash ) {
$md = self::post_to_markdown( $post_id );
if ( $md ) {
update_post_meta( $post_id, '_ajwa_md_content', $md );
update_post_meta( $post_id, '_ajwa_content_hash', $current_hash );
update_post_meta( $post_id, '_ajwa_md_auto_updated', 1 );
}
}
}
}