dkikki / markdown
为您的网站编写和解析Markdown内容
1.0.2
2024-09-30 08:22 UTC
Requires
- php: >=8.3
- league/commonmark: ^2.5
- league/html-to-markdown: ^5.1
- nette/utils: ^4.0
- symfony/yaml: *
README
Dikki Markdown是一个用于从markdown文件中获取内容的包装器,包括前导部分。例如,您使用markdown编写博客文章。您可以使用此包将markdown文件的正文内容获取为HTML,其余的前导部分作为数组。
您还可以使用此包将HTML转换为Markdown。
基本上,它只是league/commonmark和league/html-to-markdown的包装器。.
安装
composer require dikki/markdown
用法
以下示例将进行说明。
示例1:获取markdown文章的内容:
sample-post.md:
--- title: This is a sample title description: This is a description cover_image: /path/to/image.png slug: sample-post --- This is the body content of the blog post.
在您的应用程序中使用解析器:
<?php use Dikki\Markdown\MarkdownParser; require_once '../vendor/autoload.php'; $parser = new MarkdownParser( __DIR__ . '/contents/' ); $output = $parser->getFileContent("sample-post"); echo "<pre>"; print_r($output);
输出:
Array ( [title] => This is a sample title [description] => This is a description [cover_image] => /path/to/image.png [slug] => sample-post [content] => This is the body content of the blog post. )
如您所见,您获得了作为Yaml传递到markdown文件中的元数据数组以及您编写的正文内容。
将HTML转换为Markdown
简单地使用HtmltoMdConverter()
类的convert()
方法
$string = "<h1>Heading 1 Here</h1>"; $md = (new \Dikki\Markdown\HtmlToMdConverter())->convert($string); echo $md; // OUTPUT: # Heading 1 Here