Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codeFomatter

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.

Features

  • 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

Files

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

Install

npm install code-formatter

Or just copy src/index.js + src/codeFomatter.css into your project.

Usage

Node.js

const fmt = require('code-formatter');

const html = fmt.format('int x = 1;', 'java');
console.log(html);
// <code><div class='codeNewLine'>int&nbsp;<span class='keyword'>x</span>&nbsp;=&nbsp;1;</div></code>

Browser

<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>

Vue 2 Component

<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>

Vue 3 + Composition API

<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>

CSS Theme

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">

CSS Classes

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

API

format(content, lang)

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'>.

codeLineFormat(content)

Plain text → HTML lines, no syntax highlighting.

pythonFormat(content), javascriptFormat(content), htmlFormat(content), cssFormat(content)

Direct formatters per language.

escapeChar(code), reverseXMLFormat(code)

HTML entity encoding/decoding utilities.

Styling Reference

.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; }

What was cleaned up (vs codessp original)

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages