3

I'd like to disable line numbers for just a single block of code in a markdown formatted document.

Neither scanning through the related documentation nor searching on google show me the correct incantation for this, though I'm sure it's doable.


I'm using hexo, with the markdown-it parser

blueberryfields
  • 45,910
  • 28
  • 89
  • 168
  • 1
    This depends entirely on what Markdown renderer you're using. – Jordan Running Oct 07 '15 at 01:44
  • The Markdown standard (such as it is) says nothing about line numbers for code blocks; consequently there's nothing in the syntax itself that could turn them on or off. If you're getting line numbers in your output, it's because your renderer is adding them. It may be that your renderer supports some nonstandard syntax (e.g. CSS class names) that could do what you want, but without knowing what renderer you're using its impossible to say. – Jordan Running Oct 07 '15 at 01:55

3 Answers3

6

As of the latest dev version of hexo, this is now doable with a configuration tag like so:

  {% codeblock [line_number:(true|false)] [highlight:(true|false)]%}
    code snippet
  {% endcodeblock %}
blueberryfields
  • 45,910
  • 28
  • 89
  • 168
4

As was mentioned previously this depends on what markdown/highlighter you are using.

Edit:

I haven't updated my version of Hexo so blueberryfields answer is available in more recent versions. Basically looks like there is an added optional parameter line_number.

I will keep the below answer as well because it is often useful to extend Hexo to support features not yet in mainline.

If not available:

Since you tagged the post as Hexo I'm assuming you are talking about the codeblock tag.

{% codeblock [title] [lang:language] [url] [link text] %}
code snippet
{% endcodeblock %}

The version of Hexo as of this writing uses highlight.js and is defined in:

./node_modules/hexo/lib/plugins/tag/code.js

You could extend this directly, although that can get ugly if trying to update the module. You could fork Hexo and submit a pull request with your changes.

You could also extend Hexo to do what you want.

In the root of your project you could create a javascript file for custom tags:

./scripts/tags.js

You could start off with something like:

'use strict';

/**
 * simple code
 *
 * Syntax:
 *   {% simple_codeblock %}
 */
var util = require('hexo-util');
var highlight = util.highlight;

hexo.extend.tag.register('simple_codeblock', function(args, content){
    content = highlight(content, {
      lang: '',
      caption: '',
      gutter: false,
      tab: '',
      autoDetect: true
    });
    return content;

}, {ends: true});

And extend it however you want.

Note: This requires you reference hexo-util:

npm install hexo-util --save

Usage:

{% simple_codeblock %}
// place code here
{% endsimple_codeblock %}
Community
  • 1
  • 1
Matthew Sanders
  • 4,875
  • 26
  • 45
0

As the commenters on your question have highlighted, it depends on what you are using to render your markdown source. As an example, the renderer on wordpress.com allows you to specify code with configuration parameters like this:

[code language="css" gutter="false"]
your code here
[/code]

The use of gutter=false hides the line numbers. Further details can be found in their Posting Source Code documentation. Note that other markdown renderers may not support this though.

Craig Scott
  • 9,238
  • 5
  • 56
  • 85