dsh-code-highlight: Code Highlighting Plugin for Report Documents

The DSH ecosystem emphasizes that “everything is a plugin.” When writing technical reports or documentation, code blocks are often core content, but default Markdown syntax highlighting cannot directly produce vector or raster artifacts usable in Word, PPT, or PDF.

The scientificmaniac/dsh-code-highlight plugin registers a highlight_code tool. It uses a built-in lexer and renderer to convert code snippets into HTML, SVG, PNG, or LaTeX code with real syntax coloring.

Core Capabilities

This plugin mainly serves report-style documentation scenarios and provides the following capabilities:

  1. Language support: Built-in lexical definitions for 68 programming languages and configuration files. It can be specified by language name, alias, or file extension; if not specified, it is auto-detected.
  2. Multi-format output: Provides 10 output formats. These include HTML for direct embedding, SVG for vector illustrations, PNG for raster previews, and the listings and minted environments for LaTeX documents.
  3. Theme system: Built-in 7 themes, such as github-dark and paper-light (suitable for document layout).
  4. Offline capable: The tokenizer and renderer are both built in, do not depend on runtime npm packages, and can be used offline.
  5. Document layout: Supports controlling output size through parameters such as width_in (inches), dpi, and font_size_pt, ensuring generated images fit the document column width exactly.

Installation and Enablement

Before installing, make sure the DSH Desktop application has been fully quit. The installation command is:

dsh plugin --profile desktop add github:scientificmaniac/dsh-code-highlight

After installation, the highlight_code tool will be active in all sessions in the current profile. If the plugin source code is modified, DSH must be restarted to load the new modules.

Typical Usage

When calling the highlight_code tool, you must provide the code (required) and format parameters. Other parameters are optional.

Basic Rendering

{
  "action": "highlight_code",
  "code": "const a = 1;",
  "language": "javascript",
  "format": "html",
  "theme": "github-dark"
}

Generating Word/PPT Illustrations

Generate a vector image and save it directly to disk:

{
  "action": "highlight_code",
  "code": "print('Hello')",
  "language": "python",
  "format": "svg",
  "theme": "paper-light",
  "width_in": 6.3,
  "output_file": "./code_snippet.svg"
}

Generating Raster Bitmaps for PDF

{
  "action": "highlight_code",
  "code": "package main",
  "language": "go",
  "format": "png",
  "theme": "github-dark",
  "dpi": 300,
  "output_file": "./code_snippet.png"
}

LaTeX Output

Generate LaTeX source code that can be placed directly in a .tex file for compilation:

{
  "action": "highlight_code",
  "code": "return true;",
  "language": "javascript",
  "format": "latex",
  "theme": "solarized-light"
}

Rasterization and Limitations

This plugin uses the Pillow library in Python 3 for bitmap rasterization. Its core mechanism is a fixed-step grid that maps ASCII characters to monospaced font cells and maps wide characters (such as Chinese) to monospaced font cells, ensuring the layout remains stable during scaling.

Known limitations:

  1. Single snippet at a time: Only one code snippet can be processed at a time.
  2. Width limit: width_in only limits the output width; it does not stretch a short snippet to fill a page.
  3. HTML special content: In the generated HTML, content inside <script> and <style> tags is not highlighted separately.
  4. Markdown format: format: markdown outputs a colorless fenced code block; highlighting is completed by the downstream Markdown renderer.
  5. Wide character calculation: Wide characters (such as Chinese) are calculated as two cells during rasterization.
  6. Source code changes: After modifying plugin source code, DSH must be restarted for the changes to take effect.

Summary

dsh-code-highlight is a syntax highlighting tool specifically designed for document generation scenarios. It addresses the problem of inconsistent code block styling and difficulty directly embedding code blocks into Office or LaTeX documents when AI agents generate reports. With built-in support for 68 languages and offline operation, it provides reliable syntax highlighting for the automated production of technical reports.

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye