A simple Markdown parser, short and only with the rules that I currently need. The function is regex based and it is possible to enhance your custom rules.
Yes, I know, it gives a lot of open projects that solve the same goal. However it was fun to write my custom parser, only with the rules that I need, Not to much overhead. Yes, also I mean that other packages are more solid, lot of usages, but there‘s no points to learn enough about regex and markdown. Besides I know a regex parser is not the best, fastest way, but also here, I wanted to teach me in this context.
The solution is still active in his tests, you find it here. This test of the class is also built as PWA, Progressive Web App. It was only an fun project for me to understand it on a really simple site how it works. But is important, if you see the directory test
in this repository here and wonder about so much files there are not in the context of the Markdown parser. If you will check the PWA, use it on your mobile or play with Chrome/WebInspector.
The class is simple and I use it for my own documentation, that I write in markdown. Here and there is the result a single oage to help in each day to find the right syntax, hints, background and others. You can see this on this examples:
- MarkSimple Tests
- My Git Notes
- My Markdown Notes
- My Linux command notes
- Thüringen erfahren - a one pager for a bicycle event
My class supports currently the following syntax. But Pull Request are really welcome and the solution give you the possibility to add your own rule.
-
Headers,
h1
-h6
-#
to######
before the string -
Image,
![](path/to/image.png "Alt text")
-
Strong, bold text,
strong
-**
or__
before and after the string -
Italic text,
em
- an*
or_
before and after the string -
Unordered list,
ul
-*
for each line -
Ordered list,
ol
1. , 2. ,3. to some number and.
(period ) for each line -
Inline Code,
code
- an ``` backtick before and after the code string -
Code Blocks,
pre
- -
Links,
a
-[Link Text](Link URL)
-
Horizontal line,
---
***
-
Break, new line,
<br>
fixed now or\\
befor newline, up to four. -
Strike, double tilde
~~
before and after the string. -
Blockquote, a single one, with double to four
))
,}}
,]]
,but unfortunatly not yetand>>
, though twice in the regex.
- PHP 7
Install static via download, clone the repository or use dependency management via Composer
Open a terminal, e.g. termux on android. Navigate to your web root folder. Install composer (https://getcomposer.org/download/), e.g via curl, (command :
curl -sSL https://getcomposer.org/installer | php
) Run:
composer require bueltge/marksimple
Fix some missleading ../autoload.php, ( e.g. in test folder
require_once '../../../../vendor/autoload.php';
) check php errors!
You find the solution and the tests in the repository on GitHub.
See the test directory for an example with two different usages. The test directory works as PWA, for examples see only the file index.php
.
In php-files inside
<?php .... ?>
// Example with four spaces.
require_once '../vendor/autoload.php';
use Bueltge\Marksimple\Marksimple;
$ms = new Marksimple();
print $ms->parseFile('../README.md');
// Example Github Code Block ```php.
require_once '../vendor/autoload.php';
use Bueltge\Marksimple\Marksimple;
$ms = new Marksimple();
print $ms->parseFile('../README.md');
The MarkSimple
class instance uses the psr/log
feature.
And the default Logger
is the NullLogger
it will be created when declaring MarkSimple
class instance. The more details about psr/log
, please visit this link.
$testee = new Marksimple();
echo get_class($testee->logger()); //output: Psr\Log\NullLogger
add in head.php for math formula mathjax
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
At this time you have to escape a lot the markdown syntax e.g. the _
with \\_
to get math sub.
get in the src/rule folder.
Create rule from the scratch or use one existent as template and rename it to Newrule.php.
You have to change class to Newrule top in the file (approx. line 6), change regex expression, search for markdown signs in [...]
, (approx. line 17) and write html rule resp. tags you want to get (approx line 25).
Then add your Newrule to marksimple.php line 34 to 48 approx. like
'newtagname' => Rule\Newrule::class,
Newtagname like strike or sim.
Fix variable number in contents[?] respective capture group in (...)
.
numbered,and nested lists-
- ordered list done now, break fixed
- cleaned and nestable unnumbered lists
- nestable blockquote, and
fix failure on angle brackets. Fixed with>>
>
because Mr. Bueltge restricted the>
. https://www.phpliveregex.com/p/A4f
On the way to the goal of my simple parser I use lot of tests, tries on the online Regex testers. Thanks a lot to the authors of this followed two sites, great!
- PHP Live Regex Really helpful in PHP context.
- regular expressions 101
- DebuggexBeta
- Learn Regex The Easy Way
- RegExr
Copyright (c) 2017 until now, Frank Bültge
Good news, this plugin is free for everyone! Since it's released under the MIT License you can use it free of charge on your personal or commercial website.
All feedback / bug reports / pull requests are welcome.