Multi-language code formatter with syntax highlighting — pure JavaScript, zero dependencies.
Originally extracted from the codessp online teaching platform. Supports Java, JavaScript, Python, HTML, and CSS.
- 5 languages: Java, JavaScript, Python, HTML, CSS
- Zero dependencies — works in browser and Node.js
- Clean HTML output with CSS class spans for styling
- Auto line wrapping at 80 chars with smart break points
- Block / line comments, doc comments, string literals, annotations
- Brace balance tracking for correct indentation
#br#marker for manual line breaks inside comments
codeFomatter/
├── src/
│ ├── index.js ← formatter engine
│ ├── codeFomatter.css ← VS Code dark theme
│ └── stripHtml.js ← HTML utility
├── demo/
│ ├── demo.js ← CLI demo (Node)
│ └── index.html ← in-browser demo
├── test/
│ └── test.js ← 14 test cases
├── README.md
└── package.json
npm install code-formatterOr just copy src/index.js + src/codeFomatter.css into your project.
const fmt = require('code-formatter');
const html = fmt.format('int x = 1;', 'java');
console.log(html);
// <code><div class='codeNewLine'>int <span class='keyword'>x</span> = 1;</div></code><link rel="stylesheet" href="src/codeFomatter.css">
<div id="output" style="background:#1e1e1e; padding:16px;"></div>
<script src="src/index.js"></script>
<script>
const html = codeFormator.format('console.log("hi");', 'javascript');
document.getElementById('output').innerHTML = html;
</script><template>
<div class="code-block" v-html="formattedCode"></div>
</template>
<script>
import formator from 'code-formatter';
export default {
props: {
code: { type: String, default: '' },
lang: { type: String, default: 'java' }
},
computed: {
formattedCode() {
if (!this.code) return '';
return formator.format(this.code, this.lang);
}
}
}
</script>
<style scoped>
@import '~code-formatter/src/codeFomatter.css';
.code-block {
background: #1e1e1e; padding: 16px;
border-radius: 6px; overflow-x: auto;
}
</style><template>
<div class="code-block" v-html="formattedCode"></div>
</template>
<script setup>
import { computed } from 'vue';
import formator from 'code-formatter';
const props = defineProps({
code: { type: String, default: '' },
lang: { type: String, default: 'java' }
});
const formattedCode = computed(() => {
if (!props.code) return '';
return formator.format(props.code, props.lang);
});
</script>
<style scoped>
@import '~code-formatter/src/codeFomatter.css';
.code-block {
background: #1e1e1e; padding: 16px;
border-radius: 6px; overflow-x: auto;
}
</style>src/codeFomatter.css provides a VS Code dark theme. Include it in your page:
<link rel="stylesheet" href="src/codeFomatter.css">
<!-- or via npm -->
<link rel="stylesheet" href="node_modules/code-formatter/src/codeFomatter.css">| Class | Usage | Color |
|---|---|---|
.keyword |
language keywords (if, for, class) | purple bold |
.string |
string literals | orange |
.comments |
line/block comments | green italic |
.javaDoc |
Java doc comments | green italic |
.htmlTag |
HTML/XML tags | blue |
.htmlAttr |
HTML attribute names | light blue |
.csskeyword |
CSS property names | yellow |
.pyFunction |
Python built-in functions (print, len) | yellow |
.tag |
Java annotations (@Override) | blue |
.plain |
plain text | inherit |
.codeNewLine |
each code line | block |
| Param | Type | Default | Description |
|---|---|---|---|
| content | string |
— | Raw code text |
| lang | string |
'java' |
One of: java, javascript, python, html, css |
Returns HTML wrapped in <code> → <div class='codeNewLine'>.
Plain text → HTML lines, no syntax highlighting.
Direct formatters per language.
HTML entity encoding/decoding utilities.
.keyword { color: #c7254e; font-weight: bold; }
.string { color: #2a9d8f; }
.comments { color: #6a9955; }
.javaDoc { color: #6a9955; font-style: italic; }
.javaDocComments{ color: #6a9955; }
.CStylecomments { color: #6a9955; }
.htmlTag { color: #d73a49; }
.htmlAttr { color: #7f8c8d; }
.csskeyword { color: #d73a49; }
.pyFunction { color: #6f42c1; }
.tag { color: #6f42c1; }
.colon { color: #333; }
.plain { color: inherit; }
.codeNewLine { display: block; min-height: 1.4em; }| Removed | Reason |
|---|---|
import $ from "jquery" |
Not needed in standalone |
wxKeywords (WeChat Mini Program) |
Platform-specific |
maven keywords |
Not formatting-related |
htmlNoJsformat() / constructHTMLNoJsLines() |
Redundant |
ie / document.selection |
Dead code |
readyCodeDiv() / moveCursorToEnd() |
Editor-only |
cDataTag() |
Unused no-op |
MIT