lite-hl 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -28,8 +28,9 @@ To maintain its ultra-light footprint, `lite-hl` makes specific engineering trad
28
28
 
29
29
  * **Ultra-Lightweight**: At under 10KB, it is a fraction of the size of `highlight.js`.
30
30
  * **Zero-Config Coverage**: Supports every programming language automatically. There is no need to "register" or import language definitions.
31
+ * **Deep Shell Script Support**: Robust parsing of Bash/Shell conventions, recognizing 60+ common utilities (`grep`, `awk`, `docker`, etc.) and dynamic variables (`$VAR`, `${VAR}`).
31
32
  * **High Performance**: Tokenization happens in a single regex pass. It is optimized for large-scale documentation generators and real-time previews.
32
- * **Legacy Compatibility**: natively supports a `mimicHljs` mode, allowing you to use existing `highlight.js` CSS themes (like GitHub, Atom, or Monokai) as a drop-in replacement.
33
+ * **Legacy Compatibility**: natively supports a `mimicHljs` mode, allowing you to use existing `highlight.js` CSS themes (like GitHub, Atom, or Monokai) exactly as they are without modification.
33
34
  * **Runtime Agnostic**: Works perfectly in Node.js, the browser, and Edge environments.
34
35
 
35
36
  ## Installation
package/dist/index.js CHANGED
@@ -19,7 +19,8 @@ const COMMON_KEYWORDS = [
19
19
  'void', 'null', 'undefined', 'true', 'false', 'def', 'pass', 'None', 'True', 'False',
20
20
  'match', 'with', 'as', 'struct', 'func', 'go', 'chan', 'defer', 'select', 'fallthrough',
21
21
  'namespace', 'using', 'pkg', 'mod', 'require', 'fn', 'pub', 'mut', 'impl', 'loop', 'unsafe',
22
- 'trait', 'where', 'macro_rules', 'use', 'int', 'float', 'double', 'char', 'bool'
22
+ 'trait', 'where', 'macro_rules', 'use', 'int', 'float', 'double', 'char', 'bool',
23
+ 'bash', 'sh', 'sudo', 'grep', 'sed', 'awk', 'ls', 'cd', 'cp', 'mv', 'rm', 'mkdir', 'chmod', 'chown', 'git', 'pnpm', 'npm', 'yarn', 'node', 'curl', 'wget', 'echo', 'printf', 'cat', 'grep', 'find', 'xargs', 'alias', 'export', 'set', 'unset', 'read', 'source', 'type', 'which', 'whoami', 'id', 'groups', 'ps', 'top', 'kill', 'df', 'du', 'free', 'uname', 'hostname', 'ip', 'ping', 'ssh', 'scp', 'rsync', 'tar', 'gzip', 'gunzip', 'zip', 'unzip', 'sudo', 'systemctl', 'journalctl', 'docker', 'kubectl', 'apt', 'yum', 'brew', 'cargo'
23
24
  ].join('|');
24
25
  // We use named capture groups so we can easily map matches back to their token types.
25
26
  // Order is critical: comments first, then strings, then numbers, then keywords, etc.
@@ -27,6 +28,7 @@ const UNIVERSAL_REGEX = new RegExp(`(?<comment>\\/\\/[^\\n]*|\\/\\*[\\s\\S]*?\\*
27
28
  `|(?<string>"(?:\\\\.|[^"\\\\])*"|'(?:\\\\.|[^'\\\\])*'|\`(?:\\\\.|[^\`\\\\])*\`)` +
28
29
  `|(?<number>\\b\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?\\b|\\b0x[a-fA-F0-9]+\\b)` +
29
30
  `|(?<keyword>\\b(?:${COMMON_KEYWORDS})\\b)` +
31
+ `|(?<variable>\\$[a-zA-Z0-9_$]+|\\$\\{[a-zA-Z0-9_$]+\\})` +
30
32
  `|(?<function>[a-zA-Z_$][a-zA-Z0-9_$]*(?=\\s*\\())` +
31
33
  `|(?<property>(?<=\\.)[a-zA-Z_$][a-zA-Z0-9_$]*)` +
32
34
  `|(?<operator>[=+\\-*\\/%&|<>!^~?:]+)`, 'g');
@@ -54,8 +56,16 @@ export function highlight(code, options = {}) {
54
56
  // Process Token Class
55
57
  let className = tokenType;
56
58
  if (isHljs) {
59
+ // Remap token types to match highlight.js CSS class names
57
60
  if (tokenType === 'function')
58
61
  className = 'title function_';
62
+ else if (tokenType === 'property')
63
+ className = 'attr';
64
+ else if (tokenType === 'operator')
65
+ className = 'punctuation';
66
+ else if (tokenType === 'variable')
67
+ className = 'variable';
68
+ // 'comment', 'string', 'number', 'keyword' stay as-is (already match hljs names)
59
69
  className = `hljs-${className}`;
60
70
  }
61
71
  result += `<span class="${className}">${escapeHtml(text)}</span>`;
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "lite-hl",
3
- "version": "0.0.1",
3
+ "version": "0.1.1",
4
4
  "description": "A universal, zero-config, heuristic code tokenizer and highlighter.",
5
5
  "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "main": "dist/index.js",
7
10
  "types": "dist/index.d.ts",
8
11
  "scripts": {
@@ -32,11 +35,11 @@
32
35
  },
33
36
  "repository": {
34
37
  "type": "git",
35
- "url": "git+https://github.com/docmd-io/docmd.git",
38
+ "url": "git+https://github.com/docmd-io/lite-hl.git",
36
39
  "directory": "lite-hl"
37
40
  },
38
41
  "bugs": {
39
- "url": "https://github.com/docmd-io/docmd/issues"
42
+ "url": "https://github.com/docmd-io/lite-hl/issues"
40
43
  },
41
44
  "homepage": "https://docmd.io",
42
45
  "funding": "https://github.com/sponsors/mgks",
package/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
@@ -1,32 +0,0 @@
1
- name: Publish to NPM
2
- on:
3
- release:
4
- types: [published]
5
- workflow_dispatch:
6
-
7
- permissions:
8
- contents: read
9
- id-token: write
10
-
11
- jobs:
12
- publish:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - uses: actions/checkout@v6
16
-
17
- - name: Setup Node.js
18
- uses: actions/setup-node@v6
19
- with:
20
- node-version: '22'
21
- registry-url: 'https://registry.npmjs.org'
22
-
23
- - name: Install dependencies
24
- run: npm install
25
-
26
- - name: Build package
27
- run: npm run build
28
-
29
- - name: Publish to npm
30
- run: npm publish --access public
31
- env:
32
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/src/index.ts DELETED
@@ -1,101 +0,0 @@
1
- /**
2
- * lite-hl: Universal Heuristic Syntax Highlighter
3
- * Tokenizes generic programming code without relying on language-specific grammars.
4
- */
5
-
6
- // Escape HTML securely
7
- function escapeHtml(str: string): string {
8
- return str
9
- .replace(/&/g, '&amp;')
10
- .replace(/</g, '&lt;')
11
- .replace(/>/g, '&gt;')
12
- .replace(/"/g, '&quot;')
13
- .replace(/'/g, '&#039;');
14
- }
15
-
16
- const COMMON_KEYWORDS = [
17
- 'return', 'if', 'else', 'while', 'for', 'do', 'break', 'continue', 'switch', 'case', 'default',
18
- 'try', 'catch', 'finally', 'throw', 'class', 'function', 'var', 'let', 'const', 'import', 'export', 'from',
19
- 'public', 'private', 'protected', 'static', 'extends', 'implements', 'new', 'this', 'super',
20
- 'typeof', 'instanceof', 'in', 'of', 'yield', 'await', 'async', 'interface', 'type', 'enum',
21
- 'void', 'null', 'undefined', 'true', 'false', 'def', 'pass', 'None', 'True', 'False',
22
- 'match', 'with', 'as', 'struct', 'func', 'go', 'chan', 'defer', 'select', 'fallthrough',
23
- 'namespace', 'using', 'pkg', 'mod', 'require', 'fn', 'pub', 'mut', 'impl', 'loop', 'unsafe',
24
- 'trait', 'where', 'macro_rules', 'use', 'int', 'float', 'double', 'char', 'bool'
25
- ].join('|');
26
-
27
- // We use named capture groups so we can easily map matches back to their token types.
28
- // Order is critical: comments first, then strings, then numbers, then keywords, etc.
29
- const UNIVERSAL_REGEX = new RegExp(
30
- `(?<comment>\\/\\/[^\\n]*|\\/\\*[\\s\\S]*?\\*\\/|#[^\\n]*|<!--[\\s\\S]*?-->)` +
31
- `|(?<string>"(?:\\\\.|[^"\\\\])*"|'(?:\\\\.|[^'\\\\])*'|\`(?:\\\\.|[^\`\\\\])*\`)` +
32
- `|(?<number>\\b\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?\\b|\\b0x[a-fA-F0-9]+\\b)` +
33
- `|(?<keyword>\\b(?:${COMMON_KEYWORDS})\\b)` +
34
- `|(?<function>[a-zA-Z_$][a-zA-Z0-9_$]*(?=\\s*\\())` +
35
- `|(?<property>(?<=\\.)[a-zA-Z_$][a-zA-Z0-9_$]*)` +
36
- `|(?<operator>[=+\\-*\\/%&|<>!^~?:]+)`,
37
- 'g'
38
- );
39
-
40
- export interface HighlightOptions {
41
- /**
42
- * Used strictly for class attribution, does not change the heuristic tokenization.
43
- */
44
- language?: string;
45
- /**
46
- * Mimic `highlight.js` class names to maintain compatibility with existing themes.
47
- */
48
- mimicHljs?: boolean;
49
- }
50
-
51
- export function highlight(code: string, options: HighlightOptions = {}): { value: string; language: string } {
52
- const isHljs = options.mimicHljs !== false;
53
- let result = '';
54
- let lastIndex = 0;
55
-
56
- for (const match of code.matchAll(UNIVERSAL_REGEX)) {
57
- const start = match.index!;
58
- const text = match[0];
59
- const groups = match.groups!;
60
-
61
- // Append un-matched text (escaped)
62
- if (start > lastIndex) {
63
- result += escapeHtml(code.slice(lastIndex, start));
64
- }
65
- lastIndex = start + text.length;
66
-
67
- // Identify token
68
- let tokenType = '';
69
- for (const key in groups) {
70
- if (groups[key] !== undefined) {
71
- tokenType = key;
72
- break;
73
- }
74
- }
75
-
76
- // Process Token Class
77
- let className = tokenType;
78
- if (isHljs) {
79
- if (tokenType === 'function') className = 'title function_';
80
- className = `hljs-${className}`;
81
- }
82
-
83
- result += `<span class="${className}">${escapeHtml(text)}</span>`;
84
- }
85
-
86
- // Append remaining text
87
- if (lastIndex < code.length) {
88
- result += escapeHtml(code.slice(lastIndex));
89
- }
90
-
91
- return {
92
- value: result,
93
- language: options.language || 'plaintext'
94
- };
95
- }
96
-
97
- // Ensure similar API signature to highlight.js
98
- export default {
99
- highlight,
100
- getLanguage: () => true // Assume support for all languages requested!
101
- };
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "Node16",
5
- "moduleResolution": "node16",
6
- "strict": true,
7
- "declaration": true,
8
- "outDir": "dist",
9
- "esModuleInterop": true,
10
- "forceConsistentCasingInFileNames": true,
11
- "skipLibCheck": true
12
- },
13
- "include": ["src/**/*"]
14
- }