massivedynamic/parkdown

v1.2.1 2022-12-12 22:35 UTC

This package is auto-updated.

Last update: 2024-09-13 02:27:50 UTC


README

– 一个简单的递归下降 Markdown 解析器,适用于 PHP(版本 >= 8.1)

Markdown is a simple markup language

规范

支持的块类型

Parkdown 当前支持以下块类型

  • 代码块 (可以指定代码块的语言)
  • 表格 (带有对齐指定)
  • 段落
  • 块引用
  • 列表 (如本例所示)
    • 也可以嵌套
  • 水平线 ---

支持的行内类型

Parkdown 当前支持以下块类型

  • 粗体文本 (**粗体**)
  • 斜体文本 (*斜体*)
  • 代码片段
  • 图片 (![替代文本](src url))
  • 链接 ([链接文本][url 或引用])

附加功能

  • 引用 ([标记]: URL)

示例

段落

A simple paragraph can contain **bold text**, `inline codeblocks` and *italic text*. We can also link [with a direct url][https://google.com] *(i.e. to google)*
or via reference to [a later defined url][massivedynamic], if we so desire.

一个简单的段落可以包含 粗体文本行内代码块斜体文本。我们还可以通过直接 URL (即到 google) 或通过引用到 稍后定义的 URL 来链接,如果我们愿意的话。

段落可以带有 idclass 属性进行注释

Paragraphs can be annotated with ids and classes {.thisIsAClass, .anotherClass, #thisIsAnID}

结果如下

段落可以通过 ids 和 classes 进行注释 {.thisIsAClass, .anotherClass, #thisIsAnID}

<p class="thisIsAClass anotherClass" id="thisIsAnID">
	Paragraphs can be annotated with ids and classes
</p>

图片

![this is an alt text](https://images.unsplash.com/photo-1571171637578-41bc2dd41cd2?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&h=300&w=1740&q=80\)

this is an alt text

水平线

---

块引用

> Only two things are infinite,
> the universe and human stupidity,
> i am not totally shure about the universe, though...
>    – Albert Einstein

只有两样东西是无限的,宇宙和人类的愚蠢,我对宇宙不完全确定,尽管如此... – 阿尔伯特·爱因斯坦

代码块


\`\`\`php
	function main(int $argc, array $argv) : int {
		echo "Hello World!";

		return 0;
	}
\`\`\`

	function main(int $argc, array $argv) : int {
		echo "Hello World!";

		return 0;
	}

表格

| Product name | Amount   | Price  |
|--------------|:--------:|-------:|
| Football     |      7   | $18,00 |
| Golfball     |      122 | $7,00  |
| Fooseball    |      355 | $1,00  |
| Puck         |      58  | $12,00 |
产品名称数量价格
足球7$18,00
高尔夫球122$7,00
桌球355$1,00
冰球58$12,00

引用

[massivedynamic]: https://massivedynamic.eu

用法

只需构造一个新的 parkdown\Parkdown 对象,并将 Markdown 源代码传递给其构造函数。然后可以通过 ::html()::tree() 成员函数检索解析后的 DOMDocument 或其 HTML 输出。

示例

	use parkdown\Parkdown;

	$source = "
		This is a **bold** word in a paragraph.
	";

	$parser = new Parkdown($source);
	$tree   = $parser->tree();

	print_r($tree);
	echo $parser->html();

测试

可以通过 composer 运行单元测试

	composer test