fastvolt / markdown
一个快速、简单且直接的 PHP Markdown 转 HTML 转换器。
v0.2.1
2024-08-28 22:09 UTC
Requires
- php: ^8.1
- amphp/file: ^3.0
Requires (Dev)
- phpunit/phpunit: ^10.4
This package is not auto-updated.
Last update: 2024-09-25 23:19:07 UTC
README
PHP Markdown 解析器
A Fast, Simple and Straight-forward Markdown to HTML Converter for PHP.
安装
composer require fastvolt/markdown
使用方法
<?php use FastVolt\Helper\Markdown; $sample = " ## Hello, World "; # init Markdown object $mkd = Markdown::new(); # set markdown data to convert $mkd -> setContent( $sample ); # convert data to markdown print $mkd -> toHtml(); // <h2>Hello, World</h2>
将 Markdown 文件转换为 Html 文件
假设我们在 /assets
文件夹中创建了一个名为 sample.md
的文件,其中包含以下 markdown 内容
文件: assets/sample.md
# Topic ## Sub-topic **Author:** __vincent__
文件: index.php
<?php use FastVolt\Helper\Markdown; $file_directory = __DIR__ . '/assets/sample.md'; # init markdown object $mkd = Markdown::new(); # set markdown file $mkd -> setFile( $file_directory ); # convert to html print $mkd -> toHtml(); // output: <h1>Topic</h1> <h2> Sub-topic</h2> <b>Author:</b> <i>vincent</i>
将 Markdown 文件转换为 Html 文件
为了实现这一点,你需要创建一个文件夹来存储编译后的 markdown 文件。
➡️ 如果我们已经设置了名为
pages
和files
的目录,其中files
目录中有一个名为hello.md
的文件,让我们看看如何将hello.md
markdown 文件转换为 HTML 文件。之后,我们将结果 HTML 输出保存在pages
目录下名为hello.html
的新文件中
文件: files/hello.md
### hello
文件: index.php
use FastVolt\Helper\Markdown; # convert md file to html file $mkd = Markdown::new() # set markdown file to compile $mkd -> setFile( __DIR__ . '/files/hello.md' ) # set directory to store compiled html files $mkd -> setCompileDir( __DIR__ . '/pages/' ) # convert to html $mkd -> toHtmlFile( filename: 'hello' ); # check if markdown compile to html successfully if ($mkd) { print ("compile successful"); }
上述操作后,你将得到以下结果
文件: pages/hello.html
<h3>hello</h3>
要求
- PHP 8.1
- 这就是全部 😇。
注意
FastVolt 的 Markup 库是 Erusev 的 ParseDown 库的扩展/简化版本。Erusev's ParseDown Library.