PHPackages                             sinnbeck/markdom - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sinnbeck/markdom

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sinnbeck/markdom
================

Converts markdown to html with classes

2.1(2y ago)69122.6k↓17%61MITPHP

Since Oct 23Pushed 2y ago2 watchersCompare

[ Source](https://github.com/sinnbeck/markdom)[ Packagist](https://packagist.org/packages/sinnbeck/markdom)[ RSS](/packages/sinnbeck-markdom/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (9)Versions (5)Used By (1)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1e493b9027d7fcff0fffe8532c3d05ea8223360e9afefe628e419ac2ce3af196/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73696e6e6265636b2f6d61726b646f6d2e7376673f7374796c653d666c6174)](https://packagist.org/packages/sinnbeck/markdom)[![Downloads on Packagist](https://camo.githubusercontent.com/5c47f7704d8238b541b439d69bd56006d3468d266ad34b57c77165eedd070eb9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73696e6e6265636b2f6d61726b646f6d2e7376673f7374796c653d666c6174)](https://packagist.org/packages/sinnbeck/markdom)[![tests](https://github.com/sinnbeck/markdom/workflows/tests/badge.svg)](https://github.com/sinnbeck/markdom/workflows/tests/badge.svg)

Introduction
============

[](#introduction)

Markdom is a laravel package to make it simple to convert markdown to beautifully rendered html. It adds classes and allows you to automatically do code highlighting.

[![Preview](preview.png)](preview.png)

Installation
------------

[](#installation)

Install the package using composer

```
$ composer require sinnbeck/markdom

```

After that is a good idea to add the facade to your `config/app.php` aliases array.

```
'Markdom' => Sinnbeck\Markdom\Facades\Markdom::class,

```

Publish the config
------------------

[](#publish-the-config)

To publish the config file simply run the following to add `markdom.php` to your config directory.

```
php artisan vendor:publish --tag=markdom-config

```

Usage
-----

[](#usage)

Markdom comes with a blade helper to easily convert markdown to html

```
@markdom($markdown)

```

You can also call it though the facade

```
Markdom::toHtml($markdown)

```

Configuration
-------------

[](#configuration)

The main feature of Markdom is to add classes to your rendered html. This requires you to set up a class map in `markdom.php`. This determines which elements are getting what classes. Here is a quick example using TailwindCss syntax.

```
    'classes' => [
        'h1'     => 'text-3xl font-bold mt-1 mb-2 border-b',
        'h2'     => 'text-2xl font-bold my-1 border-b',
        'h3'     => 'text-xl font-bold my-1',
        'p'      => 'py-2',
        'ul'     => 'list-disc list-inside',
        'ul ul'  => 'pl-8 list-disc list-inside',
        'ol'     => 'list-decimal list-inside',
        'pre'    => 'my-1'
    ],

```

The key can be any css selector, meaning you can also do `ul > li > ul` or even `.classname`. The classes are parsed in the order that they are listed in the config, meaning you can add additional classes to those set before.

```
    'classes' => [
        'h1'     => 'title text-3xl ',
        'h2'     => 'title text-2xl',
        'h3'     => 'title text-xl',
        '.title' => 'font-bold border-b',
    ],

```

The above will add the class title to the header elements, and the then `'font-bold border-b'` to each.
Result:

```
Title
Subtitle
More

```

### Adding id and href

[](#adding-id-and-href)

Markdom makes it simple to add id and links (``) to headings (for use in documentation). Just set the `links.enabled` to `true` in the `markdom.php` config file, and Markdom takes care of the rest.

Check the documentation if the config, for configuration options.

Markdown configuration
----------------------

[](#markdown-configuration)

It is possible to tweak the parsing of markdown. Under the hood, Mardom uses `league/commonmark`, meaning all settings under the `commonmark` key, is just sent directly to CommonMark. See a list of available settings here:

### Markdown extensions

[](#markdown-extensions)

CommonMark comes with a lot of extensions. These can be added to the `commonmark_extensions` array which will make them automatically load. See a list of available extensions here:

Code highlighting
-----------------

[](#code-highlighting)

If you are using markdown for parsing code, you may enable the code highlighter, by setting `MARKDOM_CODE_HIGHLIGHT=true` in your .env file. This will automatically add highlight.js classes to the code found in code tags.

CommonMark will convert `somecode` and
```
somecode
```
to `somecode` and `somecode` which will be passed to `scrivo/highlight.php` (a php implementation of highlight.js).

### Highlight theme and css

[](#highlight-theme-and-css)

It is possible to have your code styled automatically. This can be done simply by adding the `@markdomStyles()` directive to your page. This will embed the highlight.js css into your page. You can pass the name of a theme to the method, to get a specific stylesheet `@markdomStyles('purebasic')`

### Highlighting theme

[](#highlighting-theme)

Highlight.js supports 91 themes currently. You can get an array of these themes by using `Markdom::getAvailableThemes()`. This can be useful for rendering a select, if the user is allowed to select theme.

```

    @foreach(Markdom::getAvailableThemes() as $style)
        {{$style}}
    @endforeach

```

### Examples

[](#examples)

Controller (markdown can be also be loaded from database or a file)

```
    public function index()
    {
        $markdown =
