mkdocs-material: Syntax highlight not working

Description

Using this theme, syntax highlight (for Java) is not working. If I switch to mkdocs or readthedocs theme, it is working. Adding codehilite(guess_lang=false) markdown extension does not solve it.

Steps to reproduce the bug

  1. Create a new project
  2. Put a java code block in it
  3. Serve

Package versions

  • Python: 3.4.3
  • MkDocs: 0.16.1
  • Material: 1.0.3

Project configuration

site_name: Példa oldal
theme: material
pages:
    - 'Kezdőlap': index.md
extra:
  palette:
    primary: 'Light Green'
    accent: 'Lime'
  font:
    text: 'Ubuntu'
    code: 'Ubuntu Mono'
extra_css:
    - css/extra.css

System information

  • OS: Windows 7 SP1
  • Browser: Firefox 51

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 20 (10 by maintainers)

Most upvoted comments

Other than that:

It’s not that difficult to understand, if people is having problems on accomplishing that maybe we simply have a documentation problem, maybe something else that hit who do a fresh install nowaday, who knows? For sure not a shitty rtfm response from a contributor, reading docs is exactly the problem we’re having 👎

That’s not really gentleman-like. Please mind your choice of words. Remember that we’re doing all (or most) of the work here for free.

Unfortunately, the opening post was vague, so I could not answer your question until you gave more info. So I asked with an initial terse response until more information was provided. The response was not meant to infuriate or anger anyone. It saves me time in creating an elaborate response until I know exactly how the person needs to be helped.

pymdownx.highlight controls the highlighting of inlinehilite and superfences. While all code blocks are run through it, it will not guess languages unless you enable guessing: https://facelessuser.github.io/pymdown-extensions/extensions/highlight/#options. All of this is explained in the first couple of paragraphs on pymdown-extensions official docs: https://facelessuser.github.io/pymdown-extensions/extensions/highlight/#overview.

All of this is documented, but I’m open to improvements if that helps. Material does its best to mention some of this, but sometimes you may have to go to the other libraries’ official docs to get more information, but I’m sure @squidfunk is open to suggestions and even pull requests to improve its explanations and/or linkage between other docs if required.

Fenced code is not enabled in Python Markdown by default as it is not part of the original Markdown spec, so you must enable some kind of fenced plugin. To be frank, the one provided by Python Markdown is not ideal as it does not handle nesting under other Markdown constructs well.

I just tested it with a blank installation and indeed you need a fences extension like pymdownx.superfences if you want to use Pygments. I had enabled that on all installations anyway, so it wasn’t obvious. You don’t need CodeHilite:

markdown_extensions:
  - pymdownx.highlight
  - pymdownx.superfences

I went ahead and fixed the docs in 801b3dc9.

Well, no. But it isn’t stated in the linked document, that I should’ve. The other themes work without it. Material does not?

It is stated the linked document:

CodeHilite parses code blocks and wraps them in <pre> tags. If Pygments is installed, which is a generic syntax highlighter with support for over 300 languages, CodeHilite will also highlight the code block.

Material uses Pygments for syntax highlighting by default, as its rendered on the server. This ensures it works without JavaScript. Other themes may use Highlight.js or some other JavaScript-based syntax highlighter. If you have a problem with MkDocs and Pygments, please post it on the MkDocs issue tracker. If you wish to include Highlight.js, you can easily do it with extra_css: http://squidfunk.github.io/mkdocs-material/customization/#additional-javascript

EDIT: you can also use the Docker image, that comes with all extensions pre-installed: https://hub.docker.com/r/squidfunk/mkdocs-material/

Other than that:

It’s not that difficult to understand, if people is having problems on accomplishing that maybe we simply have a documentation problem, maybe something else that hit who do a fresh install nowaday, who knows? For sure not a shitty rtfm response from a contributor, reading docs is exactly the problem we’re having -1

That’s not really gentleman-like. Please mind your choice of words. Remember that we’re doing all (or most) of the work here for free.

working for free is definitely not a free pass for being an ineducated nerd. Bear in mind that everyone is a person, free beer is a common situation in FLOSS world and if a kid does not understand that and talk bad, then better he’ll be prepared to someone else that could also talk bad. Even if no money is involved try to be professional to the 360 degrees, or face the possibility someone tell you how to properly respond.

And the problem is not that your documentation was poor, not at all. It really was about the bad attitude a contributor used to respond so try not to try this route at all. I work in FLOSS world since 20+ years and that isn’t something new I can see here. But feel free to give to me and especially @pplonski some cash, since we’ve helped (him more than me) for free as of now on a crucial documentation improvement for a product you also sell support for. Bah!

I agree with @aogier - there might be a problem with docs. The config to make highlight works I found after many tries, but it should be obvious. Maybe I will do PR to the docs?

Highlighting in what, fences? You need a fence extension. Auto detect highlighting? You need to enable auto detect.

Hi, can confirm that without codehilite there’s no code highlighting on a fresh install…

config file:

site_name: blah
theme:
  name: material

markdown_extensions:
  - codehilite
  - pymdownx.highlight:
      use_pygments: true