HTML To Markdown for PHP

Library which converts HTML to Markdown for your sanity and convenience.

Requires: PHP 5.3+

Lead Developer: @colinodell

Original Author: @nickcernis

Why convert HTML to Markdown?

"What alchemy is this?" you mutter. "I can see why you'd convert Markdown to HTML," you continue, already labouring the question somewhat, "but why go the other way?"

Typically you would convert HTML to Markdown if:

You have an existing HTML document that needs to be edited by people with good taste.

You want to store new content in HTML format but edit it as Markdown.

You want to convert HTML email to plain text email.

You know a guy who's been converting HTML to Markdown for years, and now he can speak Elvish. You'd quite like to be able to speak Elvish.

You just really like Markdown.

How to use it

Require the library by issuing this command:

composer require league/html-to-markdown

Add require 'vendor/autoload.php'; to the top of your script.

Next, create a new HtmlConverter instance, passing in your valid HTML code to its convert() function:

use League\HTMLToMarkdown\HtmlConverter;

$converter = new HtmlConverter();

$html = "

Quick, to the Batpoles!

";

$markdown = $converter->convert($html);

The $markdown variable now contains the Markdown version of your HTML as a string:

echo $markdown; // ==> ### Quick, to the Batpoles!

The included demo directory contains an HTML->Markdown conversion form to try out.

Conversion options

By default, HTML To Markdown preserves HTML tags without Markdown equivalents, like and

.

To strip HTML tags that don't have a Markdown equivalent while preserving the content inside them, set strip_tags to true, like this:

$converter = new HtmlConverter(array('strip_tags' => true));

$html = 'Turnips!';

$markdown = $converter->convert($html); // $markdown now contains "Turnips!"

Or more explicitly, like this:

$converter = new HtmlConverter();

$converter->getConfig()->setOption('strip_tags', true);

$html = 'Turnips!';

$markdown = $converter->convert($html); // $markdown now contains "Turnips!"

Note that only the tags themselves are stripped, not the content they hold.

To strip tags and their content, pass a space-separated list of tags in remove_nodes, like this:

$converter = new HtmlConverter(array('remove_nodes' => 'span div'));

$html = 'Turnips!

Monkeys!

';

$markdown = $converter->convert($html); // $markdown now contains ""

Style options

Bold and italic tags are converted using the asterisk syntax by default. Change this to the underlined syntax using the bold_style and italic_style options.

$converter = new HtmlConverter();

$converter->getConfig()->setOption('italic_style', '_');

$converter->getConfig()->setOption('bold_style', '__');

$html = 'Italic and a bold';

$markdown = $converter->convert($html); // $markdown now contains "_Italic_ and a __bold__"

Limitations

Markdown Extra, MultiMarkdown and other variants aren't supported – just Markdown.

Known issues

Nested lists and lists containing multiple paragraphs aren't converted correctly.

Lists inside blockquotes aren't converted correctly.

Report your issue or request a feature here. Issues with patches or failing tests are especially welcome.

Style notes

Setext (underlined) headers are the default for H1 and H2. If you prefer the ATX style for H1 and H2 (# Header 1 and ## Header 2), set header_style to 'atx' in the options array when you instantiate the object:

$converter = new HtmlConverter(array('header_style'=>'atx'));

Headers of H3 priority and lower always use atx style.

Links and images are referenced inline. Footnote references (where image src and anchor href attributes are listed in the footnotes) are not used.

Blockquotes aren't line wrapped – it makes the converted Markdown easier to edit.

Dependencies

HTML To Markdown requires PHP's xml, lib-xml, and dom extensions, all of which are enabled by default on most distributions.

Errors such as "Fatal error: Class 'DOMDocument' not found" on distributions such as CentOS that disable PHP's xml extension can be resolved by installing php-xml.

Contributors

Many thanks to all contributors so far. Further improvements and feature suggestions are very welcome.

How it works

HTML To Markdown creates a DOMDocument from the supplied HTML, walks through the tree, and converts each node to a text node containing the equivalent markdown, starting from the most deeply nested node and working inwards towards the root node.

To-do

Support for nested lists and lists inside blockquotes.

Offer an option to preserve tags as HTML if they contain attributes that can't be represented with Markdown (e.g. style).

Trying to convert Markdown to HTML?

Use one of these great libraries:

No guarantees about the Elvish, though.

用php将markdown转html代码_HTML to Markdown - 用PHP将HTML 转成Markdown.相关推荐

  1. dz自动开起html,discuz论坛开启markdown 允许html代码嵌入js

    | | markdown是非常好用的文档语言,mermaid是最流行的流程图语言,katex是最流行的公式语言,下面来讲解如何配置和使用. 以下配置纯属个人研究,仅供参考 1. 文件配置 /home/ ...

  2. markdown居中文本 - 代码篇

    markdown居中文本 - 代码篇 1. 效果如下: @@@ 我就是下面代码的效果-- 2. 代码设置如下: <center style='color:red;'> @@@ 我就是上面效 ...

  3. markdown引入代码_给你自己的博客加个 Markdown

    今天给大家推荐一个简单易用的开源 Markdown 组件, Markdown 组件仓库地址是: https://github.com/pandao/editor.md Markdown 是什么 Mar ...

  4. java调色板代码_简易网页调色板功能调用代码_html

    简易网页调色板功能调用代码 colorSelect('色值输入框ID','显示色值的容器ID',event) 调用起来很简单,任何物品onClick就可以. 以下是html网页特效代码,点击运行按钮可 ...

  5. markdown中引用代码_如何在Markdown中引用您JavaScript源代码

    markdown中引用代码 您是否撰写有关Javascript和Typescript的技术文章? (Do you write technical articles about Javascript a ...

  6. MarkDown行内代码块怎么打出符号`

    MarkDown行内代码块怎么打出符号ˋ 1.一般行内代码块: `p` 效果:p 2.显示ˋ (1)如果用 `ˋ` 效果:会被识别为代码块的开头 (2)如果用 `\ˋ` 效果:会被识别三个点 (3)因 ...

  7. markdown+Pygments高亮代码

    markdown+Pygments高亮代码 1. 安装 $ pip install markdown $ pip install Pygments 2. 生成css $ pygmentize -S d ...

  8. Comi - 小程序 markdown 渲染和代码高亮解决方案

    写在前面 Comi 读 ['kəʊmɪ],类似中文 科米,是腾讯 Omi 团队开发的小程序代码高亮和 markdown 渲染组件.有了这个组件加持,小程序技术社区可以开始搞起来了. 体验 感谢[小程序 ...

  9. markdown 链接跳转到标题_我是如何使用 Vim 高效率写 Markdown 的

    本文仅适合于对vim有一定了解的人阅读,没有了解的人可以看看文中的视频我使用 neovim 代替 vim ,有些插件是 neovim 独占, neovim 和 vim 的区别请自行 google系统: ...

最新文章

  1. 如何看linux是32位还是64位--转
  2. Restful Service 中 DateTime 在 url 中传递
  3. python oop求三角形面积公式_Python OOP --action()函数
  4. 常用正则表达式知识点解读及判读有效数字、手机号邮箱正则表达式
  5. java3d翻转纪念相册_HTML5 3D旋转相册的实现示例
  6. rewind java_Java LongBuffer rewind()用法及代码示例
  7. 泰勒展开与找第一项系数不为1的解题策略
  8. 2021-06-25绝对定位的理解
  9. CodeForces - 837F(二分组合思维)
  10. Python 电子书下载列表
  11. 武田宣布发表ICLUSIG(R) (ponatinib)枢纽性2期PACE血液病试验的终局数据
  12. telegram bot : 自动下载youtube视频 发送到电报客户端
  13. 语音社交APP系统开发,提供纯粹的语音社交
  14. 一副眼镜一千多贵吗_央视曝光!一副眼镜上千元,出厂价却只要几十元
  15. 线性代数 【23】 概念的深入01 - Points坐标点和Vectors向量
  16. fpga初始化错误_FPGA下载程序出错
  17. Moebius中间件
  18. 移动硬盘安装win10
  19. 关于dll加载失败原因总结
  20. 为什么渗透提权这么难

热门文章

  1. Maven仓库配置环境变量
  2. matlab ellip传递函数,地震波观测系统的MATLAB仿真报告.doc
  3. vue.config.js错误代码
  4. 任正非:到底什么样的人才能当干部
  5. BroadcastReceiver
  6. JavaScript高级程序设计红宝书学习笔记第三章基本概念
  7. 拓扑排序详解 Java 模版代码实现
  8. 好家伙,又火一个。。
  9. 小儿机器人编程基础课
  10. 在Authorware中实现动态函数图像绘制的程序实现过程