扩展 PHP Markdown 以接受 CSS 类名

发布于 2024-08-19 21:55:13 字数 593 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

み格子的夏天 2024-08-26 21:55:13

Markdown 的核心版本没有语法来支持 CSS class 名称、内联样式或 id 属性。根据您正在做的事情,您可能会发现 Textile 是一个更好的解决方案。在某些方面它与 Markdown 相似,但在其他方面又截然不同。例如,要获得您在问题中提到的输出,您可以使用以下 Textile 字符串:

(对齐标题)path/to/image.jpg(替代文本)!这是我的头像!

这将转化为:

<p>
  <img src="path/to/image.jpg" alt="Alt Text" class="alignright caption"> 
  This is my image!
</p>

您还可以使用 {} 应用内联样式(恐怖的恐怖)

您可以 在他们的现场演示页面上使用语法

我在寻找 PHP 的 Textile 解析器时遇到了一些麻烦,所以你在这个答案上的里程可能没有我希望的那么多。我确实找到了这个片段,但它肯定不是全部。

Markdown has no syntax in the core version to support CSS class names, inline styles, or id 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:

(alignright caption)path/to/image.jpg(Alt Text)! This is my image!

Which would translate to this:

<p>
  <img src="path/to/image.jpg" alt="Alt Text" class="alignright caption"> 
  This is my image!
</p>

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.

惯饮孤独 2024-08-26 21:55:13

没有“最佳方法”将其实现到 Markdown 中;您将不再使用 Markdown(而是拥有自己的创作)。

值得庆幸的是,Markdown 并没有像您想象的那样限制您的控制或自由。

Markdown 并不是 HTML 的替代品,甚至无法替代 HTML。它的语法非常小,仅对应于 HTML 标签的一个非常小的子集。我们的想法不是创建一种可以更轻松地插入 HTML 标签的语法。在我看来,HTML 标签已经很容易插入了。 Markdown 的想法是让阅读、写作和编辑散文变得容易。 HTML 是一种发布格式; Markdown 是一种书写格式。因此,Markdown 的格式化语法仅解决可以用纯文本传达的问题。

对于 Markdown 语法未涵盖的任何标记,您只需使用 HTML 本身即可。无需在其前面加前缀或对其进行分隔来表明您要从 Markdown 切换到 HTML;您只需使用标签即可。

来源: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.

Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags. The idea is not to create a syntax that makes it easier to insert HTML tags. In my opinion, HTML tags are already easy to insert. The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself. There’s no need to preface it or delimit it to indicate that you’re switching from Markdown to HTML; you just use the tags.

Source: Markdown Syntax Documentation - Inline HTML

月隐月明月朦胧 2024-08-26 21:55:13

查看此链接中的信息:

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.

夏夜暖风 2024-08-26 21:55:13

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文