librarianphp/parsed
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
librarianphp/parsed 适用场景与选型建议
librarianphp/parsed 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.07k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2021 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 librarianphp/parsed 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 librarianphp/parsed 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-10