PHP Markdown Extension for Accepting CSS Class Classes

I am a big fan of Markdown (using it in almost all my PHP projects), but not a fan of its limited release. Most of the rendering is too simple, and in fact it does not allow me to control or free my layout. For example, I can insert an image:

 ! [Alt Text] (path / to / image.jpg) This is my image! 

But it will just be converted to:

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

Suppose I wanted to put an image to the right? Currently, I cannot add any classes to the syntax, but would it not be a good function? I would 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 about the best way to implement this support in the PHP version of Markdown.

Ideas?

+6
css php markdown
source share
5 answers

Markdown does not have syntax in the main version to support CSS class names, inline styles, or id attributes. Depending on what you are doing, you may find Textile the best solution for you. In many ways, it looks like Markdown, but in others it’s completely different. For example, to get the output mentioned in your question above, you should use this Textile line:

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

To translate into 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 horror) with {}

You can play with the syntax on your live demo page.

I had a bit of trouble finding a Textile analyzer for PHP, so your mileage in this answer may not be what I hoped. I found this snippet , but it certainly cannot be the whole.

+8
source share

There is no β€œbest way” to implement this in Markdown; you would just stop using Markdown (you have your own creation instead).

Fortunately, Markdown does not limit 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, which corresponds to only a very small subset of HTML tags. The idea is not to create syntax that simplifies the insertion of HTML tags. In my opinion, HTML tags are already easily inserted. The idea behind Markdown is to make reading, writing, and editing prose easier. HTML - publication format; Markdown is a recording format. Thus, Markdowns formatting syntax resolves issues that can be transmitted as plain text.

For any markup that is not covered by Markdowns syntax, you simply use HTML. Theres no need to preface it or limit it to indicate that you are switching from Markdown to HTML; you just use tags.

Source: Markdown Syntax Documentation - Embedded HTML

+4
source share

Check out the information at this link:

http://www.pingtrip.com/weblog/2008/04/adding-custom-tags-to-markdown

You can use this exact technique to create a span or div 'block' that uses style. It seems that working on multiple lines is not a problem.

It would be harder to include the class name in the tag, but as far as I can tell right now. I am going to get around this, in the end I will post the results here if I do.

I wonder why so many people only say "Markdown is not for this"? Markdown is too useful to limit itself to blogs and commentary on posts.

EDIT:

To make it more understandable without following the link:

You can create your own tags that allow you to define start and end lines and replacement text. For example:

-s% classname%
Here is your custom made markdown script
s -

This can be easily implemented to apply the span / div block of a specific class / id. Follow the link to see an example implementation of the Warning and Note blocks.

+2
source share
+1
source share

Php Markdown Extra includes the ability to add classes, see https://michelf.ca/projects/php-markdown/extra/#spe-attr . This feature was added in March 2015, see https://github.com/michelf/php-markdown

0
source share

All Articles