Skip to content

Commit 0a76249

Browse files
committed
feat: Update to highlight.js v11
1 parent 7be7d6e commit 0a76249

File tree

8 files changed

+193
-146
lines changed

8 files changed

+193
-146
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# Changelog
22

3+
## 5.0.0
4+
5+
- Upgrade usage to highlight.js v11, you can find the breaking changes from the official page [here](https://highlightjs.readthedocs.io/en/latest/upgrade-11.html).
6+
- feat: Add the missing functions from v11 in `HighlightJS` service.
7+
- Remove deprecated function in v11, such as `initHighlighting()`, `highlightBlock()`, `fixMarkup()` from `HighlightJS` service.
8+
9+
### Breaking changes
10+
11+
- refactor: `(highlighted)` output has been updated to highlight.js v11, the result object has a new interface `HighlightAutoResult`.
12+
- refactor: `HighlightJS.configure(config)` has been updated the config interface to highlight.js v11.
13+
314
## 4.1.4
415

516
- Upgrade to Angular 12
6-
- Set peer dependency of hljs to v10
17+
- Set peer dependency of highlight.js to v10
18+
19+
> The library is still compatible with the new version of highlight.js v11 but the highlighting function will be deprecated in their next release
720
821
## 4.1.3
922

package-lock.json

Lines changed: 37 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@angular/platform-browser": "~12.0.2",
4848
"@angular/platform-browser-dynamic": "~12.0.2",
4949
"@angular/router": "~12.0.2",
50-
"highlight.js": "^10.7.2",
50+
"highlight.js": "^11.2.0",
5151
"highlightjs-line-numbers.js": "^2.8.0",
5252
"ngx-highlightjs": "^4.1.4",
5353
"ngx-scrollbar": "^7.5.6",

projects/ngx-highlightjs/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-highlightjs",
3-
"version": "4.1.4",
3+
"version": "5.0.0",
44
"description": "Instant code highlighting, auto-detect language, super easy to use.",
55
"homepage": "http://github.com/murhafsousli/ngx-highlightjs",
66
"author": {
@@ -25,13 +25,13 @@
2525
],
2626
"license": "MIT",
2727
"dependencies": {
28-
"highlight.js": "^10.0.0",
28+
"highlight.js": ">=11.0.0",
2929
"highlightjs-line-numbers.js": "^2.8.0",
3030
"tslib": "^2.0.0"
3131
},
3232
"peerDependencies": {
33-
"@angular/common": ">=8.0.0",
34-
"@angular/core": ">=8.0.0",
33+
"@angular/common": ">=12.0.0",
34+
"@angular/core": ">=12.0.0",
3535
"rxjs": ">=6.0.0"
3636
}
3737
}

projects/ngx-highlightjs/src/lib/highlight.model.ts

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
import { InjectionToken } from '@angular/core';
22

3+
/**
4+
* Full documentation is available here https://highlightjs.readthedocs.io/en/latest/api.html
5+
*/
6+
37
export interface HighlightLibrary {
48

59
/**
6-
* Core highlighting function.
7-
* @param name Accepts a language name, or an alias
8-
* @param value A string with the code to highlight.
9-
* @param ignore_illegals When present and evaluates to a true value, forces highlighting to finish
10-
* even in case of detecting illegal syntax for the language instead of throwing an exception.
11-
* @param continuation An optional mode stack representing unfinished parsing.
12-
* When present, the function will restart parsing from this state instead of initializing a new one
10+
* Core highlighting function. Accepts the code to highlight (string) and a list of options (object)
11+
* @param code Accepts the code to highlight
12+
* @param language must be present and specify the language name or alias of the grammar to be used for highlighting
13+
* @param ignoreIllegals (optional) when set to true it forces highlighting to finish even in case of detecting illegal syntax for the language instead of throwing an exception.
1314
*/
14-
highlight(name: string, value: string, ignore_illegals: boolean, continuation?: any): HighlightResult;
15+
highlight(code: string, { language, ignoreIllegals }: { language: string, ignoreIllegals: boolean }): HighlightResult;
1516

1617
/**
1718
* Highlighting with language detection.
1819
* @param value Accepts a string with the code to highlight
1920
* @param languageSubset An optional array of language names and aliases restricting detection to only those languages.
2021
* The subset can also be set with configure, but the local parameter overrides the option if set.
2122
*/
22-
highlightAuto(value: string, languageSubset: string[]): HighlightResult;
23+
highlightAuto(value: string, languageSubset: string[]): HighlightAutoResult;
2324

2425
/**
25-
* Post-processing of the highlighted markup.
26-
* Currently consists of replacing indentation TAB characters and using <br> tags instead of new-line characters.
27-
* Options are set globally with configure.
28-
* @param value Accepts a string with the highlighted markup
26+
* Applies highlighting to a DOM node containing code.
27+
* This function is the one to use to apply highlighting dynamically after page load or within initialization code of third-party
28+
* JavaScript frameworks.
29+
* The function uses language detection by default but you can specify the language in the class attribute of the DOM node.
30+
* See the scopes reference for all available language names and scopes.
31+
* @param element Element to highlight
2932
*/
30-
fixMarkup(value: string): string;
33+
highlightElement(element: HTMLElement): void;
3134

3235
/**
33-
* Applies highlighting to a DOM node containing code.
34-
* The function uses language detection by default but you can specify the language in the class attribute of the DOM node.
35-
* See the class reference for all available language names and aliases.
36-
* @param block The element to apply highlight on.
36+
* Applies highlighting to all elements on a page matching the configured cssSelector. The default cssSelector value is 'pre code',
37+
* which highlights all code blocks. This can be called before or after the page’s onload event has fired.
3738
*/
38-
highlightBlock(block: HTMLElement): void;
39+
highlightAll(): void;
3940

4041
/**
4142
* Configures global options:
@@ -44,17 +45,25 @@ export interface HighlightLibrary {
4445
configure(config: HighlightConfig): void;
4546

4647
/**
47-
* Applies highlighting to all <pre><code>..</code></pre> blocks on a page.
48+
* Adds new language to the library under the specified name. Used mostly internally.
49+
* @param languageName A string with the name of the language being registered
50+
* @param languageDefinition A function that returns an object which represents the language definition.
51+
* The function is passed the hljs object to be able to use common regular expressions defined within it.
4852
*/
49-
initHighlighting(): void;
53+
registerLanguage(languageName: string, languageDefinition: () => any): void;
5054

5155
/**
52-
* Adds new language to the library under the specified name. Used mostly internally.
53-
* @param name A string with the name of the language being registered
54-
* @param language A function that returns an object which represents the language definition.
55-
* The function is passed the hljs object to be able to use common regular expressions defined within it.
56+
* Removes a language and its aliases from the library. Used mostly internall
57+
* @param languageName: a string with the name of the language being removed.
58+
*/
59+
unregisterLanguage(languageName: string): void;
60+
61+
/**
62+
* Adds new language alias or aliases to the library for the specified language name defined under languageName key.
63+
* @param alias: A string or array with the name of alias being registered
64+
* @param languageName: the language name as specified by registerLanguage.
5665
*/
57-
registerLanguage(name: string, language: () => any): void;
66+
registerAliases(alias: string | string[], { languageName }: { languageName: string }): void;
5867

5968
/**
6069
* @return The languages names list.
@@ -68,6 +77,16 @@ export interface HighlightLibrary {
6877
*/
6978
getLanguage(name: string): any;
7079

80+
/**
81+
* Enables safe mode. This is the default mode, providing the most reliable experience for production usage.
82+
*/
83+
safeMode(): void;
84+
85+
/**
86+
* Enables debug/development mode.
87+
*/
88+
debugMode(): void;
89+
7190
/**
7291
* Add line numbers to code element
7392
* @param el Code element
@@ -76,21 +95,31 @@ export interface HighlightLibrary {
7695
}
7796

7897
export interface HighlightConfig {
79-
/** tabReplace: a string used to replace TAB characters in indentation. */
80-
tabReplace?: string;
81-
/** useBR: a flag to generate <br> tags instead of new-line characters in the output, useful when code is marked up using a non-<pre> container. */
82-
useBR?: boolean;
8398
/** classPrefix: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets. */
8499
classPrefix?: string;
85100
/** languages: an array of language names and aliases restricting auto detection to only these languages. */
86101
languages?: string[];
102+
/** languageDetectRe: a regex to configure how CSS class names map to language (allows class names like say color-as-php vs the default of language-php, etc.) */
103+
languageDetectRe: string;
104+
/** noHighlightRe: a regex to configure which CSS classes are to be skipped completely. */
105+
noHighlightRe: string;
106+
/** a CSS selector to configure which elements are affected by hljs.highlightAll. Defaults to 'pre code'. */
107+
cssSelector: string;
87108
}
88109

89110
export interface HighlightResult {
90111
language?: string;
91-
second_best?: any;
92-
top?: any;
93-
value?: string;
112+
value?: string | undefined;
113+
relevance?: number;
114+
top: any;
115+
code: string;
116+
illegal: boolean;
117+
}
118+
119+
export interface HighlightAutoResult {
120+
language?: string;
121+
secondBest?: any;
122+
value?: string | undefined;
94123
relevance?: number;
95124
}
96125

0 commit comments

Comments
 (0)