jerichen/laravel-convert-html-to-amp

1.5 2019-07-08 16:47 UTC

This package is auto-updated.

Last update: 2024-09-09 01:33:50 UTC


README

将文章内容从Html转换成符合amp的内容。包括

  • 图片
  • iframe
  • youtube-iframe
  • instagram-iframe
  • facebook-ifram

包的使用方式

composer require jerichen/laravel-convert-html-to-amp

或者

composer config repositories.laravel-convert-html-to-amp vcs git@github.com:jerichen/laravel-convert-html-to-amp
  • 发布迁移(如果原本有 Article 就只做这个)
php artisan vendor:publish --tag="amp-migrations" 
  • 如果没有 Article,执行以下操作(建立Article示例)
php artisan vendor:publish --tag="migrations" 
php artisan vendor:publish --tag="seeds" 
php artisan vendor:publish --tag="models" 
php artisan db:seed --class=ArticleTableSeeder
  • ArticleController 示例文件
<?php
namespace App\Http\Controllers;

use Jerichen\Amp\AmpHelper;
use App\Models\Entities\Article;
use Jerichen\Amp\app\models\AmpArticle;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    protected $article;
    protected $article_index_id_key = 'id';
    protected $article_content_key = 'content';

    public function __construct()
    {
        $this->article = new Article();
    }
    
    private function saveAmpArticle($data = [])
    {
        $amp_article = AmpArticle::updateOrCreate([
            'article_id' => $data['article_id'],
        ], [
            'content' => $data['amp_content'],
            'status' => AmpArticle::STATUS_OPEN,
        ]);
        return $amp_article;
    }

    public function amp()
    {
        $amp = new AmpHelper();
        $amp->setArticle($this->article);
        $amp->setArticleIdKey($this->article_index_id_key);
        $amp->setArticleContentKey($this->article_content_key);

        // push article_id
        $result = $amp->transferContent('{文章ID}');
        $this->saveAmpArticle($result);
    }
}
  • 返回值
Collection 
[
    'article_id' => '{文章ID}',
    'amp_content' => '{文章內容}'
]