Skip to content

Commit

Permalink
(chore) import types, plus typos
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Apr 22, 2021
1 parent 0e8f0d2 commit 00c54bd
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 34 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Expand Up @@ -43,7 +43,11 @@ module.exports = {
overrides: [
{
files: ["types/*.ts", "src/*.ts"],
parser: '@typescript-eslint/parser'
parser: '@typescript-eslint/parser',
rules: {
"import/no-duplicates": "off",
"import/extensions": "off"
}
},
{
files: ["src/**/*.js"],
Expand Down
43 changes: 32 additions & 11 deletions src/highlight.js
Expand Up @@ -14,6 +14,27 @@ import { compileLanguage } from './lib/mode_compiler.js';
import * as packageJSON from '../package.json';
import * as logger from "./lib/logger.js";

/**
@typedef {import('highlight.js').Mode} Mode
@typedef {import('highlight.js').CompiledMode} CompiledMode
@typedef {import('highlight.js').Language} Language
@typedef {import('highlight.js').HLJSApi} HLJSApi
@typedef {import('highlight.js').HLJSPlugin} HLJSPlugin
@typedef {import('highlight.js').PluginEvent} PluginEvent
@typedef {import('highlight.js').HLJSOptions} HLJSOptions
@typedef {import('highlight.js').LanguageFn} LanguageFn
@typedef {import('highlight.js').HighlightedHTMLElement} HighlightedHTMLElement
@typedef {import('highlight.js').BeforeHighlightContext} BeforeHighlightContext
@typedef {import('highlight.js/private').MatchType} MatchType
@typedef {import('highlight.js/private').KeywordData} KeywordData
@typedef {import('highlight.js/private').EnhancedMatch} EnhancedMatch
@typedef {import('highlight.js/private').AnnotatedError} AnnotatedError
@typedef {import('highlight.js').AutoHighlightResult} AutoHighlightResult
@typedef {import('highlight.js').HighlightOptions} HighlightOptions
@typedef {import('highlight.js').HighlightResult} HighlightResult
*/


const escape = utils.escapeHTML;
const inherit = utils.inherit;
const NO_MATCH = Symbol("nomatch");
Expand Down Expand Up @@ -94,7 +115,7 @@ const HLJS = function(hljs) {
* NEW API
* highlight(code, {lang, ignoreIllegals})
*
* @param {string} codeOrlanguageName - the language to use for highlighting
* @param {string} codeOrLanguageName - the language to use for highlighting
* @param {string | HighlightOptions} optionsOrCode - the code to highlight
* @param {boolean} [ignoreIllegals] - whether to ignore illegal matches, default is to bail
* @param {CompiledMode} [continuation] - current continuation mode, if any
Expand All @@ -107,11 +128,11 @@ const HLJS = function(hljs) {
* @property {CompiledMode} top - top of the current mode stack
* @property {boolean} illegal - indicates whether any illegal matches were found
*/
function highlight(codeOrlanguageName, optionsOrCode, ignoreIllegals, continuation) {
function highlight(codeOrLanguageName, optionsOrCode, ignoreIllegals, continuation) {
let code = "";
let languageName = "";
if (typeof optionsOrCode === "object") {
code = codeOrlanguageName;
code = codeOrLanguageName;
ignoreIllegals = optionsOrCode.ignoreIllegals;
languageName = optionsOrCode.language;
// continuation not supported at all via the new API
Expand All @@ -121,7 +142,7 @@ const HLJS = function(hljs) {
// old API
logger.deprecated("10.7.0", "highlight(lang, code, ...args) has been deprecated.");
logger.deprecated("10.7.0", "Please use highlight(code, options) instead.\nhttps://github.com/highlightjs/highlight.js/issues/2277");
languageName = codeOrlanguageName;
languageName = codeOrLanguageName;
code = optionsOrCode;
}

Expand Down Expand Up @@ -271,7 +292,7 @@ const HLJS = function(hljs) {
// at this point modeBuffer should just be the match
emitMultiClass(mode, match);
modeBuffer = "";
} else if (mode.scope) {
} else if (mode.scope && typeof mode.scope === "string") {
emitter.openNode(language.classNameAliases[mode.scope] || mode.scope);
}
top = Object.create(mode, { parent: { value: top } });
Expand Down Expand Up @@ -315,7 +336,7 @@ const HLJS = function(hljs) {
*/
function doIgnore(lexeme) {
if (top.matcher.regexIndex === 0) {
// no more regexs to potentially match here, so we move the cursor forward one
// no more regexes to potentially match here, so we move the cursor forward one
// space
modeBuffer += lexeme[0];
return 1;
Expand Down Expand Up @@ -416,7 +437,7 @@ const HLJS = function(hljs) {
/**
* Process an individual match
*
* @param {string} textBeforeMatch - text preceeding the match (since the last match)
* @param {string} textBeforeMatch - text preceding the match (since the last match)
* @param {EnhancedMatch} [match] - the match itself
*/
function processLexeme(textBeforeMatch, match) {
Expand Down Expand Up @@ -498,7 +519,7 @@ const HLJS = function(hljs) {
throw new Error('Unknown language: "' + languageName + '"');
}

const md = compileLanguage(language, { plugins });
const md = compileLanguage(language);
let result = '';
/** @type {CompiledMode} */
let top = continuation || md;
Expand Down Expand Up @@ -695,12 +716,12 @@ const HLJS = function(hljs) {
language: result.language,
// TODO: remove with version 11.0
re: result.relevance,
relavance: result.relevance
relevance: result.relevance
};
if (result.secondBest) {
element.secondBest = {
language: result.secondBest.language,
relavance: result.secondBest.relevance
relevance: result.secondBest.relevance
};
}
}
Expand Down Expand Up @@ -922,7 +943,7 @@ const HLJS = function(hljs) {
}
}

// merge all the modes/regexs into our main object
// merge all the modes/regexes into our main object
Object.assign(hljs, MODES);

return hljs;
Expand Down
11 changes: 8 additions & 3 deletions src/lib/compiler_extensions.js
@@ -1,5 +1,10 @@
import * as regex from './regex.js';

/**
@typedef {import('highlight.js').CallbackResponse} CallbackResponse
@typedef {import('highlight.js').CompilerExt} CompilerExt
*/

// Grammar extensions / plugins
// See: https://github.com/highlightjs/highlight.js/issues/2833

Expand All @@ -24,7 +29,7 @@ import * as regex from './regex.js';
* @param {RegExpMatchArray} match
* @param {CallbackResponse} response
*/
function skipIfhasPrecedingDot(match, response) {
function skipIfHasPrecedingDot(match, response) {
const before = match.input[match.index - 1];
if (before === ".") {
response.ignoreMatch();
Expand All @@ -35,7 +40,7 @@ function skipIfhasPrecedingDot(match, response) {
*
* @type {CompilerExt}
*/
export function scopeClassName(mode, parent) {
export function scopeClassName(mode, _parent) {
// eslint-disable-next-line no-undefined
if (mode.className !== undefined) {
mode.scope = mode.className;
Expand All @@ -57,7 +62,7 @@ export function beginKeywords(mode, parent) {
// doesn't allow spaces in keywords anyways and we still check for the boundary
// first
mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')(?!\\.)(?=\\b|\\s)';
mode.__beforeBegin = skipIfhasPrecedingDot;
mode.__beforeBegin = skipIfHasPrecedingDot;
mode.keywords = mode.keywords || mode.beginKeywords;
delete mode.beginKeywords;

Expand Down
6 changes: 5 additions & 1 deletion src/lib/ext/multi_class.js
Expand Up @@ -2,6 +2,10 @@
import * as logger from "../../lib/logger.js";
import * as regex from "../regex.js";

/**
@typedef {import('highlight.js').CompiledMode} CompiledMode
*/

const MultiClassError = new Error();

/**
Expand Down Expand Up @@ -36,7 +40,7 @@ function remapScopeNames(mode, regexes) {
const scopeNames = mode.scope;
/** @type Record<number,boolean> */
const emit = {};
/** @type Record<number,string|true> */
/** @type Record<number,string> */
const positions = {};

for (let i = 1; i <= regexes.length; i++) {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/mode_compiler.js
Expand Up @@ -5,6 +5,14 @@ import { beforeMatchExt } from "./exts/before_match.js";
import { compileKeywords } from "./compile_keywords.js";
import { MultiClass } from "./ext/multi_class.js";

/**
@typedef {import('highlight.js').Mode} Mode
@typedef {import('highlight.js').CompiledMode} CompiledMode
@typedef {import('highlight.js').Language} Language
@typedef {import('highlight.js').HLJSPlugin} HLJSPlugin
@typedef {import('highlight.js').CompiledLanguage} CompiledLanguage
*/

// compilation

/**
Expand All @@ -13,12 +21,11 @@ import { MultiClass } from "./ext/multi_class.js";
* Given the raw result of a language definition (Language), compiles this so
* that it is ready for highlighting code.
* @param {Language} language
* @param {{plugins: HLJSPlugin[]}} opts
* @returns {CompiledLanguage}
*/
export function compileLanguage(language, { plugins }) {
export function compileLanguage(language) {
/**
* Builds a regex with the case sensativility of the current language
* Builds a regex with the case sensitivity of the current language
*
* @param {RegExp | string} value
* @param {boolean} [global]
Expand Down
3 changes: 3 additions & 0 deletions src/lib/modes.js
@@ -1,6 +1,9 @@
import { inherit } from './utils.js';
import * as regex from './regex.js';

/** @typedef {import('highlight.js').Mode} Mode */
/** @typedef {import('highlight.js').ModeCallback} ModeCallback */

// Common regexps
export const MATCH_NOTHING_RE = /\b\B/;
export const IDENT_RE = '[a-zA-Z]\\w*';
Expand Down
3 changes: 3 additions & 0 deletions src/lib/response.js
@@ -1,4 +1,7 @@
/** @typedef {import('highlight.js').CallbackResponse} CallbackResponse */
/** @typedef {import('highlight.js').CompiledMode} CompiledMode */
/** @implements CallbackResponse */

export default class Response {
/**
* @param {CompiledMode} mode
Expand Down
1 change: 1 addition & 0 deletions src/lib/token_tree.js
Expand Up @@ -2,6 +2,7 @@ import HTMLRenderer from './html_renderer.js';

/** @typedef {{kind?: string, sublanguage?: boolean, children: Node[]} | string} Node */
/** @typedef {{kind?: string, sublanguage?: boolean, children: Node[]} } DataNode */
/** @typedef {import('highlight.js').Emitter} Emitter */
/** */

class TokenTree {
Expand Down
32 changes: 17 additions & 15 deletions types/index.d.ts
Expand Up @@ -3,16 +3,28 @@
// For TS consumers who use Node and don't have dom in their tsconfig lib, import the necessary types here.
/// <reference lib="dom" />

declare module 'highlight.js/private' {
import { CompiledMode, Mode, Language } from "highlight.js";

type MatchType = "begin" | "end" | "illegal"
type EnhancedMatch = RegExpMatchArray & {rule: CompiledMode, type: MatchType}
type AnnotatedError = Error & {mode?: Mode | Language, languageName?: string, badRule?: Mode}

type KeywordData = [string, number];
type KeywordDict = Record<string, KeywordData>
}
declare module 'highlight.js' {

import { KeywordDict} from "highlight.js/private";

export type HLJSApi = PublicApi & ModesAPI

export interface VuePlugin {
install: (vue: any) => void
}

interface PublicApi {
highlight: (codeOrlanguageName: string, optionsOrCode: string | HighlightOptions, ignoreIllegals?: boolean, continuation?: Mode) => HighlightResult
highlight: (codeOrLanguageName: string, optionsOrCode: string | HighlightOptions, ignoreIllegals?: boolean, continuation?: Mode) => HighlightResult
highlightAuto: (code: string, languageSubset?: string[]) => AutoHighlightResult
highlightBlock: (element: HTMLElement) => void
highlightElement: (element: HTMLElement) => void
Expand Down Expand Up @@ -160,16 +172,7 @@ declare module 'highlight.js' {
addSublanguage(emitter: Emitter, subLanguageName: string): void
}

/************
PRIVATE API
************/

/* for jsdoc annotations in the JS source files */

type AnnotatedError = Error & {mode?: Mode | Language, languageName?: string, badRule?: Mode}
type HighlightedHTMLElement = HTMLElement & {result?: object, secondBest?: object, parentNode: HTMLElement}
type EnhancedMatch = RegExpMatchArray & {rule: CompiledMode, type: MatchType}
type MatchType = "begin" | "end" | "illegal"
export type HighlightedHTMLElement = HTMLElement & {result?: object, secondBest?: object, parentNode: HTMLElement}

/* modes */

Expand All @@ -178,15 +181,12 @@ declare module 'highlight.js' {
"on:begin"?: ModeCallback
}

interface CompiledLanguage extends LanguageDetail, CompiledMode {
export interface CompiledLanguage extends LanguageDetail, CompiledMode {
isCompiled: true
contains: CompiledMode[]
keywords: Record<string, any>
}

type KeywordData = [string, number];
type KeywordDict = Record<string, KeywordData>

export type CompiledMode = Omit<Mode, 'contains'> &
{
contains: CompiledMode[]
Expand All @@ -209,6 +209,8 @@ declare module 'highlight.js' {
match?: RegExp | string
end?: RegExp | string
className?: string
_emit?: Record<number, boolean>
scope?: string | Record<number, string>
contains?: ("self" | Mode)[]
endsParent?: boolean
endsWithParent?: boolean
Expand Down

0 comments on commit 00c54bd

Please sign in to comment.