Skip to content

A CLI Markdown Renderer for league/commonmark compatibile AST

License

Notifications You must be signed in to change notification settings

php-school/cli-md-renderer

 
 

Repository files navigation

CLI Markdown Renderer

<img src="https://github.com/AydinHassan/cli-md-renderer/workflows/CliMdRenderer/badge.svg">

Build Status Windows Build Status Coverage Status

Usage

<?php
require_once 'vendor/autoload.php';

use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use PhpSchool\CliMdRenderer\CliRendererFactory;

$parser = new DocParser(Environment::createCommonMarkEnvironment());
$cliRenderer = (new CliRendererFactory)->__invoke();
$ast = $parser->parse(file_get_contents('path/to/file.md'));

echo $cliRenderer->renderBlock($ast);

Syntax Highlighting

FencedCode can be syntax highlighted. By default only PHP source code is Syntax Highlighted using: kadet/keylighter If you want to add syntax highlighting for other languages you should create a class which implements \AydinHassan\CliMdRenderer\SyntaxHighlighterInterface

It accepts code as a string and should return highlighted code as a string. You register your highlighter like so

<?php

use PhpSchool\CliMdRenderer\Renderer\FencedCodeRenderer;

$codeRenderer = new FencedCodeRenderer;
$codeRenderer->addSyntaxHighlighter('js', new JsSyntaxHighlighter);

If you need to do this you cannot use the factory so construction will look something like:

<?php 
require_once 'vendor/autoload.php';

use Colors\Color;
use League\CommonMark\Environment;

$environment = new Environment();
$environment->addExtension(new CliExtension());

$colors = new Color();
$colors->setForceStyle(true);

return new CliRenderer($environment, $colors);

To Do

  • Make configurable (Line Endings, colors, styles)
  • Image Renderer
  • List Renderer
  • Code Syntax Highlighting
  • Documentation

About

A CLI Markdown Renderer for league/commonmark compatibile AST

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%