The robust code cleaner for AI context generation.
clean-context strips comments and whitespace noise from code files. Unlike standard compilers, it is designed for LLM Context, meaning it prioritizes token reduction while strictly protecting strings, URLs, and Regex literals.
- Token Efficient: Drastically reduces file size for LLM prompts.
- Safety First: Uses a "Consumer Pattern" regex strategy to ensure
http://website.cominside a string isn't truncated by//. - Smart Line Removal: Automatically removes the empty whitespace left behind when a full-line comment is deleted.
- Zero Dependencies: Lightweight and fast.
# Clean a file and output to console
npx clean-context script.js
# Clean and save to new file
npx clean-context script.js -o clean.js
# Force a specific language strategy
npx clean-context raw-data.txt --lang .pyconst { clean } = require('clean-context');
// 1. Clean a file (Auto-detects extension)
const cleanCode = clean('./src/index.js');
// 2. Clean a string
const raw = `const x = 10; // comment`;
const result = clean(raw, { lang: '.js' });clean-context supports 40+ languages out of the box by mapping file extensions to robust stripping strategies.
| Language / Framework | Extensions | Comment Style |
|---|---|---|
| JavaScript / TypeScript | .js, .jsx, .ts, .tsx, .mjs, .cjs |
//, /* ... */ |
| Python | .py |
# (Triple quotes preserved) |
| Java / Kotlin / Scala | .java, .kt, .kts, .scala |
//, /* ... */ |
| C / C++ / C# | .c, .cpp, .h, .cs |
//, /* ... */ |
| Go / Rust / Swift | .go, .rs, .swift |
//, /* ... */ |
| PHP | .php |
//, /* ... */ |
| Ruby / Perl | .rb, .pl |
# |
| Shell / Bash / Zsh | .sh, .bash, .zsh |
# |
| Config & Data | .yaml, .yml, .toml, .properties, .conf |
# |
| DevOps | Dockerfile, Makefile |
# |
| Web | .html, .xml, .svg, .vue, .svelte |
<!-- ... --> |
| Styles | .css, .scss, .less, .sass |
/* ... */, // (for preprocessors) |
| SQL | .sql |
--, /* ... */ |
| Markdown | .md, .markdown |
<!-- ... --> |
If a specific extension isn't listed, you can force a strategy using the --lang flag:
- Use
.jsfor any C-style language (supports//and/* */). - Use
.pyfor any Hash-style language (supports#). - Use
.htmlfor any XML-style language (supports<!-- -->).
We take extra care not to break your code context.
| Feature | Behavior |
|---|---|
| Strings | "http://example.com" is preserved (not stripped at //). |
| Regex Literals | /^https:\/\// in JS is preserved. |
| Triple Quotes | """Docstrings""" in Python are preserved. |
| Hash Colors | color: "#ff0000" in Configs/Code is preserved. |
MIT
{ github.com/mgks }
