# ClassyMarkdown – adds classes to your MarkDown content

**URL:** <https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562>\
**Category:** Plugins\
**Created:** [March 11, 2016, 2:19pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562 "2016-03-11T14:19:42Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [March 11, 2016, 2:19pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/1 "2016-03-11T14:19:43Z")

</div>

I just released my first public Kirby plugin! 😃

* * *

# Classy Markdown

A [Kirby](https://getkirby.com) plugin for adding classes to the markup generated by  
the built-in Markdown parser.

## What is it good for?

When dealing with complex content, i.e. nested components inside your [Markdown](https://daringfireball.net/projects/markdown/)-formatted texts, you often face the problem of [side effects](http://philipwalton.com/articles/side-effects-in-css/). To avoid this, a lot of developers use BEM-Naming convention these days. That basically means, that every element you want to target with a CSS-selector has it’s own, unique class.

Info & Download:

> **[fabianmichael/kirby-classymarkdown](https://github.com/fabianmichael/kirby-classymarkdown)**
>
> kirby-classymarkdown - Add classes to your Markdown output.

* * *

If you’re find this useful or at least interesting, I would really appreciate your feedback on this.

---

<div class="post-metadata">

**Author:** ![SQBiz](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sqbiz/32/5014_2.png) [@SQBiz](https://forum.getkirby.com/u/SQBiz)\
**Post date:** [March 11, 2016, 8:22pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/2 "2016-03-11T20:22:42Z")

</div>

This is great! Markdown can be fantastic but it seems like I always need to come up with one workaround or another to get past its limitations. Looking forward to giving this a look!!

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [March 17, 2016, 11:09am UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/3 "2016-03-17T11:09:52Z")

</div>

Made a huge update, the plugin now covers most markdown features and has better defaults:

> **[fabianmichael/kirby-classymarkdown](https://github.com/fabianmichael/kirby-classymarkdown)**
>
> kirby-classymarkdown - Add classes to your Markdown output.

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [April 19, 2016, 12:43pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/4 "2016-04-19T12:43:39Z")

</div>

Released version `0.2.0` today. ClassyMarkdown ist now compatible with Kirby 2.3.0 (Beta).

Get in from GitHub:

> **[fabianmichael/kirby-classymarkdown](https://github.com/fabianmichael/kirby-classymarkdown)**
>
> kirby-classymarkdown - Add classes to your Markdown output.

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [April 19, 2016, 3:00pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/5 "2016-04-19T15:00:43Z")

</div>

I always “fixed” this by adding a “.kirbytext”-class to the wrapper of the kirbytext markup.

Then I could target those styles in a separate kirbytext.scss file.

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [April 19, 2016, 4:04pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/6 "2016-04-19T16:04:57Z")

</div>

I usually do it the same way as you. But for some components, this always causes trouble in my markup. Especially, when having nested components like tabs panes or slideshows, which often use `<li>` tags. In most cases, it might not be necessary to use all options of _ClassyMarkdown_. But for stuff like links with an URL as link text or footnotes (a feature of markdown extra), having the possibility of adding classes is very handy.

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [April 19, 2016, 4:22pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/7 "2016-04-19T16:22:30Z")

</div>

Components such as “tabs” or “slideshows” would require a separate kirbytag anyway, not?  
Then you can incorporate it there.

That’s how I do it anyway; not fighting to be right 🙂

---

<div class="post-metadata">

**Author:** ![abroess](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/abroess/32/1191_2.png) [@abroess](https://forum.getkirby.com/u/abroess)\
**Post date:** [November 9, 2016, 9:07pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/8 "2016-11-09T21:07:04Z")

</div>

Is there a bug, that links generated with kirbytext, does not get the link classes in kirby version 2.4.0?

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [November 9, 2016, 10:58pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/9 "2016-11-09T22:58:32Z")

</div>

Unfortunately, I haven’t tested the plugin in detail with Kirby 2.4 yet. I’ll have a look at it tomorrow.

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [November 10, 2016, 11:42am UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/10 "2016-11-10T11:42:39Z")

</div>

Okay, it’s been a while since the last update of my plugin, so I had to take a look at the source again … by default, ClassyMarkdown only transforms Markdown. If you also want it to set classes on Kirbytags, you have to override the built-in tags like [described in the docs](https://getkirby.com/docs/developer-guide/kirbytext/tags):

```php
# site/tags/link.php
kirbytext::$tags['link'] = array(
  'attr' => array(
    'text',
    'class',
    'title',
    'rel',
    'lang',
    'target',
    'popup'
  ),
  'html' => function($tag) {

    $link = url($tag->attr('link'), $tag->attr('lang'));
    $text = $tag->attr('text');

    if(empty($text)) {
      $text = $link;
    }

    if (str::isURL($text)) {
      $text = url::short($text);
    }

    $class = $tag->attr('class');
    $class .= ' markup--link';

    if (str::isURL($text)) {
      $class .= ' markup--link--url';
    }

    $class = trim($class);

    return html::a($link, $text, array(
      'rel' => $tag->attr('rel'),
      'class' => $class,
      'title' => $tag->attr('title'),
      'target' => $tag->target(),
    ));

  }
);

```

I did not implement it, because Kirby loads tags within `/site/tags/` before loading plugins. Due to this behavior, built-in tags can be overriden by a plugin, but tags defined in a plugin cannot be overridden by tags defined in `/site/tags/`. However, this could be achieved by checking the tags directory for localized versions of a tag before registering the plugin tags. I.e.: If the file `link.php` exists within the tags directory, use that tag. Otherwise, register the plugin’s version of the _link_ tag. Do you think, the plugin should to this or are there any pitfalls I did not think of?

---

<div class="post-metadata">

**Author:** ![abroess](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/abroess/32/1191_2.png) [@abroess](https://forum.getkirby.com/u/abroess)\
**Post date:** [November 10, 2016, 4:12pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/11 "2016-11-10T16:12:16Z")

</div>

Sorry, my fault. I used the kirby tag link and not the native html link syntax `[example link](http://example.com/)`. With the native variant it works like charme.

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [November 10, 2016, 4:19pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/12 "2016-11-10T16:19:31Z")

</div>

Yeah, but it might be worth adding the additional classes also to built-in Kirbytags as well, as they provide far more options. I scheduled that for the next update of the plugin. 🙂

[https://github.com/fabianmichael/kirby-classymarkdown/issues/1](https://github.com/fabianmichael/kirby-classymarkdown/issues/1)

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [May 23, 2017, 9:36pm UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/13 "2017-05-23T21:36:08Z")

</div>

Thanks for this plugin, I am gonna try it in the coming days as I began to use `functional css`.

Was wondering if there is a way to parse and filter a `->kirbytext()` output each time we use this function, and do something like:

- if `p`, add these classes
- if `ul`, add these classes

in general I think your plugin will work well, but I can imagine that sometimes I need to add custom variant only on a specific `kirbytext` output. And sure, you can create custom scoped css classes for that, but would be curious to know if kirby has a parsing and filtering function like the above anyway.

---

<div class="post-metadata">

**Author:** ![fabianmichael](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fabianmichael/32/13901_2.png) [@fabianmichael](https://forum.getkirby.com/u/fabianmichael)\
**Post date:** [May 24, 2017, 9:27am UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/14 "2017-05-24T09:27:44Z")

</div>

Hey @afincato,

unfortunately my plugin does not have this functionality, as it is not possible to pass custom parameter to the `->kirbytext()` method.

A word of caution, when adding classes to every element: I don’t want to tell you how write proper CSS, but you should consider to be careful when solving everything with class selectors. While functional CSS is very great for doing layout and grid stuff, I would really consider to use context-based selectors for Kirbytext/Markdown styling whenever possible. [Medium.com](http://Medium.com) does sth. like `<p class="graf graf--p graf-after--p">`, but the downsides of that are a bloated HTML source and also your HTML will become less portable. I always put Kirbytext/Markdown into a wrapper that has a special class, often I just name it `.text`. This selector does not contain any styling rules, but it makes it much easier to style the content within. You can also have different scopes for different kinds of content, e.g. `.text`, `.small-text` or `.text .text--small` etc. for handling these cases. Doing it that way, you HTML remains portable and you can handle a lot of the complexity in your CSS using child selectors. And with ClassyMarkdown on top, you can still distinguish Kirbytext content from nested modules like e.g. generated by a galllery or linklist Kirbytag.

Have a nice day 🕶  
Fabian

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [May 24, 2017, 9:43am UTC](https://forum.getkirby.com/t/classymarkdown-adds-classes-to-your-markdown-content/3562/15 "2017-05-24T09:43:13Z")

</div>

Really nice to hear this from you, as I was indeed considering the pros and cons of styling everything with atomic classes (I tried this approach with a python website I am working on) and I realised that for typography it gets easily messy the situation.

So for once, using scopes and child selectors would be way more of a careful approach!

😃
