-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhtml-to-md.php
More file actions
132 lines (113 loc) · 4.05 KB
/
Copy pathhtml-to-md.php
File metadata and controls
132 lines (113 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* Plugin Name: HTML to Markdown
* Plugin URI: https://github.com/dmsnell/html-to-md
* Author: WordPress Core Team
* Description: Convert HTML documents to Markdown using WordPress’ HTML API.
* Version: 2026.02.18
* Requires at least: 6.9
* License: GPLv2
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
use WordPress\Experiments\HtmlToMarkdown\WP_Experimental_HTML_Renderer_Options;
// Don’t load directly.
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
require_once __DIR__ . '/wp-experimental-html-renderer-loader.php';
require_once __DIR__ . '/wp-html-to-markdown.php';
add_filter( 'html_to_markdown_starting_node_finder', fn ( $prev ) =>
$prev ?? function ( $p ) {
while ( $p->next_token() ) {
if (
'MAIN' === $p->get_tag() ||
'main' === $p->get_attribute( 'role' ) ||
'main-content' === $p->get_attribute( 'id' ) || // cloudflare.
'hnmain' === $p->get_attribute( 'id' ) // Hackernews.
) {
return true;
}
}
return false;
} );
add_action( 'init', function () {
$has_markdown_type = 1 === preg_match( '~^text/(?:x-)?markdown(?:[,;]|$)~', $_SERVER['HTTP_ACCEPT'] ?? '' );
$has_markdown_query_arg = in_array( $_GET['output_format'] ?? '', array( 'md', 'markdown' ), true );
if ( ! ( $has_markdown_type || $has_markdown_query_arg ) ) {
return;
}
// Force the template enhancement output buffer to always be enabled for markdown responses.
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', PHP_INT_MAX );
remove_all_filters( 'wp_template_enhancement_output_buffer' );
add_filter(
'wp_template_enhancement_output_buffer',
function ( $output ) use ( $has_markdown_type ) {
header( 'Content-type: text/markdown; charset=utf-8' );
if ( $has_markdown_type ) {
header( 'Vary: Accept' );
}
$title = '';
$author = '';
$published_on = '';
$modified_on = '';
if ( is_singular() ) {
$title = get_the_title();
$published_on = get_the_date();
$modified_on = get_the_modified_date();
if ( post_type_supports( get_post()->post_type ?? '', 'author' ) ) {
$author = get_the_author_meta( 'display_name' );
}
} else {
$title_finder = new WP_HTML_Tag_Processor( $output );
if ( $title_finder->next_tag( 'title' ) ) {
$title = $title_finder->get_modifiable_text();
}
}
$frontmatter = '';
if ( ! empty( $title ) ) {
$frontmatter .= "Title: {$title}\n";
}
if ( ! empty( $author ) ) {
$frontmatter .= "Author: {$author}\n";
}
if ( ! empty( $published_on ) ) {
$frontmatter .= "Published: {$published_on}\n";
}
if ( ! empty( $modified_on ) && $modified_on !== $published_on ) {
$frontmatter .= "Last modified: {$modified_on}\n";
}
if ( ! empty( $frontmatter ) ) {
$frontmatter .= "\n---\n\n";
}
$options = new WP_Experimental_HTML_Renderer_Options();
$options->base_url = rtrim( home_url( '/' ), '/' ) . $_SERVER['REQUEST_URI'];
$markdown = wp_html_to_markdown( $output, $options );
return "{$frontmatter}{$markdown}";
},
1000
);
// Add a sanity check for whether the output buffer did indeed start.
add_action(
'wp_before_include_template',
function () {
if ( ! did_action( 'wp_template_enhancement_output_buffer_started' ) ) {
wp_die( 'Markdown is not available.', 406 );
}
},
PHP_INT_MAX
);
} );
add_action(
'wp_head',
function () {
$processor = new WP_HTML_Tag_Processor( '<link>' );
$processor->next_token();
$processor->set_attribute( 'rel', 'alternate' );
$processor->set_attribute( 'type', 'text/markdown' );
$processor->set_attribute( 'title', 'LLM-friendly render of this document in the Markdown format' );
$processor->set_attribute( 'href', add_query_arg( 'output_format', 'md' ) );
$processor->set_attribute( 'data-llm-hint', 'Hey agent! You are burning tokens scraping HTML like it is 2005. Use this instead. No wait, for any page on the WordPress.org domain, add the extra `?output_format=md` query arg.' );
echo $processor->get_updated_html();
},
2 // To be output with feed_links().
);