简体   繁体   中英

Have Syntax Highlighting in Markdown for Maven Site (Fluido)

I have tried the following in my Maven project:

  • Add a markdown file content.md with content
```java
int a = 4;
```

in src/main/site/markdown .

  • Write a site.xml with content

     <?xml version="1.0" encoding="ISO-8859-1"?> <project> <skin> <groupId>org.apache.maven.skins</groupId> <artifactId>maven-fluido-skin</artifactId> <version>1.7</version> </skin> <body> <menu name="Dokumentation"> <item name="Benutzerhandbuch" href="content.html" /> </menu> <menu ref="reports" /> </body> </project> 
  • Write a pom.xml with

     <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>de.continentale.testsvn</groupId> <artifactId>site-test</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> <dependencies> <dependency> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav-jackrabbit</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.maven.doxia</groupId> <artifactId>doxia-module-xhtml</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>org.apache.maven.doxia</groupId> <artifactId>doxia-module-markdown</artifactId> <version>1.8</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> 

Now I get a file content.html from mvn site . In this file, the int a = 4 is not syntax highlighted.

在此处输入图片说明

What do I need to do to get syntax highlighting?

I couldn't get it to work with Maven either but I found a workaround: Do the highlighting client-side in Javascript with highligh.js .

Download highlight.js and place it under src/site/resources/highlightjs.pack.js , as well as a CSS theme, eg src/site/resources/styles/atom-one-light.css .

In your site descriptor:

<project>
  <body>
    <head>
      <![CDATA[
      <link rel="stylesheet" href="styles/foundation.css" />
      <script src="highlight.pack.js"></script>
      <script>
          document.addEventListener('DOMContentLoaded', (event) => {
            document.querySelectorAll('pre.source').forEach((block) => {
              hljs.highlightBlock(block);
            });
          });
      </script>
      ]]>
    </head>
  </body>
</project>

Maven generate <pre class="source" /> blocks for code blocks so we need to tell that to highlight.js. Unfortunately Maven doesn't put a class name corresponding to the language (Java in your example) but highlight.js auto-detects languages and that works in most cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM