simonvomeyser/commonmark-ext-lazy-image 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

simonvomeyser/commonmark-ext-lazy-image

最新稳定版本:v2.0.3

Composer 安装命令:

composer require simonvomeyser/commonmark-ext-lazy-image

包简介

Adds support for lazy images to the phpleague/commonmark markdown parser package

README 文档

README

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->convert('![alt text](/path/to/image.jpg)');

This creates the following HTML

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

Options/Configuration

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([
    // ... other config
    '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->addExtension(new CommonMarkCoreExtension())
    ->addExtension(new LazyImageExtension());

$converter = new MarkdownConverter($environment);

$html = $converter->convert('![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" />

统计信息

  • 总下载量: 18.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-21