Discussion:
Tab size of code blocks
Igor Zhbanov
2016-10-02 10:24:54 UTC
Permalink
Hi.

Markdown allows to include code blocks by using
```c
printf("Hello World\n");
```

But it doesn't allow to specify the tab size (number of spaces). As I see,
Markdown states that the tab size is 4: "To produce a code block
in Markdown, simply indent every line of the block by at least 4 spaces
or 1 tab."

We use Markdown as a format for projects' wikis in gitlab. And we need
sometimes to include snippets of code to wiki pages. And lot of projects
use tab size of 8. It would be nice to allow to specify the tab size per
code block. For example (or with any other syntax):
```c;tab-size=8
printf("Hello World\n");
```
(Or there could be some global setting at the start of the file.)

This would allow markdown to HTML converters to set the tab-size CSS
property properly. What you think?

Thank you.
Michel Fortin
2016-10-02 11:25:28 UTC
Permalink
Post by Igor Zhbanov
Hi.
Markdown allows to include code blocks by using
```c
printf("Hello World¥n");
```
Technically this syntax is an extension that is not part of the reference Markdown spec. But it's true that many (most?) Markdown parsers support it today.
Post by Igor Zhbanov
But it doesn't allow to specify the tab size (number of spaces). As I see,
Markdown states that the tab size is 4: "To produce a code block
in Markdown, simply indent every line of the block by at least 4 spaces
or 1 tab."
You will have to check if the parser you are using has a setting like this. For instance, there is one called [`tab_width`][1] in PHP Markdown. But note that its effect will apply to everything in the document, not just code blocks, so be cautious about using tabs to indent lists and other things if you change this settings. Most likely, Gitlab uses another parser, so it it might behave differently if it has such a setting.

[1]: https://michelf.ca/projects/php-markdown/configuration/#markdown
--
Michel Fortin
https://michelf.ca
Igor Zhbanov
2016-10-02 11:31:05 UTC
Permalink
Hi Michel,
Post by Michel Fortin
Post by Igor Zhbanov
Hi.
Markdown allows to include code blocks by using
```c
printf("Hello World¥n");
```
Technically this syntax is an extension that is not part of the reference Markdown spec. But it's true that many (most?) Markdown parsers support it today.
Yes. And I'm talking about extending the markdown syntax. ;-) If it's possible.
Post by Michel Fortin
Post by Igor Zhbanov
But it doesn't allow to specify the tab size (number of spaces). As I see,
Markdown states that the tab size is 4: "To produce a code block
in Markdown, simply indent every line of the block by at least 4 spaces
or 1 tab."
You will have to check if the parser you are using has a setting like this. For instance, there is one called [`tab_width`][1] in PHP Markdown. But note that its effect will apply to everything in the document, not just code blocks, so be cautious about using tabs to indent lists and other things if you change this settings. Most likely, Gitlab uses another parser, so it it might behave differently if it has such a setting.
Gitlab uses redcarpet ruby gem to parse markdown. And it has 4 spaces
hardcoded in expand_tabs function. I've written them letter too.

I think ideally we should have both global and per-block settings.
Loading...