Skip to content

Commit

Permalink
fix: typescript typing and jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
matubu committed Jan 4, 2024
1 parent 3422ac9 commit cffe4bb
Show file tree
Hide file tree
Showing 49 changed files with 7,383 additions and 12,800 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ for pid in $(jobs -p); do
wait $pid || (wait && echo -e "\n\033[91mError: could not minify some files\033[0m" && exit 1)
done

cp src/*.d.ts dist
cp src/*.d.ts dist/node
npx -p typescript tsc --outDir dist
npx -p typescript tsc --outDir dist/node

echo '{"type": "commonjs"}' > dist/node/package.json

exit 0
12 changes: 5 additions & 7 deletions .github/workflows/jsdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ jobs:
- name: Set up workspace
uses: actions/checkout@v3

- name: Build documention
uses: andstor/jsdoc-action@v1.2.1
- uses: actions/setup-node@v4
with:
source_dir: ./src
output_dir: ./doc
config_file: jsdoc.json
template: '@alexispuga/jsdoc-template'
front_page: README.md
node-version: 20

- name: Build documention
run: npx -y typedoc src/index.js src/detect.js src/terminal.js

- name: Auto commit
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Release

"on":
on:
push:
branches:
- main
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ npm i @speed-highlight/core
```

```js
const { setTheme, printHighlight } = require('@speed-highlight/core/terminal.js');
const { setTheme, printHighlight } = require('@speed-highlight/core/terminal');

setTheme('[theme-name]')
printHighlight('console.log("hello")', 'js')
Expand Down
11 changes: 3 additions & 8 deletions dist/detect.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Try to find the language the given code belong to
* @param code The code
* @returns The language of the code
*/
export declare function detectLanguage(
code: string
): Promise<import('.').ShjLanguage>
export function detectLanguage(code: string): ShjLanguage;
export type ShjLanguage = import('./index.js').ShjLanguage;
//# sourceMappingURL=detect.d.ts.map
141 changes: 44 additions & 97 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,61 @@
/////////// typedef.js ///////////

/**
* Supported languages.
*/
export type ShjLanguage = 'asm'|'bash'|'bf'|'c'|'css'|'csv'|'diff'|'docker'|'git'|'go'|'html'|'http'|'ini'|'java'|'js'|'jsdoc'|'json'|'leanpub-md'|'log'|'lua'|'make'|'md'|'pl'|'plain'|'py'|'regex'|'rs'|'sql'|'todo'|'toml'|'ts'|'uri'|'xml'|'yaml';

/**
* Themes supported in the browser.
*/
export type ShjBrowserTheme = 'atom-dark'|'github-dark'|'github-dim'|'dark'|'default'|'github-light'|'visual-studio-dark';

/**
* Languages supported in the terminal.
*/
export type ShjTerminalTheme = 'default'|'atom-dark';

/**
* * `inline` inside `code` element
* * `oneline` inside `div` element and containing only one line
* * `multiline` inside `div` element
*/
export type ShjDisplayMode = 'inline'|'oneline'|'multiline';

export type ShjToken = 'deleted'|'err'|'var'|'section'|'kwd'|'class'|'cmnt'|'insert'|'type'|'func'|'bool'|'num'|'oper'|'str'|'esc';

export interface ShjOptions {
/**
* Indicates whether to hide line numbers.
* @default false
*/
hideLineNumbers: boolean;
}

/////////// Custom Types ///////////

export type ShjLanguageComponent =
| { type: string; match: RegExp }
| { expand: string }
| {
match: RegExp;
sub:
| string
| ((code: string) => {
type: string;
sub: Array<{ match: RegExp; sub: string | Promise<string> }>
});
};

export type ShjLanguageDefinition = Array<ShjLanguageComponent>;

/////////// index.js ///////////

/**
* Find the tokens in the given code and call the callback
* @param src The code
* @param lang The language of the code
* @param callback The callback function
* Find the tokens in the given code and call the given callback
*
* @function tokenize
* @param {String} src The code
* @param {ShjLanguage|Array} lang The language of the code
* @param {function(String, ShjToken=):void} token The callback function
* this function will be given
* * the text of the token
* * the type of the token
*/
export declare function tokenize<T extends string = ShjLanguage>(
src: string,
lang: T,
callback: (value: string, token: ShjToken) => void
): Promise<void>;

export function tokenize(src: string, lang: ShjLanguage | any[], token: (arg0: string, arg1: ShjToken | undefined) => void): Promise<void>;
/**
* Highlight a string passed as argument and return it
* @example
* elm.innerHTML = await highlightText(code, 'js');
* @param src The code
* @param lang The language of the code
* @param multiline If it is multiline, it will add a wrapper for the line numbering and header
* @param Customization options
* @returns The highlighted string
*
* @async
* @function highlightText
* @param {String} src The code
* @param {ShjLanguage} lang The language of the code
* @param {Boolean} [multiline=true] If it is multiline, it will add a wrapper for the line numbering and header
* @param {ShjOptions} [opt={}] Customization options
* @returns {Promise<String>} The highlighted string
*/
export declare function highlightText<T extends string = ShjLanguage>(
src: string,
lang: T,
multiline?: boolean,
opt?: ShjOptions
): Promise<string>;

export function highlightText(src: string, lang: ShjLanguage, multiline?: boolean, opt?: ShjOptions): Promise<string>;
/**
* Highlight a DOM element by getting the new innerHTML with highlightText
* @param elm The DOM element
* @param lang The language of the code (seaching by default on `elm` for a 'shj-lang-' class)
* @param mode The display mode (guessed by default)
* @param opt Customization options
*
* @async
* @function highlightElement
* @param {Element} elm The DOM element
* @param {ShjLanguage} [lang] The language of the code (seaching by default on `elm` for a 'shj-lang-' class)
* @param {ShjDisplayMode} [mode] The display mode (guessed by default)
* @param {ShjOptions} [opt={}] Customization options
*/
export function highlightElement(elm: Element, lang?: ShjLanguage, mode?: ShjDisplayMode, opt?: ShjOptions): Promise<void>;
export function highlightAll(opt?: ShjOptions): Promise<any>;
export function loadLanguage(languageName: string, language: ShjLanguage): void;
export type ShjToken = ('deleted' | 'err' | 'var' | 'section' | 'kwd' | 'class' | 'cmnt' | 'insert' | 'type' | 'func' | 'bool' | 'num' | 'oper' | 'str' | 'esc');
export type ShjOptions = {
/**
* Indicates whether to hide line numbers
*/
hideLineNumbers?: boolean;
};
/**
* * `inline` inside `code` element
* * `oneline` inside `div` element and containing only one line
* * `multiline` inside `div` element
*/
export declare function highlightElement<T extends string = ShjLanguage>(
elm: Element,
lang?: T,
mode?: ShjDisplayMode,
opt?: ShjOptions
): Promise<void>;

export type ShjDisplayMode = ('inline' | 'oneline' | 'multiline');
/**
* Call highlightElement on element with a css class starting with `shj-lang-`
* @param opt Customization options
* Themes supported in the browser
*/
export declare function highlightAll(opt?: ShjOptions): Promise<void>;

export type ShjBrowserTheme = ('atom-dark' | 'github-dark' | 'github-dim' | 'dark' | 'default' | 'github-light' | 'visual-studio-dark');
/**
* Load a language and add it to the langs object
* @param name The name of the language
* @param module Module that has the language as the default export
* Languages supported
*/
export declare function loadLanguage(
name: string,
module: { default: ShjLanguageDefinition }
): void;
export type ShjLanguage = ('asm' | 'bash' | 'bf' | 'c' | 'css' | 'csv' | 'diff' | 'docker' | 'git' | 'go' | 'html' | 'http' | 'ini' | 'java' | 'js' | 'jsdoc' | 'json' | 'leanpub-md' | 'log' | 'lua' | 'make' | 'md' | 'pl' | 'plain' | 'py' | 'regex' | 'rs' | 'sql' | 'todo' | 'toml' | 'ts' | 'uri' | 'xml' | 'yaml');
//# sourceMappingURL=index.d.ts.map
4 changes: 2 additions & 2 deletions dist/languages/md.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions dist/node/detect.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Try to find the language the given code belong to
* @param code The code
* @returns The language of the code
*/
export declare function detectLanguage(
code: string
): Promise<import('.').ShjLanguage>
export function detectLanguage(code: string): ShjLanguage;
export type ShjLanguage = import('./index.js').ShjLanguage;
//# sourceMappingURL=detect.d.ts.map
Loading

0 comments on commit cffe4bb

Please sign in to comment.