A simple helper class to create markdown.
composer require 'davidbadura/markdown-builder'
$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
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.
PHP-Code:
echo (new MarkdownBuilder())->h1('Hello World')->getMarkdown();
Markdown:
Hello World
===========
PHP-Code:
echo (new MarkdownBuilder())->h2('Hello second world')->getMarkdown();
Markdown:
Hello second world
------------------
PHP-Code:
echo (new MarkdownBuilder())->h3('My name is...')->getMarkdown();
Markdown:
### My name is...
PHP-Code:
echo (new MarkdownBuilder())->p('paragraph')->getMarkdown();
Markdown:
paragraph
PHP-Code:
echo (new MarkdownBuilder())->blockquote("Foo\nBar\nBaz")->getMarkdown();
Markdown:
> Foo
> Bar
> Baz
PHP-Code:
echo (new MarkdownBuilder())->bulletedList(['Foo', 'Bar', 'Baz'])->getMarkdown();
Markdown:
* Foo
* Bar
* Baz
PHP-Code:
echo (new MarkdownBuilder())->numberedList(['Foo', 'Bar', 'Baz'])->getMarkdown();
Markdown:
1. Foo
2. Bar
3. Baz
PHP-Code:
echo (new MarkdownBuilder())->hr()->getMarkdown();
Markdown:
------------------------
PHP-Code:
echo (new MarkdownBuilder())->code('$var = "test";', 'php')->getMarkdown();
Markdown:
```php
$var = "test";
```
PHP-Code:
echo (new MarkdownBuilder())->table(
['col0', 'col1'],
[
['row01', 'row01'],
['row10', 'row11'],
],
[Alignment::CENTER, Alignment::RIGHT]
)->getMarkdown();
Markdown:
|col0|col1|
|:-:|-:|
|row01|row01|
PHP-Code:
echo MarkdownBuilder::inlineBold('Hey!');
Markdown:
**Hey!**
PHP-Code:
echo MarkdownBuilder::inlineItalic('huhu');
Markdown:
*huhu*
PHP-Code:
echo MarkdownBuilder::inlineCode('$var = "test";');
Markdown:
`$var = "test";`
PHP-Code:
echo MarkdownBuilder::inlineLink('http://google.de', 'Google');
Markdown:
[Google](http://google.de)
PHP-Code:
echo MarkdownBuilder::inlineImg('cat.jpg', 'Cat');
Or with width and height set:
echo MarkdownBuilder::inlineImg('cat.jpg', 'Cat', '20', '20');
Markdown:
![Cat](cat.jpg)
Or with width and height set:
<img src="cat.jpg" width="20" height="20" alt="Cat">
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