aydin-hassan/cli-md-renderer

A CLI Markdown Render for league/commonmark compatible AST

3.0.0 2021-12-19 20:17 UTC

This package is auto-updated.

Last update: 2024-12-20 03:26:38 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 AydinHassan\CliMdRenderer\CliRendererFactory;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Parser\MarkdownParser;

$parser       = new MarkdownParser((new Environment())->addExtension(new CommonMarkCoreExtension()));
$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 AydinHassan\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 AydinHassan\CliMdRenderer\Highlighter\PhpHighlighter;
use AydinHassan\CliMdRenderer\InlineRenderer\LinkRenderer;
use AydinHassan\CliMdRenderer\Renderer\DocumentRenderer;
use AydinHassan\CliMdRenderer\Renderer\FencedCodeRenderer;
use AydinHassan\CliMdRenderer\Renderer\HeaderRenderer;
use AydinHassan\CliMdRenderer\Renderer\HorizontalRuleRenderer;
use AydinHassan\CliMdRenderer\Renderer\ParagraphRenderer;
use Colors\Color;
use Kadet\Highlighter\KeyLighter;
use League\CommonMark\Block\Element\Document;
use League\CommonMark\Block\Element\Header;
use League\CommonMark\Block\Element\HorizontalRule;
use League\CommonMark\Block\Element\Paragraph;
use League\CommonMark\Block\Element\FencedCode;
use AydinHassan\CliMdRenderer\InlineRenderer\TextRenderer;
use AydinHassan\CliMdRenderer\InlineRenderer\CodeRenderer;
use AydinHassan\CliMdRenderer\InlineRenderer\EmphasisRenderer;
use AydinHassan\CliMdRenderer\InlineRenderer\StrongRenderer;
use AydinHassan\CliMdRenderer\InlineRenderer\NewlineRenderer;
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Inline\Element\Code;
use League\CommonMark\Inline\Element\Emphasis;
use League\CommonMark\Inline\Element\Strong;
use League\CommonMark\Inline\Element\Newline;

$codeRender = new FencedCodeRenderer();
$keyLighter = new KeyLighter;
$keyLighter->init();
$codeRender->addSyntaxHighlighter('php', new PhpHighlighter($keyLighter));
$codeRender->addSyntaxHighlighter('js', new JsSyntaxHighlighter);

$blockRenderers = [
    Document::class         => new DocumentRenderer,
    Header::class           => new HeaderRenderer,
    HorizontalRule::class   => new HorizontalRuleRenderer,
    Paragraph::class        => new ParagraphRenderer,
    FencedCode::class       => $codeRender,
];

$inlineBlockRenderers = [
    Text::class             => new TextRenderer,
    Code::class             => new CodeRenderer,
    Emphasis::class         => new EmphasisRenderer,
    Strong::class           => new StrongRenderer,
    Newline::class          => new NewlineRenderer,
    Link::class             => new LinkRenderer,
];

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

return new CliRenderer($blockRenderers, $inlineBlockRenderers, $colors);

To Do

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