定制 caco/un-markdown 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

caco/un-markdown

Composer 安装命令:

composer require caco/un-markdown

包简介

A PHP library which removes markdown "formatting" from text.

README 文档

README

A simple PHP library to convert Markdown back to plain text.

The purpose of this lib is to convert markdown to plain text for e.g. chat notifications….

  • Does not loose the whole text structure as it would happen with the following call strip_tags(Parsedown::instance()->text('…'))
  • It is more performant than using a full featured markdown parser with AST support.
  • Decorate some content with prefixes e.g.
    • 🔗 for links
    • 💬 for comments
    • • for unordered list items.
  • 🏍️ Uses only regular expression for text transformation.
  • Provides a good level (Not 100% 🤷‍♂️) of compliance with GitHub Flavored Markdown Spec
  • Testdriven with over 65 unit tests 💪 and more than 370 assertions.

Usage

The basic usage is as simple as using a common markdown parser lib.

$markdownRemover = new MarkdownRemover();
echo $markdownRemover->strip('Hello **World**');

Would produce Hello World.

You can change the prefixes easily when constructing an instance.

$markdownRemover = new MarkdownRemover('"Link prefix" ', '"Image prefix️" ', '"Comment prefix" ', '');
echo $markdownRemover->strip('Wow look at this link [example.com](https://example.com/) isn't it **awesome**?');

Would produce Wow look at this link example.com "Link prefix" https://example.com/ isn't it awesome?.

You can change specific rules, remove or replace them easily.

$classUnderTest = new MarkdownRemover();
$classUnderTest
    ->getReplacements()[8]
    ->setReplace(function ($matches) {
        return ReEmphasis::toBold($matches[2]);
    });
$classUnderTest
    ->getReplacements()[9]
    ->setReplace(function ($matches) {
        return ReEmphasis::toItalic($matches[2]);
    });
$classUnderTest
    ->getReplacements()[16]
    ->setReplace(function ($matches) {
        return ReEmphasis::toMonospaced($matches[1]);
    });
echo $classUnderTest->strip('**Test** *italic* `replacement`');

Would produce 𝗧𝗲𝘀𝘁 𝘪𝘵𝘢𝘭𝘪𝘤 𝚛𝚎𝚙𝚕𝚊𝚌𝚎𝚖𝚎𝚗𝚝;

Transformation example

The following Markdown:

# Headings
Heading with `#` or as setext are supported.

Alt-H1 (Setext)
======

Alt-H2 (Setext)
------

## Emphasis, Strong emphasis & Strikethrough
Emphasis, aka italics, with *asterisks* or _underscores_.
Strong emphasis, aka bold, with **asterisks** or __underscores__.
Combined emphasis with **asterisks and _underscores_**.
Strikethrough uses two tildes. ~~Scratch this.~~

### Lists
1. Ordered lists gets
2. passed as they are
  4. As you can see the numbering 
  5. is not correct


* Unordered 
+ lists
- gets  
  + converted
  - to the bullet UTF-8 char 



- [ ] Task
- [x] List
- [ ] are
- [X] supported!

#### Links and images
[I'm an inline-style link](https://www.google.com)
[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
[I'm a reference-style link][Arbitrary case-insensitive reference text]
[I'm a relative reference to a repository file](../blob/master/LICENSE)
[You can use numbers for reference-style link definitions][1]
![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")
![alt text][logo]

##### Code
Inline `code` and block code is supported, too.

\`\`\`no-highlight
This is a code block, **MD** is ~~not~~ *interpreted*.
\`\`\`

###### Blockquotes
> Blockquotes are very handy in **email** to emulate reply text.
>> This line is part of the same quote.

###### Escaping
You can use the \\ character to escape MD. So you can escape the asterisk in strong e.g. \\\* to archive this \*\*Not strong\*\*. 

###### Thematic breaks aka <hr>
All hr gets stripped, you should not see any chars below this line:

---

***

___



[arbitrary case-insensitive reference text]: https://www.mozilla.org
[1]: http://slashdot.org
[logo]: https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 2"

Will be converted to this plain text:

Headings

Heading with # or as setext are supported.
Alt-H1 (Setext)

Alt-H2 (Setext)
Emphasis, Strong emphasis & Strikethrough

Emphasis, aka italics, with asterisks or underscores.
Strong emphasis, aka bold, with asterisks or underscores.
Combined emphasis with asterisks and underscores.
Strikethrough uses two tildes. Scratch this.
Lists

1. Ordered lists gets
2. passed as they are
  4. As you can see the numbering 
  5. is not correct

• Unordered 
• lists
• gets  
  • converted
  • to the bullet UTF-8 char 

• ⭕ Task
• ❌ List
• ⭕ are
• ❌ supported!
Links and images

I'm an inline-style link 🔗 https://www.google.com
I'm an inline-style link with title 🔗 https://www.google.com "Google's Homepage"
I'm a reference-style link 🔗 https://www.mozilla.org
I'm a relative reference to a repository file 🔗 ../blob/master/LICENSE
You can use numbers for reference-style link definitions 🔗 http://slashdot.org
🖼️ alt text
🖼️ alt text
Code

Inline code and block code is supported, too.

This is a code block, **MD** is ~~not~~ *interpreted*.

Blockquotes
💬 Blockquotes are very handy in email to emulate reply text.
💬 This line is part of the same quote.
Escaping

You can use the \ character to escape MD. So you can escape the asterisk in strong e.g. \* to archive this **Not strong**. 
Thematic breaks aka <hr>

All hr gets stripped, you should not see any chars below this line:


caco/un-markdown 适用场景与选型建议

caco/un-markdown 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 caco/un-markdown 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 caco/un-markdown 我们能提供哪些服务?
定制开发 / 二次开发

基于 caco/un-markdown 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-03