Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Include .md file in default.md

Solved by Ron Wardenier View solution

Started by blackbird 6 years ago · 6 replies · 1593 views
6 years ago

Hello.

I'm just starting to use Grav and one of the things I miss (or have not yet found a plug-in for) is how I can include a .md file from within e.g. default.md.

As an example, if I have a default.md like:

TWIG
---
title: Demo page
---

This is an example of what I want.

{{ include "some_other.md" }}

And this some_other.md file, which is in the same folder as default.md, will be inserted and rendered at the include location.

So whenever Grav will load this (default.md) page, it needs to include the raw some_other.md file and renders this at that location along with the rest of the default.md file.

I need this to include some md files I create via some scripts to be inserted into a Grav page.

Please help. ;)

6 years ago

Hello Paul,

I've seen this page inject plug-in but this does not do what I want. I don't want to inject another Grav page, I want to inject just another, non Grav, markdown file sitting somewhere in the file tree within Grav.

last edited 07/31/20 by blackbird
6 years ago Solution

The following code is a modified version of the Page Inject Plugin which features a third kind of injection: [plugin:markdown-inject](/route/to/file).

The plugin will look for the file starting at the Grav user directory. If the file exists it will insert it's content.

Simply replace the page-inject.php file with the code. Take care when there's an update of the original plugin though.

page-inject.php:

PHP
<?php
/**
 * PageInject (modified to inject plain markdown from files)
 *
 * This plugin embeds other Grav pages from markdown URLs
 *
 * Licensed under MIT, see LICENSE.
 */

namespace Grav\Plugin;

use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use RocketTheme\Toolbox\Event\Event;

class PageInjectPlugin extends Plugin
{
    /**
     * Return a list of subscribed events.
     *
     * @return array    The list of events of the plugin of the form
     *                      'name' => ['method_name', priority].
     */
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0],
        ];
    }

    /**
     * Initialize configuration.
     */
    public function onPluginsInitialized()
    {
        if ($this->isAdmin()) {
            $this->active = false;
            return;
        }

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

    /**
     * Add content after page content was read into the system.
     *
     * @param  Event  $event An event object, when `onPageContentRaw` is fired.
     */
    public function onPageContentRaw(Event $event)
    {
        /** @var Page $page */
        $page = $event['page'];

        /** @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();

            // build an anonymous function to pass to `parseLinks()`
            $function = function ($matches) use (&$page, &$twig, &$config) {

                $search = $matches[0];
                $type = $matches[1];
                $page_path = $matches[3] ?: $matches[2];
                $template = $matches[4];

                if ($type == 'markdown-inject') {
                    // "/route/to/page" from user dir
                    $user_path = $this->grav['locator']->findResource('user://');

                    if (file_exists($user_path . '/' . $page_path)) {
                        $replace = file_get_contents($user_path . '/' . $page_path);
                    }

                } else {

                    $page_path = Uri::convertUrl($page, $page_path, 'link', false, true);

                    $inject = $page->find($page_path);
                    if ($inject) {
                        // Force HTML to avoid issues with News Feeds
                        $inject->templateFormat('html');
                        if ($type == 'page-inject') {
                            if ($template) {
                                $inject->template($template);
                            }
                            $inject->modularTwig(true);
                            $replace = $inject->content();

                        } else {
                            if ($config->get('processed_content')) {
                                $replace = $inject->content();
                            } else {
                                $replace = $inject->rawMarkdown();
                            }
                        }

                    } else {

                        // replace with what you started with
                        $replace = $matches[0];
                    }
                }

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

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

    protected function parseInjectLinks($content, $function)
    {
        $regex = '/\[plugin:(content-inject|page-inject|markdown-inject)\]\(((.*)\?template=(.*)|(.*))\)/i';
        return preg_replace_callback($regex, $function, $content);
    }
}
👍 1
6 years ago

Super cool @bleutzinn, thanks for sharing that! Hope you and yours are doing well.

6 years ago

Many thanks to @bleutzinn, your adaptation to the page inject plug-in is just what I need. Now I can inject .md sections in my pages other scripts generate off-site.

Maybe your adaptation should be part of the page inject plug-in, or maybe a new markdown-inject plug-in?

Thanks again. :)

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 95 14 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago