simplydi/simplymarkdown

在您的网站上处理Markdown内容的一种非常简单的方式

dev-main 2023-08-04 16:58 UTC

This package is auto-updated.

Last update: 2024-09-04 19:35:23 UTC


README

在您的网站上处理Markdown内容的一种非常简单的方式

此包可用于在您的网站上渲染Markdown内容。例如,您可以在博客上使用Markdown编写博客文章,并使用SimplyMarkdown进行解析。它将返回Markdown内容的HTML以及您在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 SimplyDi\SimplyMarkdown\MdParser;

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

$parser = new MdParser(
    __DIR__ . '/md/'
);

$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

只需使用HtmlToMd()类的convert()方法即可。

$string = "<h1>Heading 1 Here</h1>";

$md = (new \SimplyDi\SimplyMarkdown\HtmlToMd)->convert($string);

echo $md;

// OUTPUT:

# Heading 1 Here