定制 blizzard-fs/md2html 二次开发

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

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

blizzard-fs/md2html

最新稳定版本:v1.1.0

Composer 安装命令:

composer require blizzard-fs/md2html

包简介

Standalone Markdown to HTML parser. Pure PHP, no dependencies. CommonMark + GFM with optional Carbon.now.sh-style code blocks.

README 文档

README

Standalone Markdown to HTML parser. Pure PHP, no dependencies.

Supports CommonMark and GitHub Flavored Markdown (GFM) with an optional Carbon.now.sh-style renderer for code blocks.

Install

Composer

composer require blizzard-fs/md2html

Manual

Clone the repo and require the entry point directly:

git clone https://github.com/Blizzard-fs/md2html.git

CLI Usage

# File to stdout
php md2html.php input.md

# File to file
php md2html.php input.md output.html

# Stdin
echo "# Hello" | php md2html.php

# With Carbon-styled code blocks
php md2html.php input.md --carbon
php md2html.php input.md --carbon output.html

# Options
php md2html.php input.md --safe --heading-ids --nofollow
php md2html.php input.md --wrap --title "My Page" --css "body{max-width:800px}"
php md2html.php input.md --carbon --wrap --heading-ids --hard-breaks

CLI Options

Flag Description
--carbon Carbon.now.sh-style code blocks
--safe Strip raw HTML blocks
--heading-ids Generate id attributes on headings
--hard-breaks Treat newlines as <br>
--nofollow Add rel="nofollow noopener" to links
--wrap Wrap output in full HTML document
--title <str> Document title (requires --wrap)
--css <str> Custom CSS (requires --wrap)
--max-nesting <n> Max block nesting depth (default 10)

PHP Usage

require_once __DIR__ . '/md2html/src/RendererInterface.php';
require_once __DIR__ . '/md2html/src/PlainHtmlRenderer.php';
require_once __DIR__ . '/md2html/src/ParseContext.php';
require_once __DIR__ . '/md2html/src/InlineParser.php';
require_once __DIR__ . '/md2html/src/BlockParser.php';
require_once __DIR__ . '/md2html/src/MarkdownParser.php';

$parser = new MarkdownParser();
$html = $parser->parse('# Hello **world**');

With Composer autoloading:

require_once __DIR__ . '/vendor/autoload.php';

use BlizzardFs\Md2Html\MarkdownParser;

$parser = new MarkdownParser();
$html = $parser->parse('# Hello **world**');

Carbon renderer

use BlizzardFs\Md2Html\CarbonHtmlRenderer;
use BlizzardFs\Md2Html\MarkdownParser;

$renderer = new CarbonHtmlRenderer();
$parser = new MarkdownParser($renderer);
$html = $parser->parse($markdown);

Code blocks render with macOS-style window chrome, dark background, and language labels. CSS is emitted once, inline.

Options

use BlizzardFs\Md2Html\MarkdownParser;
use BlizzardFs\Md2Html\ParserOptions;

$options = (new ParserOptions())
    ->setHeadingIds(true)
    ->setNoFollowLinks(true)
    ->setSafeMode(true);

$parser = new MarkdownParser(options: $options);
$html = $parser->parse($markdown);
Option Default Description
setWrapDocument(bool) false Wrap output in <!DOCTYPE html> document
setDocumentTitle(string) '' <title> for document wrapper
setDocumentCss(string) '' Custom <style> in document <head>
setSafeMode(bool) false Strip raw HTML blocks
setHeadingIds(bool) false Generate id slugs on headings
setHardBreaks(bool) false Treat newlines as <br>
setNoFollowLinks(bool) false Add rel="nofollow noopener" to links
setMaxNestingLevel(int) 10 Limit recursion depth for nested blocks

Implement OptionsInterface to provide your own options logic. Custom renderers that need access to the active options implement OptionsAwareInterface (a setOptions() method); the parser injects options into any renderer that does.

Custom renderer

Implement RendererInterface to control all HTML output:

use BlizzardFs\Md2Html\RendererInterface;
use BlizzardFs\Md2Html\MarkdownParser;

class MyRenderer implements RendererInterface
{
    // 22 methods - one per element type
    // See src/RendererInterface.php for the full contract
}

$parser = new MarkdownParser(new MyRenderer());

Supported Elements

Block-level: headings (ATX + setext), paragraphs, fenced and indented code blocks, blockquotes (nested), tables (with alignment), ordered and unordered lists (nested, loose/tight), task lists, horizontal rules, HTML passthrough, footnote sections.

Inline: bold, italic, bold-italic, strikethrough, inline code, links (inline + reference), images, autolinks, email links, footnote references, backslash escapes, line breaks.

GFM extensions: tables, strikethrough, task lists, autolinks, underscore-in-word handling (foo_bar_baz stays literal).

Architecture

MarkdownParser (facade)
  ├── OptionsInterface (configuration)
  ├── ParseContext (shared state)
  ├── InlineParser(context, renderer, options)
  ├── BlockParser(context, inlineParser, renderer, options)
  └── RendererInterface
        ├── PlainHtmlRenderer (default, options-aware)
        └── CarbonHtmlRenderer (composes PlainHtmlRenderer)

Strategy pattern with composition. CarbonHtmlRenderer delegates everything except codeBlock() to PlainHtmlRenderer - no inheritance.

Requirements

  • PHP 8.1+

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固