定制 atk14/drink-markdown 二次开发

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

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

atk14/drink-markdown

Composer 安装命令:

composer require atk14/drink-markdown

包简介

Extended PHP Markdown parser tuned for usage in ATK14 projects

README 文档

README

Tests

Extended PHP Markdown parser tuned for usage in ATK14 projects. It's built on Michel Fortin's PHP Markdown Extra.

Originally it was developed for the project "Doctor Ink" (shortly "Drink").

Beware! It has some interconnections to the ATK14 Framework thus it can't be easily used in other projects.

Features

DrinkMarkdown extends PHP Markdown Extra to:

  • automatically converting URL and email text to clickable links
  • providing optional HTML purification
  • providing source code syntax highlighting
  • table rendering improved
  • iobjects (to be explained)

Basic Usage

$dm = new DrinkMarkdown([
  "table_class" => "table", // the CSS class for tables, default is "table table-bordered table-hover"
  "html_purification_enabled" => true, // default is true
  "temp_dir" => "/path/to/temp", // default is constant TEMP or sys_get_temp_dir()
  "iobjects_processing_enabled" => true, // insertable objects processing, default is true
  "urlize_text" => true, // reconstruct missing links to urls or emails? default is true
  "shortcodes_enabled" => true, // whether to enable or disable processing of shortcodes, default is true
  "shortcode_autowiring_enabled" => true, // Smarty shortcodes are being registered automatically
]);

$html = $dm->transform($markdown);

Usage in a ATK14 template

DrinkMarkdown package comes with two helpers usables in ATK14 templates.

If you have a trusted content:

{$text|markdown nofilter} {* or *}
{!$text|markdown}

If you have an insecure content, e.g. a comment from a user:

{$comment|safe_markdown nofilter} {* or *}
{!$comment|safe_markdown}

Shortcodes

DrinkMarkdown can be extended with extensions: so called shortcodes.

There are three types of shortcodes:

  • block shortcodes,
  • inline block shortcodes and
  • function shortcodes.

Rendering of shortcodes is either provided callback functions or by Smarty (template engine used in ATK14 framework) plugins. Block shortcodes corresponds to Smarty block plugins, function shortcodes corresponds to Smarty function plugins. The difference between block and inline block shortcodes is that block shortcodes affect whole paragraphs, but inline block shortcodes can operate inside paragraphs or sentences.

Built-in shortcodes

DrinkMarkdown contains block shortcodes for organizing text into columns.

[row]

[col]
### Column 1
This is text of the first column.
[/col]

[col]
### Column 2
This is text of the second column.
[/col]

[col]
### Column 3
This is text of the third column.
[/col]

[/row]

See a living example at http://markdown.plovarna.cz/czech/multiple-columns/

Shortcode div renders

element with given attributes. Unlike direct usage of the HTML element div, markdown syntax is being interpreted inside the shortcode.

[div class="teaser" id="id_teaser"]

## Hello World!

Welcome to this very nice place.

[/div]

Inline block shortcode span renders element with given attributes. Again, markdown syntax is being interpreted inside the shortcode.

## Hello [span class="world"]World[/span]!

Welcome to this very nice place.

Custom shortcodes

$dm = new DrinkMarkdown();

1. Callbacks

$dm->registerBlockShortcode("alert", function($columns,$params){
  $params += [
    "type" => "primary"
  ];
  return "<div class=\"alert alert-$params[type]\" role=\"alert\">$content</div>";
});

$dm->registerInlineBlockShortcode("upper", function($content,$params){ return strtoupper($content); });

$dm->registerFunctionShortcode("name", function($params){
  $params += [
    "gender" => "male"
  ];
  return $params["gender"]=="female" ? "Samantha Doe" : "John Doe";
});

2. Smarty plugins

$dm->registerBlockShortcode("alert");
$dm->registerInlineBlockShortcode("upper");
$dm->registerFunctionShortcode("name");

If no callback is specified during a shortcode registration, an appropriate Smarty plugin is required. Note the naming conventions of plugins.

<?php
// file: app/helpers/block.drink_shortcode__alert.php
function smarty_block_drink_shortcode__alert($params,$content,$template,&$repeat){
  if($repeat){ return; }

  $params += array(
    "type" => "primary"
  );

  return "<div class=\"alert alert-$params[type]\" role=\"alert\">$content</div>";
}

<?php
// file: app/helpers/block.drink_shortcode__upper.php
function smarty_block_drink_shortcode__upper($params,$content,$template,&$repeat){
  if($repeat){ return; }

  return strtoupper($content);
}

<?php
// file: app/helpers/function.drink_shortcode__name.php
function smarty_function_drink_shortcode__name($params,$template){
  $params += array(
    "gender" => "male"
  );

  return $params["gender"]=="female" ? "Samantha Doe" : "John Doe";
}

Now, everything is set and ready. The following markdown text...

## This is welcome screen!

[alert type="info"]
Welcome [upper][name gender="female"][/upper]
[/alert]

... will be rendered as:

<h2>This is welcome screen!</h2>

<div class="alert alert-info">
<p>Welcome SAMANTHA DOE!</p>
</div>

Shortcode autowiring

By default, all Smarty plugins found are registered as either function shortcodes or block shortcodes.

For example, if file app/helpers/function.drink_shortcode__current_year.php exists, the shortcode [current_year] can be used without previous registration.

If shortcode autowiring is not desired behaviour, it can be disabled by "shortcode_autowiring_enabled" option in the constructor.

Installation

Just use the Composer:

cd path/to/your/atk14/project/
composer require atk14/drink-markdown

Optionaly you can link (or copy & edit) helpers to your project.

ln -s ../../vendor/atk14/drink-markdown/src/app/helpers/block.markdown.php app/helpers/
ln -s ../../vendor/atk14/drink-markdown/src/app/helpers/block.safe_markdown.php app/helpers/
ln -s ../../vendor/atk14/drink-markdown/src/app/helpers/modifier.markdown.php app/helpers/
ln -s ../../vendor/atk14/drink-markdown/src/app/helpers/modifier.safe_markdown.php app/helpers/
ln -s ../../vendor/atk14/drink-markdown/src/app/helpers/block.drink_shortcode__row.php app/helpers/
ln -s ../../vendor/atk14/drink-markdown/src/app/helpers/block.drink_shortcode__col.php app/helpers/
mkdir -p app/views/shared/helpers/drink_shortcodes
ln -s ../../../../../vendor/atk14/drink-markdown/src/app/views/shared/helpers/drink_shortcodes/_row.tpl app/views/shared/helpers/drink_shortcodes/
ln -s ../../../../../vendor/atk14/drink-markdown/src/app/views/shared/helpers/drink_shortcodes/_col.tpl app/views/shared/helpers/drink_shortcodes/

License

DrinkMarkdown is free software distributed under the terms of the MIT license

atk14/drink-markdown 适用场景与选型建议

atk14/drink-markdown 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.8k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「markdown」 「atk14」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-02