扩展 PHP Markdown 以接受 CSS 类名
我是 Markdown 的忠实粉丝(几乎在我所有的 PHP 项目中都使用它),但不喜欢它有限的输出。大部分渲染都有点太简单了,并且实际上并没有让我对其布局有太多的控制或自由。例如,我可以插入一个图像:
![Alt Text](path/to/image.jpg) This is my image!
但这将简单地转换为:
<p>
<img src="path/to/image.jpg" alt="Alt Text" /> This is my image!
</p>
假设我想将图像向右浮动?我目前无法向语法中添加任何类,但这不是一个很好的功能吗?我希望能够输出:
<p>
<img src="path/to/image.jpg" alt="Alt Text" class="alignright caption">
This is my image!
</p>
支持一个或多个 CSS 类将是惊人的。我的问题是关于在 PHP 版本的 Markdown 中实现此支持的最佳方法。
有想法吗?
I'm a huge fan of Markdown (using it in nearly all of my PHP projects) but not a fan of its limited output. Much of the rendering is a bit too simple, and doesn't really allow me much control or freedom regardings its layout. For instance, I can insert an image:
![Alt Text](path/to/image.jpg) This is my image!
But that will simply be converted into:
<p>
<img src="path/to/image.jpg" alt="Alt Text" /> This is my image!
</p>
Suppose I wanted to float my image to the right? I cannot currently add any classes to the syntax, but wouldn't that be a nice feature to have? I'd like to be able to output:
<p>
<img src="path/to/image.jpg" alt="Alt Text" class="alignright caption">
This is my image!
</p>
Support for one or more CSS classes would be phenomenal. My question is regarding the best way of implementing this support into the PHP-version of Markdown.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Markdown 的核心版本没有语法来支持 CSS
class
名称、内联样式或id
属性。根据您正在做的事情,您可能会发现 Textile 是一个更好的解决方案。在某些方面它与 Markdown 相似,但在其他方面又截然不同。例如,要获得您在问题中提到的输出,您可以使用以下 Textile 字符串:这将转化为:
您还可以使用
{}
应用内联样式(恐怖的恐怖)您可以 在他们的现场演示页面上使用语法。
我在寻找 PHP 的 Textile 解析器时遇到了一些麻烦,所以你在这个答案上的里程可能没有我希望的那么多。我确实找到了这个片段,但它肯定不是全部。
Markdown has no syntax in the core version to support CSS
class
names, inline styles, orid
attributes. Depending on what you are doing, you may find Textile to be a better solution for you. In someways it is similar to Markdown, and in others it is quite different. For instance, to get the output you mentioned above in your question, you would use this Textile string:Which would translate to this:
You can also apply inline styles (horror of horrors) using
{}
You can play with the syntax on their live demo page.
I had a bit of trouble finding a Textile parser for PHP, so your mileage on this answer may not be as much as I hoped. I did find this snippet, but it surely can't be the entire thing.
没有“最佳方法”将其实现到 Markdown 中;您将不再使用 Markdown(而是拥有自己的创作)。
值得庆幸的是,Markdown 并没有像您想象的那样限制您的控制或自由。
来源:Markdown 语法文档 - 内联 HTML
There is no "best way" to implement this into Markdown; you would simply cease to be using Markdown (you instead have your own creation).
Thankfully, Markdown does not restrict your control or freedom as much as you think.
Source: Markdown Syntax Documentation - Inline HTML
查看此链接中的信息:
http://www .pingtrip.com/weblog/2008/04/adding-custom-tags-to-markdown
您可以使用该精确技术来制作将应用样式的 span 或 div “块”。它似乎在多行上工作没有问题。
在标签中包含类名会更困难,但我现在还不能说有多少。我最终会抽出时间来做这件事,如果我这样做的话,我会在这里发布结果。
我想知道为什么这么多人只说“Markdown 不适合这个”? Markdown 太有用了,不能仅限于博客和留言板评论。
编辑:
为了使其更清楚,而无需点击链接:
您可以创建自定义标签,允许您定义开始和结束字符串以及替换文本。例如:
-s %classname%
这是你的
定制风格
降价脚本
s-
这可以很容易地实现以应用特定类/id 的 span/div 块。点击链接查看“警告”和“注意”框的示例实现。
Check out the info at this link:
http://www.pingtrip.com/weblog/2008/04/adding-custom-tags-to-markdown
You could use that exact technique to make a span or div 'block' which would have the style applied. It seems to work on multiple lines no problem.
It would be more difficult to include the classname in the tag, but by how much I cannot say right now. I'm going to get around to doing it eventually, I'll post the results here if I do.
I wonder why so many people only say "Markdown isn't for that"? Markdown is much too useful to be limited to blogs and message board comments.
EDIT:
To make it more clear without following the link:
You can create custom tags that allow you to define start and end strings and the replacement text. For example:
-s %classname%
Here is your
customly styled
markdown script
s-
That could pretty easily be implemented to apply a span/div block of a certain class/id. Follow the link to see an example implementation of "Warning" and "Note" boxes.
将 markdown 与 haml 结合使用,请参阅 http://xtargets.com/ 2010/10/05/为什么你不会使用-haml/
use markdown with haml, see http://xtargets.com/2010/10/05/why-wouldnt-you-use-haml/
Php Markdown Extra 包含添加类的功能,请参阅 https://michelf.ca /projects/php-markdown/extra/#spe-attr。此功能于 2015 年 3 月添加,请参阅 https://github.com/michelf/php-markdown
Php Markdown Extra includes the ability to add classes, see https://michelf.ca/projects/php-markdown/extra/#spe-attr. This feature was added March 2015, see https://github.com/michelf/php-markdown