GithubHelp home page GithubHelp logo

spatie / commonmark-highlighter Goto Github PK

View Code? Open in Web Editor NEW
133.0 4.0 9.0 58 KB

Highlight code blocks with league/commonmark

Home Page: https://spatie.be/open-source

License: MIT License

PHP 100.00%
commonmark highlight

commonmark-highlighter's Introduction

Highlight your markdown code blocks with league/commonmark

Latest Version Build Status Quality Score StyleCI Total Downloads

A block renderer for league/commonmark to highlight code blocks using scrivo/highlight.php.

highlight.php is a server side code highlighter written in PHP that currently supports 185 languages. It's a port of highlight.js by Ivan Sagalaev that makes full use of the language and style definitions of the original JavaScript project.

The output html is compatible with highlight.js themes, which you can explore on highlightjs.org.

What are the benefits of using this package over highlight.js?

  • Less JavaScript, which means faster page loads
  • No more flash of unstyled code blocks

This project was inspired by sixlive/parsedown-highlight.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/commonmark-highlighter

Usage

Create a custom CommonMark environment, and register the FencedCodeRenderer and IndentedCodeRender as described in the league/commonmark documentation.

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
use League\CommonMark\MarkdownConverter;
use Spatie\CommonMarkHighlighter\FencedCodeRenderer;
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer;

$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addRenderer(FencedCode::class, new FencedCodeRenderer());
$environment->addRenderer(IndentedCode::class, new IndentedCodeRenderer());

$markdownConverter = new MarkdownConverter($environment);

echo $markdownConverter->convertToHtml($markdown);

The underlying highlight library recommends specifying a subset of languages for the auto-detection. You can pass an array of languages to any of the renderers.

new FencedCodeRenderer(['html', 'php', 'js']);

new IndentedCodeRenderer(['html', 'php', 'js']);

Highlighting specific lines

Line numbers start at 1.

```php - Don't highlight any lines
```php{4} - Highlight just line 4
```php{4-6} - Highlight the range of lines from 4 to 6 (inclusive)
```php{1,5} - Highlight just lines 1 and 5 on their own
```php{1-3,5} - Highlight 1 through 3 and then 5 on its own
```php{5,7,2-3} - The order of lines don't matter
However, specifying 3-2 will not work.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

License

The MIT License (MIT). Please see License File for more information.

commonmark-highlighter's People

Contributors

adrianmrn avatar alexanderglueck avatar allejo avatar boast avatar freekmurze avatar imliam avatar jdlien avatar jonnybarnes avatar owenvoke avatar rubenvanassche avatar sebastiandedeyne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

commonmark-highlighter's Issues

highlighting script tag

hi there,
I don't know if it's package related, but any idea why the "script" tag isn't highlighted correctly?

Screenshot 2021-05-25 at 17 01 27

The code for this (picture):
Screenshot 2021-05-25 at 17 06 20

I save the textarea directly to the db from SimpleMde with markdown data, and on frontend I use github.css style for this example, and use {!! !!} tags to render.

With php I don't have any problem, fully colorize the opening tag.

Highlight line in combination with emojis

If you try to highlight a specific line in combination with an emoji, the emoji will not be shown anymore:

    ```php{2}
    // ✅ ...
    $isUserPending = $user->isStatus('pending');
    ```

Screen Shot 2020-08-26 at 20 14 51

Without the {2} the emoji is shown correctly:

    ```php
    // ✅ ...
    $isUserPending = $user->isStatus('pending');
    ```

image

Have you an idea what this is about @allejo?

PS: Here some info about how I set up the converter:

        $environment = Environment::createCommonMarkEnvironment();
        $languages = ['html', 'php', 'js', 'shell', 'shell'];

        $environment->addBlockRenderer(FencedCode::class, new FencedCodeRenderer($languages));
        $environment->addBlockRenderer(IndentedCode::class, new IndentedCodeRenderer($languages));

        $commonMarkConverter = new CommonMarkConverter([], $environment);

        return $commonMarkConverter->convertToHtml($codeExample);

Add .hljs class to all blocks

Hi
Is it possible to add .hljs class even to blocks with unsupported languages. Currently, unsupported languages (like blade, vue ..etc) display without a background when applying a theme. This will confuse the reader as they appear just like a regular text.

This is without apply the class:
image

This is with the class applied:
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.