8fold/commonmark-fluent-markdown
Composer 安装命令:
composer require 8fold/commonmark-fluent-markdown
包简介
A fluent API for CommonMark by the PHP League
README 文档
README
A fluent API for use with the highly-extensible CommonMark parser from the league of extraordinary packages.
We try to put off instantiation and execution until the last possible moment.
Installation
composer require 8fold/commonmark-fluent-markdown
Usage
⚠️ Warning: Users of this library are responsible for sanitizing content.
There are two entry classes:
- Markdown: Does not follow strictly follow conventions established by the League CommonMark.
- FluentCommonMark: Tries to mirror the conventions of League CommonMark in a fluent way.
The naming convention for methods that are not part of the League CommonMark implementation follow the convention established by PSR-7.
Methods prefixed by the word with will return a new instance to facilitate immutability.
Markdown
The Markdown class makes some presumptions the FluentCommonMark class does not:
- You will be using the CommonMarkCoreExtension.
- There will always be the potential for front matter; therefore, the FrontMatterExtension will always be used to separate front matter from the body.
The Markdown class uses the the default configuration provided by CommonMark with modifications recommended by the security page of the CommonMark documentation.
The Markdown class also affords users the ability to use the 8fold CommonMark Abbreviations and 8fold CommonMark Accessible Heading Permalinks extensions whereas FluentCommonMark is strictly vanilla League CommonMark.
Write some markdown:
# Markdown
Woohoo!
Pass the markdown into the Markdown class and ask for the convertedContent:
use Eightfold\Markdown\Markdown; print Markdown::create()->convert($markdown);
Output:
<h1>Markdown</h1> <p>Woohoo!</p>
use Eightfold\Markdown\Markdown; print Markdown::create()->minified()->convert($markdown);
<h1>Markdown</h1><p>Woohoo!</p>
You can have markdown that is nothing but front matter as well.
--- title: The title ---
use Eightfold\Markdown\Markdown; print Markdown::create()->minified()->getFrontMatter($markdown);
Output:
[ 'title' => 'The title' ]
The Markdown extends the FluentCommonMark class.
FluentMarkdown
The FluentMarkdown class is designed to mimic the behavior and feel of the CommonMark library. There are additional methods in place to facilitate the fully fluent nature of this library.
Container
The Container class is a singleton that may contain one or more converter configurations.
This is useful if you find yourself instantiating multiple markdown converters:
- With each server request.
- With the same configuration and options.
By placing those converters in the Container, they only need to be instantiated once and you should see a performance increase by doing so.
Container::instance()->addConverter(
Markdown::create()->abbreviations()
)->addConverter(
FluentCommonMark::create()->descriptionLists()
);
// Returns the Markdown instance (first converter in list)
$html = Container::instance()->converter()->convert('');
// Returns HTML converted by FluentCommonMark insance
$html = Container::instance()->converter()->convertToHtml('');
Extensions
Each internal CommonMark extension is available via the fluent API along with 8fold Abbreviations:
--- title: Front matter --- ~~strikethrough from GitHub Flavored Markdown~~ An [.abbr](abbreviation) from 8fold Abbreviations.
Setting the extensions and printing the result:
use Eightfold\Markdown\Markdown; use Eightfold\Markdown\FluentCommonMark; print Markdown::create() ->minified() ->gitHubFlavoredMarkdown() ->abbreviations() ->convert($markdown); print FluentCommonMark::create() ->commonMarkCore() ->gitHubFlavoredMarkdown() ->abbreviations() ->convertToHtml($markdown) ->getContent();
The result:
<p><del>strikethrough from GitHub Flavored Markdown</del></p><p>An <abbr title="abbreviation">abbr</abbr> from 8fold Abbreviations.</p> <p><del>strikethrough from GitHub Flavored Markdown</del></p> <p>An <abbr title="abbreviation">abbr</abbr> from 8fold Abbreviations.</p>
If the extension accepts a configuration, you can pass it into the method and the primary configuration will be modified accordingly.
use Eightfold\Markdown\Markdown; print Markdown::create($markdown) ->disallowedRawHtml([ 'disallowed_tags' => ['div'] ]);
Not passing in a configuration results in using the default established by the CommonMark library.
Details
This is actually our third foray into wrapping CommonMark.
CommonMark has been a staple in 8fold web development since inception. As we've progressed and continued to slowly evolve our own XML and HTML generating packages and used those solutions in an array of websites, CommonMark has been featured front and center, as it were.
Given how much CommonMark is used in our various projects and our desire to be loosely coupled with any solutions we don't write ourselves, I think we've come to a solution that accomplishes both those missions.
Minimal code to start, configure, and render HTML. A consistent API to reduce impact as CommonMark continues to evolve.
Other
8fold/commonmark-fluent-markdown 适用场景与选型建议
8fold/commonmark-fluent-markdown 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.73k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 8fold/commonmark-fluent-markdown 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 8fold/commonmark-fluent-markdown 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-10