Socket
Socket
Sign inDemoInstall

prismjs-terminal

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prismjs-terminal - npm Package Compare versions

Comparing version 1.2.0 to 1.2.1

91

dist/index.d.ts

@@ -0,20 +1,97 @@

/**
* A function that applies styles, typically one of the chalk builder functions
*/
export type StyleFn = (s: string) => string;
/**
* Either one {@link StyleFn} or several
*/
export type Styles = StyleFn | StyleFn[];
export type Theme = {
[k: string]: Styles;
} | Map<string, Styles>;
export type CompiledRule = [stack: string[], styles: StyleFn[]][];
export type CompiledTheme = Map<string, CompiledRule>;
import Prism from 'prismjs';
export type Tokens = string | Prism.Token | (string | Prism.Token)[];
/**
* Theme objects can be either a Map or object where the keys are the selectors
* and the values are either a styling function or an array of styling
* functions to be applied in order.
*
* The `_` style rule applies to the block as a whole, and is used
* as the default style. This is where you'd usually port a PrismJS
* theme's `code[class*="language-"]` css rule.
*
* The `lineNumber` style rule will apply to line numbers, if they
* are used.
*
* The semantics are similar to CSS, where a nested property will be
* applied to nodes within that nesting stack with a higher
* priority the more tags that match, and later rules taking
* precedence over earlier ones. It's _not_ a full CSS selector
* syntax though, so things like `.token.italic.bold` aren't
* supported. Just individual token class names, possibly nested.
* Also, chalk is not CSS, and a terminal is not a browser, so
* there are some differences and limitations of course.
*
* Aliases are also not supported, styles have to be applied to the
* actual parsed class names PrismJS provides.
*/
export type Theme = Record<string, Styles> | Map<string, Styles>;
/**
* Options for the highlighting functions
*/
export interface PrismJSTerminalOpts {
/**
* The theme to use. Either a {@link Theme} object, or a
* string identifying one of the built-in themes.
*
* @default 'moria'
*/
theme?: keyof typeof themes | Theme;
/**
* The language of the supplied code to highlight. Defaults to `tsx` if no
* filename is provided, or else tries to infer the language from the
* filename. You must have previously called `loadLanguages([...])` from
* `PrismJS` in order to highlight a given language, if you want something
* that is not automatically included when `tsx` and `typescript` are
* included.
*
* @default 'tsx'
*/
language?: string;
/**
* The minimum width to make the block on the screen.
*
* @default 0
*/
minWidth?: number;
/**
* The maximum width to make the block on the screen.
*
* @default `process.stdout.columns` or `80`.
*/
maxWidth?: number;
/**
* How many spaces to horizontally pad the code block.
*
* @default 1
*/
padding?: number;
/**
* Whether or not to prepend a line number to each line.
*
* @default false
*/
lineNumbers?: boolean;
}
/**
* Highlight the string of code provided, returning the string of highlighted
* code.
*/
export declare const highlight: (code: string, { language, theme, minWidth, maxWidth, padding, lineNumbers, }?: PrismJSTerminalOpts) => string;
/**
* Read the filename provided, and highlight its code. If a language is not
* provided in the opts, it will attempt to infer from the filename.
*/
export declare const highlightFile: (filename: string, opts?: PrismJSTerminalOpts) => Promise<string>;
/**
* Read the filename provided, and highlight its code. If a language is not
* provided in the opts, it will attempt to infer from the filename.
*
* Synchronous {@link highlightFile}
*/
export declare const highlightFileSync: (filename: string, opts?: PrismJSTerminalOpts) => string;

@@ -21,0 +98,0 @@ import * as themes from './themes/index.js';

24

dist/index.js

@@ -0,1 +1,2 @@

// TODO: start stack with [`language-${lang}`]
import chalk from 'chalk';

@@ -58,5 +59,3 @@ import { readFileSync } from 'fs';

};
/* c8 ignore start */
const trimTrailingCR = (c) => c.endsWith('\n') ? c.substring(0, c.length - 1) : c;
/* c8 ignore stop */
const blockStyle = (code, c, { minWidth = 0, maxWidth = process.stdout.columns || 80, padding = 1, lineNumbers = false, }) => {

@@ -66,9 +65,10 @@ const lines = trimTrailingCR(code).split('\n');

let max = minWidth;
const npad = lineNumbers ? String(lines.length).length : 0;
const tpad = npad + padding * 2;
for (const l of lines) {
const len = stringLength(l);
lens.push(len);
if (len < maxWidth && len > max)
if (len < maxWidth - tpad && len > max)
max = len;
}
const npad = lineNumbers ? String(lines.length).length : 0;
for (let i = 0; i < lens.length; i++) {

@@ -87,3 +87,7 @@ const len = lens[i];

};
export const highlight = (code, { language = 'tsx', theme = 'xonokai', minWidth, maxWidth, padding, lineNumbers, } = {}) => {
/**
* Highlight the string of code provided, returning the string of highlighted
* code.
*/
export const highlight = (code, { language = 'tsx', theme = 'moria', minWidth, maxWidth, padding, lineNumbers, } = {}) => {
const t = typeof theme === 'string' ? themes[theme] : theme;

@@ -113,2 +117,6 @@ if (!t) {

};
/**
* Read the filename provided, and highlight its code. If a language is not
* provided in the opts, it will attempt to infer from the filename.
*/
export const highlightFile = async (filename, opts = {}) => {

@@ -119,2 +127,8 @@ if (!opts.language)

};
/**
* Read the filename provided, and highlight its code. If a language is not
* provided in the opts, it will attempt to infer from the filename.
*
* Synchronous {@link highlightFile}
*/
export const highlightFileSync = (filename, opts = {}) => {

@@ -121,0 +135,0 @@ if (!opts.language)

import { Theme } from '../index.js';
/**
* This theme just shows which token names are in use
* The list of tokens is everything defined in the
* tsx PrismJS language grammar, since that is a superset
* of JavaScript, TS, JSX, and HTML.
* This theme just shows which token names are in use The list of tokens is
* everything defined in the tsx PrismJS language grammar, since that is a
* superset of JavaScript, TS, JSX, and HTML.
*/
export declare const theme: Theme;
//# sourceMappingURL=debug.d.ts.map
/**
* This theme just shows which token names are in use
* The list of tokens is everything defined in the
* tsx PrismJS language grammar, since that is a superset
* of JavaScript, TS, JSX, and HTML.
* This theme just shows which token names are in use The list of tokens is
* everything defined in the tsx PrismJS language grammar, since that is a
* superset of JavaScript, TS, JSX, and HTML.
*/

@@ -7,0 +6,0 @@ export const theme = new Map(`

import type { Theme } from '../index.js';
/**
* Port of GHColors prismjs theme.
* <https://github.com/PrismJS/prism-themes/blob/057c2a4430d78268528e65ba92860703c9cb56d8/themes/prism-ghcolors.css>
*/
export declare const theme: Theme;
//# sourceMappingURL=github.d.ts.map

@@ -1,4 +0,6 @@

// Port of GHColors PrismJS theme
// https://github.com/PrismJS/prism-themes/blob/057c2a4430d78268528e65ba92860703c9cb56d8/themes/prism-ghcolors.css
import chalk from 'chalk';
/**
* Port of GHColors prismjs theme.
* <https://github.com/PrismJS/prism-themes/blob/057c2a4430d78268528e65ba92860703c9cb56d8/themes/prism-ghcolors.css>
*/
export const theme = {

@@ -5,0 +7,0 @@ _: chalk.hex('#393A34').bgHex('#fff'),

@@ -0,7 +1,39 @@

/**
* Port of the `xonokai` PrismJS theme.
*/
export { theme as xonokai } from './xonokai.js';
/**
* This theme just shows which token names are in use The list of tokens is
* everything defined in the tsx PrismJS language grammar, since that is a
* superset of JavaScript, TS, JSX, and HTML.
*/
export { theme as debug } from './debug.js';
/**
* Inspired by vim Moria theme
* https://github.com/vim-scripts/moria
*
* It's not *quite* as faithful as I'd like (and I *would* like, since it's the
* color scheme I personally use in Vim, and so most natural for me), because
* Vim and PrismJS make slightly different choices about how they label program
* tokens.
*/
export { theme as moria } from './moria.js';
/**
* Port of GHColors prismjs theme.
* <https://github.com/PrismJS/prism-themes/blob/057c2a4430d78268528e65ba92860703c9cb56d8/themes/prism-ghcolors.css>
*/
export { theme as github } from './github.js';
/**
* Port of the `prism-dark` PrismJS theme.
* <https://github.com/PrismJS/prism/blob/867804573562ef0ac5acf470420b97e43488ccde/themes/prism-dark.css>
*
* Slightly tweaked a few colors so that they are a bit more readable on a
* terminal, some terminals have rendering issues when contrast is too low.
*/
export { theme as prismDark } from './prism-dark.js';
/**
* No styling, just the code as plain text.
* Will add line numbers and padding if requested, of course.
*/
export { theme as plain } from './plain.js';
//# sourceMappingURL=index.d.ts.map

@@ -0,7 +1,39 @@

/**
* Port of the `xonokai` PrismJS theme.
*/
export { theme as xonokai } from './xonokai.js';
/**
* This theme just shows which token names are in use The list of tokens is
* everything defined in the tsx PrismJS language grammar, since that is a
* superset of JavaScript, TS, JSX, and HTML.
*/
export { theme as debug } from './debug.js';
/**
* Inspired by vim Moria theme
* https://github.com/vim-scripts/moria
*
* It's not *quite* as faithful as I'd like (and I *would* like, since it's the
* color scheme I personally use in Vim, and so most natural for me), because
* Vim and PrismJS make slightly different choices about how they label program
* tokens.
*/
export { theme as moria } from './moria.js';
/**
* Port of GHColors prismjs theme.
* <https://github.com/PrismJS/prism-themes/blob/057c2a4430d78268528e65ba92860703c9cb56d8/themes/prism-ghcolors.css>
*/
export { theme as github } from './github.js';
/**
* Port of the `prism-dark` PrismJS theme.
* <https://github.com/PrismJS/prism/blob/867804573562ef0ac5acf470420b97e43488ccde/themes/prism-dark.css>
*
* Slightly tweaked a few colors so that they are a bit more readable on a
* terminal, some terminals have rendering issues when contrast is too low.
*/
export { theme as prismDark } from './prism-dark.js';
/**
* No styling, just the code as plain text.
* Will add line numbers and padding if requested, of course.
*/
export { theme as plain } from './plain.js';
//# sourceMappingURL=index.js.map
import { Theme } from '../index.js';
/**
* Inspired by vim Moria theme
* https://github.com/vim-scripts/moria
*
* It's not *quite* as faithful as I'd like (and I *would* like, since it's the
* color scheme I personally use in Vim, and so most natural for me), because
* Vim and PrismJS make slightly different choices about how they label program
* tokens.
*/
export declare const theme: Theme;
//# sourceMappingURL=moria.d.ts.map

@@ -1,9 +0,11 @@

// Inspired by vim Moria theme
// https://github.com/vim-scripts/moria
//
// It's not *quite* as faithful as I'd like (and I *would* like, since it's
// the color scheme I personally use in Vim, and so most natural for me),
// because Vim and PrismJS make slightly different choices about how they
// label program tokens.
import chalk from 'chalk';
/**
* Inspired by vim Moria theme
* https://github.com/vim-scripts/moria
*
* It's not *quite* as faithful as I'd like (and I *would* like, since it's the
* color scheme I personally use in Vim, and so most natural for me), because
* Vim and PrismJS make slightly different choices about how they label program
* tokens.
*/
export const theme = {

@@ -10,0 +12,0 @@ _: chalk.ansi256(252).bgAnsi256(234),

@@ -0,3 +1,7 @@

/**
* No styling, just the code as plain text.
* Will add line numbers and padding if requested, of course.
*/
import { Theme } from '../index.js';
export declare const theme: Theme;
//# sourceMappingURL=plain.d.ts.map
import type { Theme } from '../index.js';
/**
* Port of the `prism-dark` PrismJS theme.
* <https://github.com/PrismJS/prism/blob/867804573562ef0ac5acf470420b97e43488ccde/themes/prism-dark.css>
*
* Slightly tweaked a few colors so that they are a bit more readable on a
* terminal, some terminals have rendering issues when contrast is too low.
*/
export declare const theme: Theme;
//# sourceMappingURL=prism-dark.d.ts.map

@@ -1,6 +0,9 @@

// Port of prism-dark theme
// https://github.com/PrismJS/prism/blob/867804573562ef0ac5acf470420b97e43488ccde/themes/prism-dark.css
// Slightly tweaked a few colors so that they are a bit more readable on
// a terminal, some terminals have rendering issues when contrast is too low.
import chalk from 'chalk';
/**
* Port of the `prism-dark` PrismJS theme.
* <https://github.com/PrismJS/prism/blob/867804573562ef0ac5acf470420b97e43488ccde/themes/prism-dark.css>
*
* Slightly tweaked a few colors so that they are a bit more readable on a
* terminal, some terminals have rendering issues when contrast is too low.
*/
export const theme = {

@@ -7,0 +10,0 @@ _: chalk.white.bgHex('#231E19'),

import type { Theme } from '../index.js';
/**
* Port of xonokai PrismJS theme
* <https://github.com/PrismJS/prism-themes/blob/fe8d8fd7b80fb7d6bc0ed1a7b53009e5632701d7/themes/prism-xonokai.css>
*/
export declare const theme: Theme;
//# sourceMappingURL=xonokai.d.ts.map

@@ -1,4 +0,6 @@

// Port of xonokai PrismJS theme
// https://github.com/PrismJS/prism-themes/blob/fe8d8fd7b80fb7d6bc0ed1a7b53009e5632701d7/themes/prism-xonokai.css
import chalk from 'chalk';
/**
* Port of xonokai PrismJS theme
* <https://github.com/PrismJS/prism-themes/blob/fe8d8fd7b80fb7d6bc0ed1a7b53009e5632701d7/themes/prism-xonokai.css>
*/
export const theme = {

@@ -5,0 +7,0 @@ // default style

{
"name": "prismjs-terminal",
"version": "1.2.0",
"version": "1.2.1",
"description": "PrismaJS syntax highlighting for the terminal",

@@ -50,3 +50,3 @@ "keywords": [

"format": "prettier --write . --loglevel warn",
"typedoc": "typedoc --tsconfig tsconfig-esm.json ./src/*.ts"
"typedoc": "typedoc ./src/*.ts"
},

@@ -92,3 +92,3 @@ "prettier": {

"engines": {
"node": ">=14"
"node": ">=16"
},

@@ -98,3 +98,3 @@ "funding": {

},
"repository": "https://github.com/isaacs/prismajs-term",
"repository": "https://github.com/isaacs/prismajs-terminal",
"dependencies": {

@@ -101,0 +101,0 @@ "chalk": "^5.2.0",

@@ -83,3 +83,4 @@ # prismjs-terminal

- `theme` The theme to use. Either a Theme object, or a
string identifying one of the built-in themes.
string identifying one of the built-in themes. Defaults to
`'moria'`.
- `language` The language of the supplied code to highlight.

@@ -86,0 +87,0 @@ Defaults to `tsx` if no filename is provided, or else

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc