Skip to content

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 4, 2021
2 parents 6a7fad0 + 1191d6d commit f8e2c37
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v2.0.0
## 12/03/2021

1. [](#new)
* Added support for new remote injects.

# v1.4.5
## 04/27/2021

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ You should now have all the plugin files under
enabled: true
active: true
processed_content: true
remote_injections:
```

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.
Expand Down Expand Up @@ -70,4 +71,24 @@ There are two ways to use this plugin in your markdown content:
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.
## Remote Injects
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:
```yaml
remote_injections:
dev: https://dev.somehost.com/
foo: https://foo.com/bar
```

This will then allow you to inject page content from one Grav instance to another using this syntax:

```markdown
[plugin:page-inject](remote://dev/home/modular/_callout)
```

Where the `remote://dev` protocol tells the plugin to retrieve the requested page from the `dev` injection configuration via the path `/home/modular/_callout`.

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.

[grav]: http://github.com/getgrav/grav
12 changes: 10 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Page Inject
type: plugin
slug: page-inject
version: 1.4.5
version: 2.0.0
description: "**Page Inject** is a powerful plugin that lets you inject entire pages or page content into other pages using simple markdown syntax"
icon: trello
author:
Expand All @@ -13,7 +13,7 @@ keywords: inject, embed, markdown
bugs: https://github.com/getgrav/grav-plugin-page-inject/issues
license: MIT
dependencies:
- { name: grav, version: '>=1.6.0' }
- { name: grav, version: '>=1.7.25' }

form:
validation: strict
Expand Down Expand Up @@ -52,3 +52,11 @@ form:
validate:
type: bool
help: If enabled the page is pre-rendered before being injected, so relative paths work correctly

remote_injections:
type: array
label: Remote Injections
help: Key should be a short slug, e.g. "dev", and the full URL should be "https://foo.com/path"
placeholder_key: Short slug
placeholder_value: Full Url

34 changes: 32 additions & 2 deletions page-inject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Grav\Framework\Psr7\Response;
use Grav\Framework\RequestHandler\Exception\RequestException;
use Grav\Plugin\Admin\Admin;
use http\Client\Request;
use Psr\Http\Message\ResponseInterface;
use RocketTheme\Toolbox\Event\Event;

Expand Down Expand Up @@ -53,6 +54,7 @@ public function onPluginsInitialized()

$this->enable([
'onPageContentRaw' => ['onPageContentRaw', 0],
'onPagesInitialized' => ['onPagesInitialized', 0],
]);
}

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


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

$page_path = Uri::convertUrl($page, $page_path, 'link', false, true);
preg_match('#remote://(.*?)/(.*)#', $page_path, $remote_matches);

if (isset($remote_matches[1]) && $remote_matches[2]) {
$remote_injections = $this->grav['config']->get('plugins.page-inject.remote_injections', []);
$remote_url = $remote_injections[$remote_matches[1]] ?? null;
if ($remote_url) {
$url = $remote_url . '/?action=contentInject&path=/' . urlencode($remote_matches[2]);
$response = \Grav\Common\HTTP\Response::get($url);
return $response;
}
} else {
$page_path = Uri::convertUrl($page, $page_path, 'link', false, true);

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

// do the replacement
return str_replace($search, $replace, $search);
}


};

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

public function onPagesInitialized()
{
$uri = $this->grav['uri'];
$action = $uri->query('action');
$path = $uri->query('path');
if ($action === 'contentInject' && isset($path)) {
$pages = $this->grav['pages'];
$page = $pages->find($path);
if ($page instanceof PageInterface && $page->published()) {
echo $page->content();
exit;
}
}
}

protected function parseInjectLinks($content, $function)
{
$regex = '/\[plugin:(content-inject|page-inject)\]\(((.*)\?template=(.*)|(.*))\)/i';
Expand Down
3 changes: 2 additions & 1 deletion page-inject.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
enabled: true
active: true
processed_content: true
processed_content: true
remote_injections:

0 comments on commit f8e2c37

Please sign in to comment.