Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax highlighting markdown lists

I'm using Pelican along with pygments to generate syntax highlighting for my MD documents, however, i'm having trouble applying the highlighting to indented blocks.

For example:

  1. Text 1

  2. Text 2

      Code Here
    
  3. Text 3

The 10 space method works great for placing code as a sub-element of a list, however, i can't apply syntax highlighting as i normally would outside of a list like

```python

Can someone explain how i can have the code as a sub-element of the list WITH syntax highlighting?

Update: I've figured it out now. For anyone that's confused in the future here's how my code looks.

1. MD List El 1
2. MD List El 2
3. MD List El 3
(blank line)
(2 tabs):::python
        code here
(blank line)
4. MD List El 3

Doing it this way successfully generated the list numbers for me.

like image 796
asaji Avatar asked Dec 25 '22 14:12

asaji


2 Answers

Pelican's documentation states:

For Markdown, include the language identifier just above the code block, indenting both the identifier and code:

A block of text.

    :::identifier
    <code goes here>

The specified identifier (e.g. python, ruby) should be one that appears on the list of available lexers.

Of course, an extra level of indent is needed to nest that code block in a list. Markdown's indentation level is 4 spaces, so 4 spaces to nest inside a list plus 4 spaces to make it a code block = 8 spaces of indent.

Therefore this should do the trick (with spaces represented by "·" for illustrative purposes):

1. Text 1

2. Text 2

········:::python
········#Code Here

3. Text 3

You could also use tabs (represented by "→" for illustrative purposes):

1. Text 1

2. Text 2

→→:::python
→→#Code Here

3. Text 3

If you are still not getting syntax highlighting, are you sure you installed all of the dependencies? Code highlighting is done by Pygments which needs to be installed in addition to Python-Markdown.

You also need to make sure that you have created/copied Pygments compatible css files to your project and linked to them from your html templates. I'm not sure if Pelican gives you this by default or not, but without it, the highlighting won't be visible even if it is there.

like image 99
Waylan Avatar answered Jan 03 '23 00:01

Waylan


Pelican seems to use this library for its Markdown support. According to its documentation you should be able to do something like

:::python
import re
# ...

or

#!python
import re
# ...

In either case, you should get a rendered

import re
# ...

Without that first line.

If those don't work, you can try this HTML comment syntax, which is supported by a number of Markdown parsers, including the one used for Stack Overflow:

* Item 1
* Item 2

<!-- language: lang-python -->

    import re
    # ...
like image 34
Chris Avatar answered Jan 03 '23 01:01

Chris