Skip to content

Commit f8e2c37

Browse files
committed
Merge branch 'release/2.0.0'
2 parents 6a7fad0 + 1191d6d commit f8e2c37

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v2.0.0
2+
## 12/03/2021
3+
4+
1. [](#new)
5+
* Added support for new remote injects.
6+
17
# v1.4.5
28
## 04/27/2021
39

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ You should now have all the plugin files under
2828
enabled: true
2929
active: true
3030
processed_content: true
31+
remote_injections:
3132
```
3233

3334
If you need to change any value, then the best process is to copy the [page-inject.yaml](page-inject.yaml) file into your `users/config/plugins/` folder (create it if it doesn't exist), and then modify there. This will override the default settings.
@@ -70,4 +71,24 @@ There are two ways to use this plugin in your markdown content:
7071
7172
Sometimes you just want the content of another page injected directly into your current page. Use `content-inject` for this purpose. The content is not rendered with the associated twig template, merely injected into the current page.
7273
74+
## Remote Injects
75+
76+
It is now possible to retrieve remote content from another Grav instance as long as both of the sites are running the latest version of the `page-inject` plugin. First in the **client** Grav instance you need to define a remote connection to another Grav **server** in the plugin configuration. For example:
77+
78+
```yaml
79+
remote_injections:
80+
dev: https://dev.somehost.com/
81+
foo: https://foo.com/bar
82+
```
83+
84+
This will then allow you to inject page content from one Grav instance to another using this syntax:
85+
86+
```markdown
87+
[plugin:page-inject](remote://dev/home/modular/_callout)
88+
```
89+
90+
Where the `remote://dev` protocol tells the plugin to retrieve the requested page from the `dev` injection configuration via the path `/home/modular/_callout`.
91+
92+
This is particularly useful for modular content that is already a snippet of content that is being reused on the **server**. This will retrieve the content, and because a modular page's content is pre-rendered with the appropriate Twig template, it will include all the HTML of the modular page. If you request a regular page (non-modular), there will be no Twig and just plain HTML content will be sent.
93+
7394
[grav]: http://github.com/getgrav/grav

blueprints.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Page Inject
22
type: plugin
33
slug: page-inject
4-
version: 1.4.5
4+
version: 2.0.0
55
description: "**Page Inject** is a powerful plugin that lets you inject entire pages or page content into other pages using simple markdown syntax"
66
icon: trello
77
author:
@@ -13,7 +13,7 @@ keywords: inject, embed, markdown
1313
bugs: https://github.com/getgrav/grav-plugin-page-inject/issues
1414
license: MIT
1515
dependencies:
16-
- { name: grav, version: '>=1.6.0' }
16+
- { name: grav, version: '>=1.7.25' }
1717

1818
form:
1919
validation: strict
@@ -52,3 +52,11 @@ form:
5252
validate:
5353
type: bool
5454
help: If enabled the page is pre-rendered before being injected, so relative paths work correctly
55+
56+
remote_injections:
57+
type: array
58+
label: Remote Injections
59+
help: Key should be a short slug, e.g. "dev", and the full URL should be "https://foo.com/path"
60+
placeholder_key: Short slug
61+
placeholder_value: Full Url
62+

page-inject.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Grav\Framework\Psr7\Response;
2121
use Grav\Framework\RequestHandler\Exception\RequestException;
2222
use Grav\Plugin\Admin\Admin;
23+
use http\Client\Request;
2324
use Psr\Http\Message\ResponseInterface;
2425
use RocketTheme\Toolbox\Event\Event;
2526

@@ -53,6 +54,7 @@ public function onPluginsInitialized()
5354

5455
$this->enable([
5556
'onPageContentRaw' => ['onPageContentRaw', 0],
57+
'onPagesInitialized' => ['onPagesInitialized', 0],
5658
]);
5759
}
5860

@@ -140,7 +142,6 @@ public function onPageContentRaw(Event $event)
140142
/** @var Config $config */
141143
$config = $this->mergeConfig($page);
142144

143-
144145
if ($config->get('enabled') && $config->get('active')) {
145146
// Get raw content and substitute all formulas by a unique token
146147
$raw = $page->getRawContent();
@@ -153,7 +154,18 @@ public function onPageContentRaw(Event $event)
153154
$page_path = $matches[3] ?: $matches[2];
154155
$template = $matches[4];
155156

156-
$page_path = Uri::convertUrl($page, $page_path, 'link', false, true);
157+
preg_match('#remote://(.*?)/(.*)#', $page_path, $remote_matches);
158+
159+
if (isset($remote_matches[1]) && $remote_matches[2]) {
160+
$remote_injections = $this->grav['config']->get('plugins.page-inject.remote_injections', []);
161+
$remote_url = $remote_injections[$remote_matches[1]] ?? null;
162+
if ($remote_url) {
163+
$url = $remote_url . '/?action=contentInject&path=/' . urlencode($remote_matches[2]);
164+
$response = \Grav\Common\HTTP\Response::get($url);
165+
return $response;
166+
}
167+
} else {
168+
$page_path = Uri::convertUrl($page, $page_path, 'link', false, true);
157169

158170
$inject = $page->find($page_path);
159171
if ($inject) {
@@ -181,13 +193,31 @@ public function onPageContentRaw(Event $event)
181193

182194
// do the replacement
183195
return str_replace($search, $replace, $search);
196+
}
197+
198+
184199
};
185200

186201
// set the parsed content back into as raw content
187202
$page->setRawContent($this->parseInjectLinks($raw, $function));
188203
}
189204
}
190205

206+
public function onPagesInitialized()
207+
{
208+
$uri = $this->grav['uri'];
209+
$action = $uri->query('action');
210+
$path = $uri->query('path');
211+
if ($action === 'contentInject' && isset($path)) {
212+
$pages = $this->grav['pages'];
213+
$page = $pages->find($path);
214+
if ($page instanceof PageInterface && $page->published()) {
215+
echo $page->content();
216+
exit;
217+
}
218+
}
219+
}
220+
191221
protected function parseInjectLinks($content, $function)
192222
{
193223
$regex = '/\[plugin:(content-inject|page-inject)\]\(((.*)\?template=(.*)|(.*))\)/i';

page-inject.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
enabled: true
22
active: true
3-
processed_content: true
3+
processed_content: true
4+
remote_injections:

0 commit comments

Comments
 (0)