Skip to content

Advanced Markdown

Introduction#

Since HydePHP makes heavy use of Markdown, there are some extra features and helpers created just for Hyde to make using Markdown even easier and more powerful!

Using Blade in Markdown#

Hyde supports Laravel Blade in Markdown through single-line BladeDown directives and full Blade blocks. Both forms are enabled by default in HydePHP v3.

To use Blade in your Markdown files, simply use the Blade shortcode directive, followed by your desired Blade string.

Standard syntax#

 [Blade]: {{ "Hello World!" }} // Will render: 'Hello World!'

Blade includes#

Only single-line shortcode directives are supported. If you need to use multi-line Blade code, use an @include directive to render a more complex Blade template. You can pass data to includes by specifying an array to the second argument.

 [Blade]: @include("hello-world")
 [Blade]: @include("hello", ["name" => "World"])

Blade blocks#

For multi-line Blade, use an executable blade render fenced block:

```blade render
@php($world = 'world')

{{ "Hello $world" }}
```

The Blade is evaluated at build time, and the rendered output is wrapped in a <div class="blade-block not-prose"> element. When compiling a page, the $page variable is available to the block.

You can also render a Blade component using the blade component(name) directive. Component data is passed using YAML front matter at the start of the block:

```blade component(alert)
---
type: warning
title: Check this
---
```

If the block does not start with YAML front matter, its content is rendered as Markdown and passed directly to the component slot. This is useful when the component does not need any data:

```blade component(alert)
This content is passed to the component **slot**.
```

To pass both component data and Markdown slot content, enclose the data in YAML front matter and add the Markdown after it:

```blade component(alert)
---
type: warning
title: Check this
---

This content is passed to the component **slot**.
```

A fence using only blade is an ordinary syntax-highlighted code sample and is not executed. Unsupported Blade block directives, including blade component without a component name, throw an exception.

Trusting Markdown content#

Blade directives and Blade blocks are both controlled by markdown.enable_blade, and can execute arbitrary PHP during the site build. Hyde projects normally treat source files committed to the project as trusted: content changes should be reviewed both for the text they publish and for executable directives hidden in the source.

If your site accepts Markdown outside that trusted review process, or builds pull requests before they have been reviewed, disable Blade in Markdown in the config/markdown.php file:

'enable_blade' => false,

Disabling Blade in Markdown is not a sandbox for contributors who can add arbitrary project files, since they could add a malicious Blade template instead. Treat project-level write access as trusted and review source changes before building them in a privileged environment.

Limitations#

All [Blade]: shortcodes must be the first word on a new line, and only single-line shortcodes are supported. Use Blade blocks for multi-line code.

Coloured Blockquotes#

The HydePHP Markdown converter also supports some extra directives and features. One of them being four different coloured blockquotes. Simply append the desired colour after the initial > character.

‎> Normal Blockquote
‎>info Info Blockquote
‎>warning Warning Blockquote
‎>danger Danger Blockquote
‎>success Success Blockquote

Normal Blockquote

Info Blockquote

Warning Blockquote

Danger Blockquote

Success Blockquote

Customizations#

You can easily customize these styles by publishing and editing the markdown-blockquote.blade.php file.

php hyde publish:views components

Markdown usage#

The coloured blockquotes also support inline Markdown, just like normal blockquotes.

‎>info Formatting is **supported**!

Limitations#

Note that these currently do not support multi-line blockquotes.

Code Block Filepaths#

When browsing these documentation pages you may have noticed a label in the top right corner of code blocks specifying the file path. These are also created by using a custom Hyde feature that turns code comments into automatic code blocks.

Usage#

Simply add a code comment with the path in the first line of a fenced code block like so:

```php
‎// filepath: hello-world.php

echo 'Hello World!';
```

Which becomes:

echo 'Hello World!';

Alternative syntax#

The syntax is rather forgiving, by design, and supports using both // and # for comments. The colon is also optional, and the 'filepath' string is case-insensitive. So the following is also perfectly valid:

```js
‎// filepath hello.js
console.log('Hello World!');
```

If you have a newline after the filepath, like in the first example, it will be removed so your code stays readable.

Advanced usage#

Since HTML in Markdown is enabled by default, anything within the path label will be rendered as HTML. This means you can add links, or even images to the label. This requires allow_html to remain true in config/markdown.php.

```markdown
‎// filepath: <a href="https://github.com">View file on Github</a>
```

Limitations#

The filepaths are hidden on mobile devices using CSS to prevent them from overlapping with the code block.

Hyde automatically adds clickable permalink anchors to headings in documentation pages. When you hover over a heading, a # link appears that you can click to get a direct link to that section.

Usage & Configuration#

The feature is enabled by default for documentation pages. When enabled, Hyde will automatically add permalink anchors to headings between levels 2-4 (h2-h4). The permalinks are hidden by default and appear when hovering over the heading.

You can enable it for other page types by adding the page class to the permalinks.pages array in the config/markdown.php file, or disable it for all pages by setting the array to an empty array.

'permalinks' => [
    'pages' => [
        \Hyde\Pages\DocumentationPage::class,
    ],
],

Advanced Customization#

Under the hood, Hyde uses a custom Blade-based heading renderer when converting Markdown to HTML. This allows for more flexibility and customization compared to standard Markdown parsers. You can also publish and customize the Blade component used to render the headings:

php hyde publish:components

This will copy the markdown-heading.blade.php component to your views directory where you can modify its markup and behavior.

HydePHP provides a powerful feature for automatically converting Markdown links to source files to the corresponding routes in the built site.

This allows for a much better writing experience when using an IDE, as you can easily navigate to the source file by clicking on the link. Hyde will then automatically resolve the link to the correct route when building the site, formatting the links properly using dynamic relative paths and your configured pretty_urls setting.

Usage#

Using the feature is simple: Just use the source file path when linking to the page you want to resolve:

[Home](/_pages/index.blade.php)
[Docs](/_docs/index.md)
[Featured Post](/_posts/hello-world.md)
![Logo](/_media/logo.svg)

As you can see, it works for both pages and media assets. The leading slash is optional and will be ignored by Hyde, but including it often gives better IDE support.

Behind the Scenes#

During the build process, HydePHP converts source paths to their corresponding routes and evaluates them depending on the page being rendered.

If your page is in the site root then:

  • /_pages/index.blade.php becomes index.html
  • /_media/logo.svg becomes media/logo.svg

If your page is in a subdirectory then:

  • /_pages/index.blade.php becomes ../index.html
  • /_media/logo.svg becomes ../media/logo.svg

Of course, if your page is in a more deeply nested directory, the number of ../ will increase accordingly.

We will also match your configured preference for pretty_urls and only include the .html extension when desired.

Limitations#

There are some limitations and considerations to keep in mind when using this feature:

  • This feature will not work for dynamic routes (not backed by a file)
  • If you rename a file, links will break. Your IDE may warn about this.
  • If a file is not found, we will not be able to see it when evaluating links.
  • Relative links are not supported (so ../_pages/index.blade.php will not work)

Configuration#

Full configuration reference#

All Markdown-related configuration options are in the config/markdown.php file. You can find the full reference on the Customization page.

Raw HTML Tags#

To convert Markdown, HydePHP uses the GitHub Flavored Markdown extension. HydePHP v3 allows raw HTML by default because project source is normally trusted and reviewed. If you process Markdown from outside your trusted review process, set the allow_html option to false in your config/markdown.php file to strip potentially unsafe HTML tags.

'allow_html' => false,

When HTML is allowed, Hyde configures the DisallowedRawHtml CommonMark extension so that no HTML tags are stripped out.

Tailwind Typography Prose Classes#

HydePHP uses the Tailwind Typography to style rendered Markdown. We do this by adding the .prose CSS class to the HTML elements containing the rendered Markdown, using the built-in Blade components.

You can easily edit these classes, for example if you want to customize the prose colours, in the config/markdown.php file.

'prose_classes' => 'prose dark:prose-invert', // [tl! remove]
'prose_classes' => 'prose dark:prose-invert prose-img:inline', // [tl! add]

Please note that if you add any new classes, you may need to recompile your CSS file.