davidbadura/markdown-builder

Markdown Builder

1.1.1 2021-10-23 09:22 UTC

README

Mutation testing badge Type Coverage Latest Stable Version License

A simple helper class to create markdown.

Installation

composer require 'davidbadura/markdown-builder'

Usage

$builder = new MarkdownBuilder();

$builder
    ->h1('Markdown Builder')
    ->p('A simple helper class to create markdown.')
    ->h2('Install ' . $builder->inlineBold('this') . ' powerfull library')
    ->code("composer require 'davidbadura/markdown-builder@dev'", 'bash')
    ->h2('Todos')
    ->bulletedList([
        'write tests',
        (string)$builder
            ->block()
            ->numberedList(['A', 'B', 'C'])
        ,
        'add more markdown features'
    ]);
    
echo $builder->getMarkdown();
Markdown Builder
================

A simple helper class to create markdown.

Install **this** powerfull library
----------------------------------

```bash
composer require 'davidbadura/markdown-builder@dev'
```

Todos
-----
* write tests
* 1. A
  2. B
  3. C
* add more markdown features

API

The markdown builder have two kinds of elements, block and inline elements. Block elements will be buffered and you can get the markdown if you call the method getMarkdown(). All inline Elements are prefixed with inline* and you get instantly the markdown output.

Block Elements

h1

PHP-Code:

echo (new MarkdownBuilder())->h1('Hello World')->getMarkdown();

Markdown:

Hello World
===========

h2

PHP-Code:

echo (new MarkdownBuilder())->h2('Hello second world')->getMarkdown();

Markdown:

Hello second world
------------------

h3

PHP-Code:

echo (new MarkdownBuilder())->h3('My name is...')->getMarkdown();

Markdown:

### My name is...

p

PHP-Code:

echo (new MarkdownBuilder())->p('paragraph')->getMarkdown();

Markdown:

paragraph

blockquote

PHP-Code:

echo (new MarkdownBuilder())->blockquote("Foo\nBar\nBaz")->getMarkdown();

Markdown:

> Foo
> Bar
> Baz

bulleted list

PHP-Code:

echo (new MarkdownBuilder())->bulletedList(['Foo', 'Bar', 'Baz'])->getMarkdown();

Markdown:

* Foo
* Bar
* Baz

numbered list

PHP-Code:

echo (new MarkdownBuilder())->numberedList(['Foo', 'Bar', 'Baz'])->getMarkdown();

Markdown:

1. Foo
2. Bar
3. Baz

hr

PHP-Code:

echo (new MarkdownBuilder())->hr()->getMarkdown();

Markdown:

------------------------

code

PHP-Code:

echo (new MarkdownBuilder())->code('$var = "test";', 'php')->getMarkdown();

Markdown:

```php
$var = "test";
```

table

PHP-Code:

echo (new MarkdownBuilder())->table(
    ['col0', 'col1'],
    [
        ['row01', 'row01'],
        ['row10', 'row11'],
    ],
    [Alignment::CENTER, Alignment::RIGHT]
)->getMarkdown();

Markdown:

|col0|col1|
|:-:|-:|
|row01|row01|

Inline Blocks

bold

PHP-Code:

echo (new MarkdownBuilder())->inlineBold('Hey!');

Markdown:

**Hey!**

italic

PHP-Code:

echo (new MarkdownBuilder())->inlineItalic('huhu');

Markdown:

*huhu*

code

PHP-Code:

echo (new MarkdownBuilder())->inlineCode('$var = "test";');

Markdown:

`$var = "test";`

link

PHP-Code:

echo (new MarkdownBuilder())->inlineLink('http://google.de', 'Google', 'Title');

Markdown:

[Google](http://google.de "Title")

img

PHP-Code:

echo (new MarkdownBuilder())->inlineImg('cat.jpg', 'Cat', 'Title');

Markdown:

![Cat](cat.jpg "Title")

Advance features

if you need collapse blocks, you can create a new builder instance with his own clean buffer. this can you do by call block().

PHP-Code:

$builder = new MarkdownBuilder();
$builder->blockqoute(
    (string)$builder
      ->block()
      ->h1('Lists')
      ->bulletedList([
        'Foo',
        $builder->block()->numberedList(['A', 'B', 'C']),
        'Bar'
      ])
);

Markdown:

>  Lists
>  =====
>
>  * Foo
>  * 1. A
>    2. B
>    3. C
>  * Bar