Skip to main content

Overview

repo2pdf uses highlight.js to automatically detect and highlight code syntax in your PDFs. This feature makes code more readable by applying colors to keywords, strings, comments, and other language elements.

How It Works

When syntax highlighting is enabled, repo2pdf:
  1. Detects the language based on file extension
  2. Applies highlight.js to parse and tokenize the code
  3. Maps HTML classes to PDF color values
  4. Renders colored text in the final PDF
From clone.ts:263-276:
let highlightedCode;
try {
  if (addHighlighting && hljs.getLanguage(extension)) {
    highlightedCode = hljs.highlight(data, {
      language: extension,
    }).value;
  } else {
    highlightedCode = hljs.highlight(data, {
      language: "plaintext",
    }).value;
  }
} catch (error) {
  highlightedCode = hljs.highlight(data, {
    language: "plaintext",
  }).value;
}

Enabling Syntax Highlighting

When running repo2pdf, you’ll be prompted to select features:
? Select the features you want to include: (Press <space> to select)
 Add line numbers
 Add highlighting
 Add page numbers
 Remove comments
 Remove empty lines
 One PDF per file
Use the spacebar to select “Add highlighting” to enable this feature.

Color Mappings

repo2pdf uses a carefully selected color palette optimized for readability in PDF format. The colors are mapped from highlight.js token types to specific hex values.
Here are all the syntax element colors used by repo2pdf (from syntax.ts:8-41):
Token TypeColorHex ValueUse Case
commentSlateGray#708090Code comments
punctuationDarkSlateGray#2F4F4FBrackets, semicolons
tagTeal#008080HTML/XML tags
attributeSeaGreen#2E8B57HTML attributes
doctagSeaGreen#2E8B57Documentation tags
keywordNavy#000080Language keywords
metaSteelBlue#4682B4Meta information
nameDarkOliveGreen#556B2FFunction/class names
selector-tagDarkCyan#008B8BCSS selectors
deletionDarkRed#8B0000Deleted code
numberOrangeRed#FF4500Numeric literals
quoteSlateBlue#6A5ACDQuoted text
selector-classDarkSlateBlue#483D8BCSS class selectors
selector-idDarkViolet#9400D3CSS ID selectors
stringDarkGreen#006400String literals
template-tagLightSlateGray#778899Template tags
typeDimGray#696969Type annotations
sectionLightSeaGreen#20B2AASection headings
titleMediumPurple#9370DBTitles
linkBlueViolet#8A2BE2Links
operatorBrown#A52A2AOperators (+, -, etc)
regexpFireBrick#B22222Regular expressions
selector-attrCadetBlue#5F9EA0Attribute selectors
selector-pseudoChartreuse#7FFF00Pseudo selectors
symbolCrimson#DC143CSymbols
template-variableDarkMagenta#8B008BTemplate variables
variableGold#FFD700Variables
literalLimeGreen#32CD32Literal values
additionForestGreen#228B22Added code
built_inGreenYellow#ADFF2FBuilt-in functions
bulletLawnGreen#7CFC00List bullets
codeGray#7F8C8DInline code

Supported Languages

repo2pdf supports all languages recognized by highlight.js, including:

JavaScript/TypeScript

.js, .jsx, .ts, .tsx

Python

.py, .pyw

Java/Kotlin

.java, .kt, .kts

C/C++

.c, .h, .cpp, .hpp

Ruby

.rb, .rake

Go

.go

Rust

.rs

PHP

.php

Swift

.swift
If a language is not recognized, repo2pdf will fall back to plaintext rendering without colors.

HTML to PDF Conversion

The conversion process works by:
  1. Parsing HTML output from highlight.js
  2. Extracting class names (e.g., hljs-keyword)
  3. Looking up colors from the color map
  4. Applying colors to PDF text segments
From syntax.ts:45-50:
const getColorFromClasses = (cls?: string) => {
  if (!cls) return undefined;
  const m = /\bhljs-([^\s"]+)/.exec(cls);
  if (!m) return undefined;
  return colorMap.get(m[1].toLowerCase());
};

Example Output

Here’s how different code elements appear in the PDF:
// This comment will be SlateGray
function greet(name: string): void {
  // 'function' and 'void' = Navy
  // 'greet' = DarkOliveGreen
  // 'name: string' type = DimGray
  const message = "Hello, " + name; // string = DarkGreen
  console.log(message); // built_in = GreenYellow
}

Tips for Better Highlighting

Use standard file extensions: Ensure your files use standard extensions (.js, .py, .java) for accurate language detection.
Combine with line numbers: Syntax highlighting works great with line numbers for maximum readability.
Very long files with complex syntax may take slightly longer to process due to highlight.js parsing.

Disabling Highlighting

If you prefer monochrome output, simply answer No when prompted:
? Would you like to add syntax highlighting? No
All code will be rendered in black text on white background.