Skip to content

Commit 04e1bd2

Browse files
committed
created medium rss grav plugin
1 parent e1aad89 commit 04e1bd2

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Medium RSS Plugin
2+
3+
A plugin for [Grav CMS](https://getgrav.org/) that fetches the RSS profile page feed from [Medium](https://medium.com/) and displays the titles and teasers of your articles in a list.
4+
5+
## Installation
6+
7+
1. Upload the plugin files to the `user/plugins/mediumrss` directory of your Grav installation.
8+
2. Make sure the plugin directory is named `mediumrss`.
9+
3. Enable the plugin in the `mediumrss.yaml` file or via the Admin Panel.
10+
4. Use as page template `medium`.
11+
12+
## Configuration
13+
14+
The plugin configuration is done through the `blueprints.yaml` file. Here are the available settings:
15+
16+
```yaml
17+
enabled: true
18+
rss_feed_url: 'https://medium.com/feed/@your-username'
19+
target: '_blank'
20+
```
21+
## Usage
22+
The plugin automatically inserts the latest Medium posts into your page. The posts are displayed using the _medium.html.twig_ template.
23+
24+
This plugin is licensed under the MIT License

blueprints.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Medium RSS Plugin
2+
description: Ein Plugin für Grav CMS, das den RSS-Feed von Medium abruft und die Titel und Teaser in einer Liste anzeigt.
3+
version: 1.0.0
4+
icon: medium
5+
author:
6+
name: Samuel Stein
7+
email: mail@samuelstein.de
8+
url: https://github.com/samuelstein/mediumrss
9+
form:
10+
validation: strict
11+
fields:
12+
enabled:
13+
type: toggle
14+
label: Plugin status
15+
highlight: 1
16+
default: 1
17+
options:
18+
1: PLUGIN_ADMIN.ENABLED
19+
0: PLUGIN_ADMIN.DISABLED
20+
validate:
21+
type: bool
22+
23+
rss_feed_url:
24+
type: text
25+
size: large
26+
label: RSS Feed URL
27+
placeholder: 'https://medium.com/feed/@your-username'
28+
help: 'Die URL des Medium RSS-Feeds, den Sie abrufen möchten.'
29+
validate:
30+
type: url
31+
32+
target:
33+
type: select
34+
size: medium
35+
label: Link Target
36+
help: 'Wählen Sie, ob die Links in einem neuen Tab oder im selben Tab geöffnet werden sollen.'
37+
default: '_blank'
38+
options:
39+
_blank: 'Neuer Tab'
40+
_self: 'Gleicher Tab'

mediumrss.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace Grav\Plugin;
3+
4+
use Grav\Common\Plugin;
5+
use RocketTheme\Toolbox\Event\Event;
6+
7+
class MediumRssPlugin extends Plugin
8+
{
9+
public static function getSubscribedEvents()
10+
{
11+
return [
12+
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
13+
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
14+
];
15+
}
16+
17+
public function onTwigTemplatePaths(Event $event)
18+
{
19+
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
20+
}
21+
22+
public function onTwigSiteVariables(Event $event)
23+
{
24+
$this->grav['twig']->twig_vars['fetchMediumRSS'] = $this->fetchMediumRSS();
25+
}
26+
27+
private function fetchMediumRSS()
28+
{
29+
$response = file_get_contents($this->config->get('plugins.mediumrss.rss_feed_url'));
30+
$xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
31+
$items = $xml->channel->item;
32+
$output = '';
33+
34+
foreach ($items as $item) {
35+
$title = (string) $item->title;
36+
$link = (string) $item->link;
37+
$description = (string) $item->description;
38+
$content = (string) $item->children('content', true)->encoded;
39+
40+
// Überprüfen, ob der Inhalt nicht leer ist
41+
if (!empty($content)) {
42+
// Extrahiere den ersten Absatz des Inhalts
43+
preg_match('/<p>(.*?)<\/p>/', $content, $matches);
44+
$firstParagraph = $matches[1] ?? '';
45+
} else {
46+
$firstParagraph = '';
47+
}
48+
49+
$output .= '<li style="text-decoration: none; list-style-type: none;">';
50+
$output .= '<a href="' . htmlspecialchars($link) . '" target="'.$this->config->get('plugins.mediumrss.target').'"><strong>' . htmlspecialchars($title) . '</strong></a><br>';
51+
$output .= htmlspecialchars($firstParagraph);
52+
$output .= '</li><br>';
53+
}
54+
55+
return $output;
56+
}
57+
}

mediumrss.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
enabled: true

templates/medium.html.twig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends 'partials/base.html.twig' %}
2+
3+
{% block content %}
4+
<div class="center-content">
5+
{{ page.content }}
6+
</div>
7+
8+
<div id="rss-feed">
9+
<h2>Recent posts</h2>
10+
<ul id="rss-feed-list">
11+
{{ fetchMediumRSS|raw }}
12+
</ul>
13+
</div>
14+
{% endblock %}

0 commit comments

Comments
 (0)