Git Product home page Git Product logo

commonmark-ext-lazy-image's Introduction

Extension to render lazy images in markdown

Tests

This adds support for lazy images to the league/commonmark package version ^2.0.

Install

composer require simonvomeyser/commonmark-ext-lazy-image
โš ๏ธ When you are using Version 1.0 of league\commonmark

The current version of this pacakge is only compatible with `League\CommonMark 2.0`, for `1.0` compatibility install the latest `1.0` version of this package like so:
composer require simonvomeyser/commonmark-ext-lazy-image "^v1.2.0"

You can find the old documentation here.

Example

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use SimonVomEyser\CommonMarkExtension\LazyImageExtension;

$environment = new Environment([]);
$environment->addExtension(new CommonMarkCoreExtension())
            ->addExtension(new LazyImageExtension());

$converter = new MarkdownConverter($environment);
$html = $converter->convertToHtml('![alt text](/path/to/image.jpg)');

This creates the following HTML

<img src="/path/to/image.jpg" alt="alt text" loading="lazy" />

Options

By default, only the loading="lazy" attribute is added

While this should hopefully be sufficient in the future, you can use the provided options to integrate with various lazy loading libraries.

Here is an example how to use this package with the lozad library:

//...
$environment = new Environment([]);
$environment->addExtension(new CommonMarkCoreExtension())
            ->addExtension(new LazyImageExtension());

$converter = new MarkdownConverter([
  'lazy_image' => [
      'strip_src' => true, // remove the "src" to add it later via js, optional
      'html_class' => 'lozad', // the class that should be added, optional
      'data_attribute' => 'src', // how the data attribute is named that provides the source to get picked up by js, optional
  ]
], $environment)
$html = $converter->convertToHtml('![alt text](/path/to/image.jpg)');

This creates the following HTML

<img src="" alt="alt text" loading="lazy" data-src="/path/to/image.jpg" class="lozad" />

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.