Skip to content

Commit 7ac7bfc

Browse files
authored
Feeds and comments support (#1)
1 parent 905060d commit 7ac7bfc

File tree

2 files changed

+335
-10
lines changed

2 files changed

+335
-10
lines changed

README.md

Lines changed: 140 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP 8.1+
88

99
## Usage
1010

11+
### Single posts
12+
1113
Visit any single post on your site with the `Accept` header set to `text/markdown` to get the post content directly as Markdown.
1214

1315
**Example:**
@@ -16,6 +18,60 @@ Visit any single post on your site with the `Accept` header set to `text/markdow
1618
curl -H "Accept: text/markdown" https://example.com/my-awesome-post
1719
```
1820

21+
### Markdown feed
22+
23+
Access a feed of your posts in Markdown format at `/feed/markdown/`:
24+
25+
**Example:**
26+
27+
```bash
28+
curl https://example.com/feed/markdown/
29+
```
30+
31+
The feed includes:
32+
- Feed metadata (site name, description, last updated, feed URL)
33+
- Post title, author, publication date (ISO 8601), and permalink
34+
- Post categories and tags
35+
- Post content converted to Markdown
36+
- Optional excerpt support
37+
- Optional comment support
38+
39+
**Example Output:**
40+
41+
```markdown
42+
# My WordPress Site - Markdown Feed
43+
44+
**Description:** Just another WordPress site
45+
**Last Updated:** 2025-10-03T19:45:00+00:00
46+
**Feed URL:** https://example.com/feed/markdown/
47+
48+
---
49+
50+
# Hello World!
51+
52+
**Author:** John Doe
53+
**Published:** 2025-10-03T12:00:00+00:00
54+
**URL:** https://example.com/hello-world/
55+
**Categories:** News, Updates
56+
**Tags:** announcement, wordpress
57+
58+
Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
59+
60+
---
61+
```
62+
63+
**Feed URL structure:**
64+
65+
The Markdown feed is accessible at `https://yoursite.com/feed/markdown/`. Note that WordPress requires pretty permalinks to be enabled (Settings → Permalinks must be set to anything other than "Plain").
66+
67+
**Autodiscovery:**
68+
69+
The plugin automatically adds a `<atom:link>` element to your site's RSS feed, allowing feed readers and LLMs to discover the Markdown version:
70+
71+
```xml
72+
<atom:link href="https://example.com/feed/markdown/" rel="alternate" type="text/markdown" />
73+
```
74+
1975
## Installation
2076

2177
### via Composer
@@ -30,13 +86,17 @@ composer require roots/post-content-to-markdown
3086
2. Place in `wp-content/plugins/post-content-to-markdown/`
3187
3. Activate via wp-admin or WP-CLI
3288

89+
**Note:** After activation, you may need to flush rewrite rules by visiting Settings → Permalinks and clicking "Save Changes" if the `/feed/markdown/` endpoint doesn't work immediately.
90+
3391
## Filters
3492

3593
The plugin provides several filters for customization:
3694

37-
### `post_content_to_markdown/post_types`
95+
### Single post filters
96+
97+
#### `post_content_to_markdown/post_types`
3898

39-
Filter the post types that can be served as markdown.
99+
Filter the post types that can be served as Markdown for single posts.
40100

41101
```php
42102
add_filter('post_content_to_markdown/post_types', function ($post_types) {
@@ -47,7 +107,71 @@ add_filter('post_content_to_markdown/post_types', function ($post_types) {
47107

48108
**Default:** `['post']`
49109

50-
### `post_content_to_markdown/converter_options`
110+
### Feed filters
111+
112+
#### `post_content_to_markdown/feed_post_types`
113+
114+
Filter the post types included in the Markdown feed.
115+
116+
```php
117+
add_filter('post_content_to_markdown/feed_post_types', function ($post_types) {
118+
return ['post', 'page'];
119+
});
120+
```
121+
122+
**Default:** `['post']`
123+
124+
#### `post_content_to_markdown/feed_posts_per_page`
125+
126+
Filter the number of posts included in the Markdown feed.
127+
128+
```php
129+
add_filter('post_content_to_markdown/feed_posts_per_page', function ($count) {
130+
return 20;
131+
});
132+
```
133+
134+
**Default:** `10`
135+
136+
#### `post_content_to_markdown/feed_include_comments`
137+
138+
Enable or disable comments in the Markdown feed.
139+
140+
```php
141+
add_filter('post_content_to_markdown/feed_include_comments', function () {
142+
return true;
143+
});
144+
```
145+
146+
**Default:** `false`
147+
148+
#### `post_content_to_markdown/feed_include_excerpt`
149+
150+
Enable or disable post excerpts in the Markdown feed.
151+
152+
```php
153+
add_filter('post_content_to_markdown/feed_include_excerpt', function () {
154+
return true;
155+
});
156+
```
157+
158+
**Default:** `false`
159+
160+
#### `post_content_to_markdown/feed_cache_duration`
161+
162+
Filter the cache duration for the Markdown feed in seconds.
163+
164+
```php
165+
add_filter('post_content_to_markdown/feed_cache_duration', function ($duration) {
166+
return 2 * HOUR_IN_SECONDS; // Cache for 2 hours
167+
});
168+
```
169+
170+
**Default:** `HOUR_IN_SECONDS` (1 hour)
171+
172+
### Conversion filters
173+
174+
#### `post_content_to_markdown/converter_options`
51175

52176
Filter the HTML to Markdown converter options.
53177

@@ -64,25 +188,33 @@ add_filter('post_content_to_markdown/converter_options', function ($options) {
64188

65189
**Available options:**
66190
- `header_style`: `'atx'` (default) or `'setext'`
67-
- `strip_tags`: Remove HTML tags without markdown equivalents (default: `true`)
191+
- `strip_tags`: Remove HTML tags without Markdown equivalents (default: `true`)
68192
- `remove_nodes`: Space-separated list of DOM nodes to remove (default: `'script style'`)
69193
- `hard_break`: Convert `<br>` to newlines (default: `true`)
70194

71-
### `post_content_to_markdown/markdown_output`
195+
#### `post_content_to_markdown/markdown_output`
72196

73-
Filter the final markdown output after conversion.
197+
Filter the final Markdown output after conversion.
74198

75199
```php
76200
add_filter('post_content_to_markdown/markdown_output', function ($markdown, $original_html) {
77-
// Add a footer to all markdown output
201+
// Add a footer to all Markdown output
78202
return $markdown . "\n\n---\nConverted from HTML to Markdown";
79203
}, 10, 2);
80204
```
81205

82206
**Parameters:**
83-
- `$markdown`: The converted markdown text
207+
- `$markdown`: The converted Markdown text
84208
- `$original_html`: The original HTML content
85209

210+
## Performance
211+
212+
The Markdown feed is cached for 1 hour by default to optimize performance. The cache is automatically cleared when:
213+
- A post is published or updated
214+
- A post is deleted
215+
216+
You can customize the cache duration using the `post_content_to_markdown/feed_cache_duration` filter.
217+
86218
## Resources
87219

88220
* [Serving Markdown Based on Accept Headers and User Agent Detection](https://benword.com/serving-markdown-based-on-accept-headers-and-user-agent-detection)

0 commit comments

Comments
 (0)