Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a good-looking notification or warning box in Github Flavoured Markdown?

I am looking for something simple like triple backticks, but that still allows other markdown syntax inside.
This is not the case for triple backticks, since it is used for code blocks:

**NOTE:**
Find the docs [here](http://example.com/).

Everything inside is taken literally, so it is not possible to use other markdown features inside, like links or bold text ...

Is there any syntax or feature in markdown that enables such notification boxes?

like image 891
David Deprost Avatar asked Nov 06 '19 19:11

David Deprost


People also ask

What GitHub Flavored Markdown is and how it is used?

GitHub Flavored Markdown, often shortened as GFM, is the dialect of Markdown that is currently supported for user content on GitHub.com and GitHub Enterprise. This formal specification, based on the CommonMark Spec, defines the syntax and semantics of this dialect. GFM is a strict superset of CommonMark.

How do you add a note in markdown?

Turn on Markdown Formatting for notes. For this, go to Tools > Options > Appearance and check "Use Markdown format in notes".

How do I highlight a markdown in GitHub?

To highlight code, write the name of the language the code is written in after the initial triple backticks.

How do I add bullets to a readme in GitHub?

You can create bullet points in an unordered list in markdown format using an asterisk “*” at the beginning of the line. Links can be inserted anywhere in the readme.md. The structure is very similar to an image file, but without the exclamation mark at the beginning of the line.


2 Answers

As Waylan mentions in his answer, Github strips script and style tags from the markdown before displaying it. This means the only possibilities for notification boxes that will render on Github are those provided by native markdown or html.

After some searching and experimenting, I discovered it is possible to (ab)use the tables syntax,
and combine it with Github emoji markdown.

For example:

  • Boxes from a single cell table:

    | :exclamation:  This is very important   |
    |-----------------------------------------|
    
    
    | :zap:        Ignore at your own risk!   |
    |-----------------------------------------|
    

    md-box__single-cell

  • Boxes from a single row table with 2 cells:

    | :memo:        | Take note of this       |
    |---------------|:------------------------|
    
    
    | :point_up:    | Remember to not forget! |
    |---------------|:------------------------|
    

    md-box__single-row

  • Boxes from a 2 row table:

    | :warning: WARNING          |
    |:---------------------------|
    | I should warn you ...      |
    
    
    | :boom: DANGER              |
    |:---------------------------|
    | Will explode when clicked! |
    

    md-box__double-row

Note that markdown tables do not allow newlines, so you should use <br /> tags if you need multiple lines inside the box. In this case it might be simpler to directly use the html <table> tag, since it doesn't have this newline restriction. It also allows getting rid of the bold styling of the table header <thead> by replacing its <th> tags with <td> tags:

<table>
  <thead>
    <tr>
      <td align="left">
        :information_source: Information
      </td>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
        <ul>
          <li>Tis not true.</li>
          <li>I won't explode.</li>
        </ul>
      </td>
    </tr>
  </tbody>
</table>

md-box__html-table

like image 159
David Deprost Avatar answered Sep 19 '22 04:09

David Deprost


Is there any syntax or feature in markdown that enables such notification boxes?

Yes, you can use raw HTML. As the original rules explain:

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.

Therefore, styling a warning box around some text is out-of-scope for Markdown as that is a publishing concern, not a writing concern. However, as the ruled continue:

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.

An as you are using Github Flavored Markdown, you get the added benefit that when formatted properly, you can still have Markdown processed within the block level raw HTML tags (so long as the parser you are using is compliant with the spec). As the spec explains, Markdown is processed within a raw HTML block after the first blank line. So simply include a blank line after the opening raw HTML tag and you won't have any issues.

<div class="warning">

**NOTE:**
Find the docs [here](http://example.com/).

</div>

Of course, you will also need to define what your warning block looks like. In the above example, I assigned the warning class to the wrapping <div> tag. Some CSS would also need to be defined within the document (in a <style> tag) to style any <div> which has the warning class.

Alternatively, if you only have one warning box in your document, you could define the styles in an inline style attribute: <div style="..."> (replacing ... with the actual CSS rules).

Warning!

A word of caution though. If you plan to have your document rendered and hosted on github.com, this will not work. While GitHub's Markdown parser will work as described above, GitHub also does some postprocessing for security reasons (as documented at github/markup). The sanitation filter they use will strip out all styles (inline or not). In that case, there is no option available. The same applies here on StackOverflow.

like image 35
Waylan Avatar answered Sep 20 '22 04:09

Waylan