Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default pre code language for a whole markdown document

Usually my markdown document uses only one type of code language. For example, python is used to highlight all my code blocks.

```python
some code here.
```

```python
another piece of code
```

Since I need to type python for every piece of my code block, I would like to know if there is a way to define a default pre code highlighting language for the whole document. Then if it's defined, for example, cpp, all of the code blocks in the document will be highlighted using cpp syntax.

Thank you very much.

like image 535
richard1017 Avatar asked Mar 01 '26 05:03

richard1017


1 Answers

There is no solution that will work everywhere unfortunately, Markdown components are usually "standalone", with no overall configuration available.

Whilst there is no enforced standard of Markdown, CommonMark is the closest, and it makes no mention of a default language for code blocks:

The line with the opening code fence may optionally contain some text following the code fence; this is trimmed of leading and trailing spaces or tabs and called the info string.

...

An info string can be provided after the opening code fence. Although this spec doesn’t mandate any particular treatment of the info string, the first word is typically used to specify the language of the code block.

For example, if you're using Jekyll, this inside the _config.yml will do what you're asking:

kramdown:
  syntax_highlighter_opts:
    default_lang: python

If you're after a solution for a specific platform (or IDE etc), I'd recommend updating the question.

like image 140
Jake Lee Avatar answered Mar 03 '26 18:03

Jake Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!