承接 librarianphp/parsed 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

librarianphp/parsed

最新稳定版本:1.0.1

Composer 安装命令:

composer require librarianphp/parsed

包简介

A generic content parser that supports the devto markdown format

README 文档

README

A generic content parser based on the devto post format, with front matter and liquid tag support. Parsed uses league/commonmark as base markdown parser.

Current liquid tags implemented:

  • HTML Video embed (mp4): {% video path_to_video.mp4 %}
  • HTML Audio embed (mp3): {% audio path_to_audio.mp3 %}
  • Twitter embed: {% twitter tweet_id %}
  • Youtube video embed: {% youtube video_id %}
  • GitHub File (Gists not supported atm): {% github full_path_to_repo_file %}

More to come, contributions welcome.

Installation

composer require librarianphp/parsed

Usage Examples

<?php
use Parsed\Content;
use Parsed\ContentParser;

$content = "---\n";
$content .= "title: Content Title\n";
$content .= "description: My Description\n";
$content .= "custom: custom\n";
$content .= "---\n";
$content .= "## Testing";

$article = new Content($content);
$article->parse(new ContentParser(), true);

print_r($article);
Parsed\Content Object
(
    [raw] => ---
title: Content Title
description: My Description
custom: custom
---
## Testing
    [front_matter] => Array
        (
            [title] => Content Title
            [description] => My Description
            [custom] => custom
        )

    [body_markdown] => ## Testing
    [body_html] => <h2>Testing</h2>
)

Obtaining Front Matter

There are two methods to work with the front matter: frontMatterHas and frontMatterGet:

$article = new Content($content);
$article->parse(new ContentParser(), true);

if ($article->frontMatterHas('title')) {
    return $article->frontMatterGet('title');
}

Creating Custom Liquid Tags

Liquid tags are classes that implement the CustomTagParserInterface. They need to implement a method named parse, which receives the string provided to the liquid tag when called from the markdown file. For instance, this is the full code for the video liquid tag parser class:

<?php
#src/CustomTagParser/VideoTagParser.php

namespace Parsed\CustomTagParser;

use Parsed\CustomTagParserInterface;

class VideoTagParser implements CustomTagParserInterface
{
    public function parse($tag_value, array $params = [])
    {
        return "<video controls>" .
         "<source src=\"$tag_value\" type=\"video/mp4\">" .
         "Your browser does not support the video tag." .
         "</video>";
    }
}

You'll have to include your custom tag parser class within the ContentParser:

$parser = new \Parsed\ContentParser();
$parser->addCustomTagParser('video', new VideoTagParser());

Note: The built-in tag parsers are already registered within ContentParser. These are: video, audio, twitter, youtube and github.

For instance, if you have in your markdown:

{% video /videos/test.mp4 %}

It will convert to the tag into the following code:

<video controls>
   <source src="/videos/test.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

Tests

Parsed uses Pest as testing framework. To run the tests:

./vendor/bin/pest

统计信息

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

GitHub 信息

  • Stars: 19
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固