承接 nks-hub/nette-markdown 相关项目开发

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

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

nks-hub/nette-markdown

Composer 安装命令:

composer require nks-hub/nette-markdown

包简介

Nette DI extension wrapping league/commonmark with Latte filter support

README 文档

README

Build Status Packagist Version Packagist Downloads License: MIT PHP Tests

Nette Framework DI extension wrapping league/commonmark with GitHub Flavored Markdown support, Latte filter integration, and configurable extension system.

Why nette-markdown?

Nette Framework lacks a first-class Markdown integration. While league/commonmark is an excellent CommonMark parser, wiring it into Nette's DI container, configuring extensions, and registering Latte filters requires boilerplate code in every project.

This package provides a zero-config drop-in that just works:

  • Nette DI Extension — Register in config.neon, get MarkdownConverter autowired everywhere
  • Latte Filter — Use {$content|markdown} in any template, automatically registered
  • GFM by Default — Tables, task lists, strikethrough, autolinks out of the box
  • Configurable — Enable/disable extensions, set security policies, add heading permalinks
  • Safe by Default — HTML input stripped, unsafe links blocked

Quick Start

Installation

composer require nks-hub/nette-markdown

Register in Nette DI

# config/common.neon
extensions:
    markdown: NksHub\Markdown\DI\MarkdownExtension

That's it. MarkdownConverter is now available for injection, and the |markdown Latte filter is registered automatically.

Usage

In Presenters / Services (Dependency Injection)

use NksHub\Markdown\MarkdownConverter;

class WikiPresenter extends Nette\Application\UI\Presenter
{
    public function __construct(
        private MarkdownConverter $markdown,
    ) {}

    public function renderView(string $slug): void
    {
        $page = $this->pages->findBySlug($slug);
        $this->template->htmlContent = $this->markdown->convert($page->getContent());
    }
}

In Latte Templates

{* Filter syntax *}
{$page->getContent()|markdown|noescape}

{* Or in a block *}
{var $html = ($page->getContent()|markdown)}
{$html|noescape}

Standalone (Without Nette DI)

use NksHub\Markdown\MarkdownConverter;

$converter = new MarkdownConverter();
$html = $converter->convert('# Hello **World**');
// <h1>Hello <strong>World</strong></h1>

// Safe mode — strips all HTML, blocks unsafe links
$safeHtml = $converter->convertSafe($userInput);

Features

Feature Description
CommonMark Full CommonMark spec compliance via league/commonmark 2.x
GFM Tables Pipe-delimited tables with alignment
Task Lists - [x] Done / - [ ] Todo checkbox rendering
Strikethrough ~~deleted~~ to <del>deleted</del>
Autolinks URLs automatically converted to clickable links
Footnotes [^1] reference-style footnotes (opt-in)
Smart Punctuation Curly quotes, em-dashes, ellipsis (opt-in)
Heading Permalinks Anchor links on headings for deep linking (opt-in)
Table of Contents Auto-generated TOC from headings (opt-in)
Fenced Code Blocks ```lang syntax with language class output
XSS Protection HTML stripped, unsafe links (javascript:) blocked by default
Latte Integration |markdown filter auto-registered on all Latte engines

Configuration

Default (Zero Config)

extensions:
    markdown: NksHub\Markdown\DI\MarkdownExtension

Defaults: CommonMark + GFM (tables, task lists, strikethrough, autolinks), HTML stripped, unsafe links blocked.

Full Configuration

extensions:
    markdown: NksHub\Markdown\DI\MarkdownExtension

markdown:
    # Extensions to enable (default: commonmark + gfm)
    extensions:
        - commonmark       # Core CommonMark spec (always needed)
        - gfm              # Tables + Strikethrough + Task Lists + Autolinks
        - footnote         # Reference-style footnotes
        - smartpunct       # Curly quotes, em-dashes, ellipsis
        - heading_permalink # Anchor links on headings
        - toc              # Auto-generated Table of Contents

    # HTML input handling: strip (default), allow, escape
    html_input: strip

    # Block javascript:, vbscript:, data: links (default: false = blocked)
    allow_unsafe_links: false

    # Heading permalink symbol (only when heading_permalink extension enabled)
    heading_permalink:
        symbol: '#'

Available Extensions

Key Extension Description
commonmark CommonMarkCoreExtension Core CommonMark spec — always include this
gfm Tables + Strikethrough + TaskList + Autolink GitHub Flavored Markdown bundle
footnote FootnoteExtension [^1] reference-style footnotes
smartpunct SmartPunctExtension Smart quotes, dashes, ellipsis
heading_permalink HeadingPermalinkExtension Clickable anchor links on headings
toc TableOfContentsExtension Auto-generated TOC from headings

API Reference

MarkdownConverter

use NksHub\Markdown\MarkdownConverter;

// Default: CommonMark + GFM, HTML stripped, unsafe links blocked
$converter = new MarkdownConverter();

// Custom config
$converter = new MarkdownConverter([
    'extensions' => ['commonmark', 'gfm', 'footnote'],
    'html_input' => 'allow',
    'allow_unsafe_links' => false,
]);

// Convert Markdown to HTML
$html = $converter->convert($markdown);

// Safe conversion (always strips HTML + blocks unsafe links, regardless of config)
$safeHtml = $converter->convertSafe($untrustedInput);

MarkdownFilter (Latte)

Automatically registered when using the DI extension. Callable as a Latte filter:

{$content|markdown|noescape}

Returns Latte\Runtime\Html instance, so |noescape is required to render the HTML.

MarkdownExtension (Nette DI)

Registers two services:

  • NksHub\Markdown\MarkdownConverter — injectable converter
  • NksHub\Markdown\Latte\MarkdownFilter — auto-registered on all Latte engines

Security

The converter is safe by default:

Setting Default Effect
html_input strip Removes all raw HTML from Markdown input
allow_unsafe_links false Blocks javascript:, vbscript:, data: URLs

For user-generated content, use convertSafe() which enforces both settings regardless of constructor config.

Never use html_input: allow with untrusted input — it allows raw HTML injection including <script> tags.

Development

# Install dependencies
composer install

# Run tests (PHPUnit 10)
vendor/bin/phpunit

# Run with coverage
vendor/bin/phpunit --coverage-html coverage/

# Static analysis
vendor/bin/phpstan analyse

Requirements

  • PHP: 8.2+
  • league/commonmark: ^2.8
  • nette/di: ^3.1
  • latte/latte: ^3.0

Contributing

Contributions are welcome! For major changes, please open an issue first.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: description')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Support

License

MIT License — see LICENSE for details.

Links

Made with ❤️ by NKS Hub

nks-hub/nette-markdown 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-12