DEV Community

Tomoyuki Aota
Tomoyuki Aota

Posted on • Edited on

5 3 1 1 1

[Markdown] Short notation for an image with a link using reference-style link

(A Japanese translation is available here.)

Introduction

In Markdown, when we want to put an image with a link, the notation tends to be lengthy.

For example, the status badge for Travis CI will be like this:

Build Status

[![Build Status](https://travis-ci.org/travis-ci/travis-web.svg?branch=master)](https://travis-ci.org/travis-ci/travis-web)

Enter fullscreen mode Exit fullscreen mode

(This badge is an example from Embedding Status Images on Travis CI.)

In a table, such lengthy notation will be problematic, like this:

OS Status
Ubuntu Build Status
| OS     | Status |
|:------:|:------:|
| Ubuntu | [![Build Status](https://travis-ci.org/travis-ci/travis-web.svg?branch=master)](https://travis-ci.org/travis-ci/travis-web) |
Enter fullscreen mode Exit fullscreen mode

The table above has just one badge. When we want to put several badges in a row, the notation becomes so complex that it is difficult to recognize the column separator |. For example, README.md of srz-zumix/iutest has a table with many badges whose raw content looks very complicated.

Can we get shorter notation somehow?

Solution

Reference-style link is useful. For the table, it will be like this:

| OS     | Status |
|:------:|:------:|
| Ubuntu | [![travisci-image]][travisci-link] |
Enter fullscreen mode Exit fullscreen mode

travisci-image and travisci-link also needs be defined in the document, like this:

[travisci-image]: https://travis-ci.org/travis-ci/travis-web.svg?branch=master
Enter fullscreen mode Exit fullscreen mode
[travisci-link]: https://travis-ci.org/travis-ci/travis-web
Enter fullscreen mode Exit fullscreen mode

With this short notation for the badge, the notation of the table will be kept managable even when we put several badges in a row.

travisci-image and travisci-link are arbitrary strings, so we can do whatever we want, like giving them comprehensible names, adjusting length of the names so that the column separator | aligns across rows, etc.

Lastly, I created an example GitHub Gist. Click "Raw" button to see that this notation actually works!

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
sergo profile image
Sergo

Yeah, I like to put images in this way. Thanks!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay