php-school/cli-md-renderer

A CLI Markdown Render for league/commonmark compatible AST

1.0.0 2022-11-21 21:02 UTC

This package is auto-updated.

Last update: 2024-04-10 13:56:08 UTC


README

<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