Skip to content

Commit

Permalink
fix: formatting and import updates
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Best-Codes committed Dec 12, 2024
1 parent d1e643e commit 787fada
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ node_modules
dist

# bun
bun.lockb
bun.lockb
7 changes: 3 additions & 4 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";


/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
];
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "best-highlight",
"version": "0.2.0",
"version": "0.2.1",
"description": "A lightweight syntax highlighter",
"type": "module",
"main": "./dist/index.cjs",
Expand Down Expand Up @@ -55,7 +55,8 @@
"rollup": "^4.28.1",
"rollup-plugin-dts": "^6.1.1",
"tsup": "^8.3.5",
"typescript": "^5.7.2"
"typescript": "^5.7.2",
"typescript-eslint": "^8.18.0"
},
"files": [
"dist"
Expand Down
24 changes: 12 additions & 12 deletions rollup.dts.config.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import dts from 'rollup-plugin-dts';
import { createMinifier } from 'dts-minify';
import * as ts from 'typescript';
import { writeFileSync } from 'fs';
import { createMinifier } from "dts-minify";
import { writeFileSync } from "fs";
import dts from "rollup-plugin-dts";
import ts from "typescript";

const minifier = createMinifier(ts);

const minifyPlugin = {
name: 'minify-dts',
name: "minify-dts",
writeBundle(options, bundle) {
for (const fileName in bundle) {
const file = bundle[fileName];
if (fileName.endsWith('.d.ts')) {
if (fileName.endsWith(".d.ts")) {
const minified = minifier.minify(file.code, {
keepJsDocs: false
keepJsDocs: false,
});
writeFileSync(options.file, minified);
}
}
}
},
};

export default {
input: './src/index.ts',
input: "./src/index.ts",
output: {
file: 'dist/index.d.ts',
format: 'es'
file: "dist/index.d.ts",
format: "es",
},
plugins: [dts(), minifyPlugin]
plugins: [dts(), minifyPlugin],
};
2 changes: 1 addition & 1 deletion scripts/compress-readme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ cp "$README_PATH" "$BACKUP_PATH"
# Replace content with minimal version
echo '[See GitHub Repo for details](https://github.com/The-Best-Codes/best-highlight#readme).' > "$README_PATH"

echo "README.md has been compressed and backed up to read-backup"
echo "README.md has been compressed and backed up to read-backup"
12 changes: 6 additions & 6 deletions scripts/minify-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ export {};

const inputFile = Bun.argv[2];
if (!inputFile) {
console.error('Please provide a CSS file to minify');
console.error("Please provide a CSS file to minify");
process.exit(1);
}

const css = await Bun.file(inputFile).text();

// Simple CSS minification
const minified = css
.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/g, '') // Remove comments and whitespace
.replace(/ {2,}/g, ' ') // Remove multiple spaces
.replace(/ ([{:}]) /g, '$1') // Remove spaces around brackets and colons
.replace(/([{:}]) /g, '$1') // Remove spaces after brackets and colons
.replace(/;}/g, '}') // Remove last semicolon in block
.replace(/\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/g, "") // Remove comments and whitespace
.replace(/ {2,}/g, " ") // Remove multiple spaces
.replace(/ ([{:}]) /g, "$1") // Remove spaces around brackets and colons
.replace(/([{:}]) /g, "$1") // Remove spaces after brackets and colons
.replace(/;}/g, "}") // Remove last semicolon in block
.trim();

console.log(minified);
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeAll, describe, expect, test } from "bun:test";
import { tokenize, highlight, highlightElement } from "./index";
import { JSDOM } from "jsdom";
import { highlight, highlightElement, tokenize } from "./index";

// Setup DOM environment
let dom: JSDOM;
Expand Down Expand Up @@ -197,7 +197,7 @@ describe("highlightElement", () => {
highlightElement(element);

expect(element.innerHTML).toBe(
'<span class="bh-npm-token bh-npm-text"></span>'
'<span class="bh-npm-token bh-npm-text"></span>',
);
expect(element.classList.contains("bh-npm-highlight")).toBe(true);
});
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Token } from "./types";
import { languages } from "./langs";
import { Token } from "./types";

// Compiled whitespace pattern
const whitespacePattern = /^\s+/;
Expand All @@ -14,7 +14,7 @@ export function tokenize(code: string, language: string): Token[] {
for (const [type, patterns] of Object.entries(lang)) {
compiledPatterns.set(
type,
patterns.map((p) => new RegExp(p.source, p.flags))
patterns.map((p) => new RegExp(p.source, p.flags)),
);
}

Expand Down Expand Up @@ -91,8 +91,8 @@ export function highlight(code: string, language: string): string {
.map(
(token) =>
`<span class="bh-npm-token bh-npm-${token.type}">${escapeHtml(
token.content
)}</span>`
token.content,
)}</span>`,
)
.join("");
}
Expand Down
8 changes: 4 additions & 4 deletions src/langs/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { TokenPattern } from "../types";
import { javascript } from "./javascript";
import { python } from "./python";
import { typescript } from "./typescript";
import { html } from "./html";
import { css } from "./css";
import { html } from "./html";
import { javascript } from "./javascript";
import { json } from "./json";
import { markdown } from "./markdown";
import { python } from "./python";
import { typescript } from "./typescript";

export const languages: { [key: string]: TokenPattern } = {
javascript,
Expand Down
92 changes: 70 additions & 22 deletions src/themes/dark.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,76 @@
.bh-npm-token.bh-npm-keyword { color: #c586c0; }
.bh-npm-token.bh-npm-string { color: #ce9178; }
.bh-npm-token.bh-npm-number { color: #b5cea8; }
.bh-npm-token.bh-npm-comment { color: #6a9955; }
.bh-npm-token.bh-npm-punctuation { color: #d4d4d4; }
.bh-npm-token.bh-npm-operator { color: #569cd6; }
.bh-npm-token.bh-npm-identifier { color: #9cdcfe; }
.bh-npm-token.bh-npm-keyword {
color: #c586c0;
}
.bh-npm-token.bh-npm-string {
color: #ce9178;
}
.bh-npm-token.bh-npm-number {
color: #b5cea8;
}
.bh-npm-token.bh-npm-comment {
color: #6a9955;
}
.bh-npm-token.bh-npm-punctuation {
color: #d4d4d4;
}
.bh-npm-token.bh-npm-operator {
color: #569cd6;
}
.bh-npm-token.bh-npm-identifier {
color: #9cdcfe;
}

/* CSS specific */
.bh-npm-token.bh-npm-value { color: #b5cea8; }
.bh-npm-token.bh-npm-selector { color: #d7ba7d; }
.bh-npm-token.bh-npm-property { color: #9cdcfe; }
.bh-npm-token.bh-npm-value {
color: #b5cea8;
}
.bh-npm-token.bh-npm-selector {
color: #d7ba7d;
}
.bh-npm-token.bh-npm-property {
color: #9cdcfe;
}

/* Markdown specific */
.bh-npm-token.bh-npm-heading { color: #569cd6; font-weight: bold; }
.bh-npm-token.bh-npm-emphasis { font-style: italic; }
.bh-npm-token.bh-npm-link { color: #4ec9b0; text-decoration: underline; }
.bh-npm-token.bh-npm-image { color: #4ec9b0; }
.bh-npm-token.bh-npm-code { color: #ce9178; background: #1e1e1e; }
.bh-npm-token.bh-npm-list { color: #d4d4d4; }
.bh-npm-token.bh-npm-blockquote { color: #608b4e; font-style: italic; }
.bh-npm-token.bh-npm-horizontalRule { color: #d4d4d4; }
.bh-npm-token.bh-npm-heading {
color: #569cd6;
font-weight: bold;
}
.bh-npm-token.bh-npm-emphasis {
font-style: italic;
}
.bh-npm-token.bh-npm-link {
color: #4ec9b0;
text-decoration: underline;
}
.bh-npm-token.bh-npm-image {
color: #4ec9b0;
}
.bh-npm-token.bh-npm-code {
color: #ce9178;
background: #1e1e1e;
}
.bh-npm-token.bh-npm-list {
color: #d4d4d4;
}
.bh-npm-token.bh-npm-blockquote {
color: #608b4e;
font-style: italic;
}
.bh-npm-token.bh-npm-horizontalRule {
color: #d4d4d4;
}

/* HTML specific */
.bh-npm-token.bh-npm-tag { color: #569cd6; }
.bh-npm-token.bh-npm-attribute { color: #9cdcfe; }
.bh-npm-token.bh-npm-doctype { color: #808080; }
.bh-npm-token.bh-npm-entity { color: #d7ba7d; }
.bh-npm-token.bh-npm-tag {
color: #569cd6;
}
.bh-npm-token.bh-npm-attribute {
color: #9cdcfe;
}
.bh-npm-token.bh-npm-doctype {
color: #808080;
}
.bh-npm-token.bh-npm-entity {
color: #d7ba7d;
}
92 changes: 70 additions & 22 deletions src/themes/github.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,76 @@
.bh-npm-token.bh-npm-keyword { color: #d73a49; }
.bh-npm-token.bh-npm-string { color: #032f62; }
.bh-npm-token.bh-npm-number { color: #005cc5; }
.bh-npm-token.bh-npm-comment { color: #6a737d; }
.bh-npm-token.bh-npm-punctuation { color: #24292e; }
.bh-npm-token.bh-npm-operator { color: #d73a49; }
.bh-npm-token.bh-npm-identifier { color: #24292e; }
.bh-npm-token.bh-npm-keyword {
color: #d73a49;
}
.bh-npm-token.bh-npm-string {
color: #032f62;
}
.bh-npm-token.bh-npm-number {
color: #005cc5;
}
.bh-npm-token.bh-npm-comment {
color: #6a737d;
}
.bh-npm-token.bh-npm-punctuation {
color: #24292e;
}
.bh-npm-token.bh-npm-operator {
color: #d73a49;
}
.bh-npm-token.bh-npm-identifier {
color: #24292e;
}

/* CSS specific */
.bh-npm-token.bh-npm-value { color: #005cc5; }
.bh-npm-token.bh-npm-selector { color: #22863a; }
.bh-npm-token.bh-npm-property { color: #005cc5; }
.bh-npm-token.bh-npm-value {
color: #005cc5;
}
.bh-npm-token.bh-npm-selector {
color: #22863a;
}
.bh-npm-token.bh-npm-property {
color: #005cc5;
}

/* Markdown specific */
.bh-npm-token.bh-npm-heading { color: #24292e; font-weight: bold; }
.bh-npm-token.bh-npm-emphasis { font-style: italic; }
.bh-npm-token.bh-npm-link { color: #0366d6; text-decoration: underline; }
.bh-npm-token.bh-npm-image { color: #0366d6; }
.bh-npm-token.bh-npm-code { color: #24292e; background: #f6f8fa; }
.bh-npm-token.bh-npm-list { color: #24292e; }
.bh-npm-token.bh-npm-blockquote { color: #6a737d; font-style: italic; }
.bh-npm-token.bh-npm-horizontalRule { color: #e1e4e8; }
.bh-npm-token.bh-npm-heading {
color: #24292e;
font-weight: bold;
}
.bh-npm-token.bh-npm-emphasis {
font-style: italic;
}
.bh-npm-token.bh-npm-link {
color: #0366d6;
text-decoration: underline;
}
.bh-npm-token.bh-npm-image {
color: #0366d6;
}
.bh-npm-token.bh-npm-code {
color: #24292e;
background: #f6f8fa;
}
.bh-npm-token.bh-npm-list {
color: #24292e;
}
.bh-npm-token.bh-npm-blockquote {
color: #6a737d;
font-style: italic;
}
.bh-npm-token.bh-npm-horizontalRule {
color: #e1e4e8;
}

/* HTML specific */
.bh-npm-token.bh-npm-tag { color: #22863a; }
.bh-npm-token.bh-npm-attribute { color: #005cc5; }
.bh-npm-token.bh-npm-doctype { color: #6a737d; }
.bh-npm-token.bh-npm-entity { color: #005cc5; }
.bh-npm-token.bh-npm-tag {
color: #22863a;
}
.bh-npm-token.bh-npm-attribute {
color: #005cc5;
}
.bh-npm-token.bh-npm-doctype {
color: #6a737d;
}
.bh-npm-token.bh-npm-entity {
color: #005cc5;
}
Loading

0 comments on commit 787fada

Please sign in to comment.