parsedown README

The project's README from github.com/erusev/parsedown. As one markdown file: README.md.

Parsedown

Total Downloads Version License

Better Markdown Parser in PHP — demo

Features

Installation

Install the composer package:

composer require erusev/parsedown

Or download the latest release and include Parsedown.php

Example

$Parsedown = new Parsedown();

echo $Parsedown->text('Hello _Parsedown_!'); # prints: <p>Hello <em>Parsedown</em>!</p>

You can also parse inline markdown only:

echo $Parsedown->line('Hello _Parsedown_!'); # prints: Hello <em>Parsedown</em>!

More examples in the wiki and in this video tutorial.

Security

Parsedown is capable of escaping user-input within the HTML that it generates. Additionally Parsedown will apply sanitisation to additional scripting vectors (such as scripting link destinations) that are introduced by the markdown syntax itself.

To tell Parsedown that it is processing untrusted user-input, use the following:

$Parsedown->setSafeMode(true);

If instead, you wish to allow HTML within untrusted user-input, but still want output to be free from XSS it is recommended that you make use of a HTML sanitiser that allows HTML tags to be whitelisted, like HTML Purifier.

In both cases you should strongly consider employing defence-in-depth measures, like deploying a Content-Security-Policy (a browser security feature) so that your page is likely to be safe even if an attacker finds a vulnerability in one of the first lines of defence above.

Safe mode does not necessarily yield safe results when using extensions to Parsedown. Extensions should be evaluated on their own to determine their specific safety against XSS.

Escaping HTML

WARNING: This method is not safe from XSS!

If you wish to escape HTML in trusted input, you can use the following:

$Parsedown->setMarkupEscaped(true);

Beware that this still allows users to insert unsafe scripting vectors, ex: [xss](javascript:alert%281%29).

Questions

How does Parsedown work?

It tries to read Markdown like a human. First, it looks at the lines. It’s interested in how the lines start. This helps it recognise blocks. It knows, for example, that if a line starts with a - then perhaps it belongs to a list. Once it recognises the blocks, it continues to the content. As it reads, it watches out for special characters. This helps it recognise inline elements (or inlines).

We call this approach "line based". We believe that Parsedown is the first Markdown parser to use it. Since the release of Parsedown, other developers have used the same approach to develop other Markdown parsers in PHP and in other languages.

Is it compliant with CommonMark?

It passes most of the CommonMark tests. Most of the tests that don't pass deal with cases that are quite uncommon. Still, as CommonMark matures, compliance should improve.

Who uses it?

Laravel Framework, Bolt CMS, Grav CMS, Herbie CMS, Kirby CMS, October CMS, Pico CMS, Statamic CMS, phpDocumentor, RaspberryPi.org, Symfony Demo and more.

How can I help?

Use it, star it, share it and if you feel generous, sponsor me.

What else should I know?

I also make Nota — a notes app designed for local Markdown files.

parsedown (erusev/parsedown)

Searching Database…

15.1K ·GitHub
这是什么?Parsedown 是一个 PHP 的 Markdown 解析库:你把 Markdown 文本交给它,它会把文本转换成 HTML。
它是我在 PHP 代码里调用的库,还是可以运行的程序?它是一个库:你把它引入 PHP 项目,在代码需要把 Markdown 转成 HTML 的地方调用它——没有可以运行的界面或程序。
我在代码里怎么用它?创建一个 Parsedown 实例,调用它的 text() 方法并传入 Markdown 字符串,它会返回渲染好的 HTML。README 里展示的就是这种基本用法。
我可以放心地渲染网站访客提交的 Markdown 吗?默认情况下不安全——README 直接警告说它的默认方法无法防范 XSS。安全模式会转义 HTML,但 README 也提醒它仍然存在不安全的脚本注入风险。
实际项目里都有谁在用它?它的使用非常广泛:Laravel Framework、Bolt CMS、Grav CMS、Kirby CMS、October CMS、Pico CMS、Statamic CMS、phpDocumentor、RaspberryPi.org 和 Symfony Demo 都在用它。
它能通过 CommonMark 测试套件吗?大部分能通过:Parsedown 通过了大多数 CommonMark 测试,没通过的属于比较少见的情况。作者预计随着规范逐渐成熟,兼容性也会不断提高。
Parsedown 解析 Markdown 的方式和其他解析器有什么不同?它像人一样阅读 Markdown:先根据每一行的开头判断块级元素(比如以 - 开头的行可能属于列表),然后再关注特殊字符来识别行内元素。作者把这种方式称为“基于行”的方法,并称 Parsedown 是第一个使用这种方法的解析器,后来 PHP 及其他领域的一些解析器也纷纷效仿。
作者还做过什么?作者还开发了一个叫 Nota 的笔记应用,专为本地 Markdown 文件设计。
What are similar projects?marked, markdown-it, nom, lexer, liteparse, PHP-Parser, turndown, go
Creator
erusev
Size
1.5 MB
Stars
15.1K
Age
13y
Commits · 90 Days