nxt-highlightjs 22.0.0 → 22.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,576 +1,544 @@
1
- import * as i0 from '@angular/core';
2
- import { InjectionToken, makeEnvironmentProviders, inject, DOCUMENT, PendingTasks, resource, Injectable, ElementRef, effect, SecurityContext, Directive, input, signal, output, booleanAttribute } from '@angular/core';
3
- import { DomSanitizer } from '@angular/platform-browser';
4
-
5
- /**
6
- * @internal
7
- */
8
- const NXT_HIGHLIGHT_OPTIONS = new InjectionToken('NXT_HIGHLIGHT_OPTIONS');
1
+ import * as i0 from "@angular/core";
2
+ import { DOCUMENT, Directive, ElementRef, Injectable, InjectionToken, PendingTasks, SecurityContext, booleanAttribute, effect, inject, input, makeEnvironmentProviders, output, resource, signal } from "@angular/core";
3
+ import { DomSanitizer } from "@angular/platform-browser";
4
+ const NXT_HIGHLIGHT_OPTIONS = new InjectionToken("NXT_HIGHLIGHT_OPTIONS");
9
5
  function provideHighlightOptions(options) {
10
- return makeEnvironmentProviders([
11
- {
12
- provide: NXT_HIGHLIGHT_OPTIONS,
13
- useValue: options
14
- }
15
- ]);
6
+ return makeEnvironmentProviders([{
7
+ provide: NXT_HIGHLIGHT_OPTIONS,
8
+ useValue: options
9
+ }]);
16
10
  }
17
-
18
- /**
19
- * @internal
20
- */
21
11
  var LoaderErrors;
22
- (function (LoaderErrors) {
23
- LoaderErrors["FULL_WITH_CORE_LIBRARY_IMPORTS"] = "The full library and the core library were imported, only one of them should be imported!";
24
- LoaderErrors["FULL_WITH_LANGUAGE_IMPORTS"] = "The highlighting languages were imported they are not needed!";
25
- LoaderErrors["CORE_WITHOUT_LANGUAGE_IMPORTS"] = "The highlighting languages were not imported!";
26
- LoaderErrors["LANGUAGE_WITHOUT_CORE_IMPORTS"] = "The core library was not imported!";
27
- LoaderErrors["NO_FULL_AND_NO_CORE_IMPORTS"] = "Highlight.js library was not imported!";
12
+ (function(LoaderErrors) {
13
+ LoaderErrors["FULL_WITH_CORE_LIBRARY_IMPORTS"] = "The full library and the core library were imported, only one of them should be imported!";
14
+ LoaderErrors["FULL_WITH_LANGUAGE_IMPORTS"] = "The highlighting languages were imported they are not needed!";
15
+ LoaderErrors["CORE_WITHOUT_LANGUAGE_IMPORTS"] = "The highlighting languages were not imported!";
16
+ LoaderErrors["LANGUAGE_WITHOUT_CORE_IMPORTS"] = "The core library was not imported!";
17
+ LoaderErrors["NO_FULL_AND_NO_CORE_IMPORTS"] = "Highlight.js library was not imported!";
28
18
  })(LoaderErrors || (LoaderErrors = {}));
29
-
30
- // @ts-nocheck
31
19
  const BREAK_LINE_REGEXP = /\r\n|\r|\n/g;
32
- /**
33
- * The contents of this file is from highlight.js, with a small modification to HTMLRenderer to make sure spans don't contain line breaks (important to improve performance of line numbers).
34
- */
35
- /** @typedef {{scope?: string, language?: string, children: Node[]} | string} Node */
36
- /** @typedef {{scope?: string, language?: string, children: Node[]} } DataNode */
37
- /** @typedef {import('highlight.js').Emitter} Emitter */
38
- /** */
39
- /** @returns */
40
20
  const newNode = (opts = {}) => {
41
- /** @type DataNode */
42
- const result = { children: [] };
43
- Object.assign(result, opts);
44
- return result;
21
+ const result = { children: [] };
22
+ Object.assign(result, opts);
23
+ return result;
45
24
  };
46
- class TokenTree {
47
- constructor() {
48
- /** @type DataNode */
49
- this.rootNode = newNode();
50
- this.stack = [this.rootNode];
51
- }
52
- get top() {
53
- return this.stack[this.stack.length - 1];
54
- }
55
- get root() {
56
- return this.rootNode;
57
- }
58
- /** @param node */
59
- add(node) {
60
- this.top.children.push(node);
61
- }
62
- /** @param scope */
63
- openNode(scope) {
64
- /** @type Node */
65
- const node = newNode({ scope });
66
- this.add(node);
67
- this.stack.push(node);
68
- }
69
- closeNode() {
70
- if (this.stack.length > 1) {
71
- return this.stack.pop();
72
- }
73
- return undefined;
74
- }
75
- closeAllNodes() {
76
- while (this.closeNode())
77
- ;
78
- }
79
- toJSON() {
80
- return JSON.stringify(this.rootNode, undefined, 4);
81
- }
82
- /**
83
- * @typedef { import("./html_renderer").Renderer } Renderer
84
- * @param builder
85
- */
86
- walk(builder) {
87
- // this does not
88
- return this.constructor._walk(builder, this.rootNode);
89
- // this works
90
- // return TokenTree._walk(builder, this.rootNode);
91
- }
92
- /**
93
- * @param builder
94
- * @param node
95
- */
96
- static _walk(builder, node) {
97
- if (typeof node === 'string') {
98
- builder.addText(node);
99
- }
100
- else if (node.children) {
101
- builder.openNode(node);
102
- node.children.forEach((child) => this._walk(builder, child));
103
- builder.closeNode(node);
104
- }
105
- return builder;
106
- }
107
- /**
108
- * @param node
109
- */
110
- static _collapse(node) {
111
- if (typeof node === 'string')
112
- return;
113
- if (!node.children)
114
- return;
115
- if (node.children.every(el => typeof el === 'string')) {
116
- // node.text = node.children.join("");
117
- // delete node.children;
118
- node.children = [node.children.join('')];
119
- }
120
- else {
121
- node.children.forEach((child) => {
122
- TokenTree._collapse(child);
123
- });
124
- }
125
- }
126
- }
127
- /**
128
- Currently this is all private API, but this is the minimal API necessary
129
- that an Emitter must implement to fully support the parser.
130
-
131
- Minimal interface:
132
-
133
- - addText(text)
134
- - __addSublanguage(emitter, subLanguageName)
135
- - startScope(scope)
136
- - endScope()
137
- - finalize()
138
- - toHTML()
139
-
140
- */
141
- /**
142
- * @implements {Emitter}
143
- */
144
- class TokenTreeEmitter extends TokenTree {
145
- /**
146
- * @param options
147
- */
148
- constructor(options) {
149
- super();
150
- this.options = options;
151
- }
152
- /**
153
- * @param text
154
- */
155
- addText(text) {
156
- if (text === '') {
157
- return;
158
- }
159
- this.add(text);
160
- }
161
- /** @param scope */
162
- startScope(scope) {
163
- this.openNode(scope);
164
- }
165
- endScope() {
166
- this.closeNode();
167
- }
168
- /**
169
- * @param emitter
170
- * @param name
171
- */
172
- __addSublanguage(emitter, name) {
173
- /** @type DataNode */
174
- const node = emitter.root;
175
- if (name)
176
- node.scope = `language:${name}`;
177
- this.add(node);
178
- }
179
- toHTML() {
180
- const renderer = new HTMLRenderer(this, this.options);
181
- return renderer.value();
182
- }
183
- finalize() {
184
- this.closeAllNodes();
185
- return true;
186
- }
187
- }
188
- /**
189
- * @typedef {object} Renderer
190
- * @property {(text: string) => void} addText
191
- * @property {(node: Node) => void} openNode
192
- * @property {(node: Node) => void} closeNode
193
- * @property {() => string} value
194
- */
195
- /** @typedef {{scope?: string, language?: string, sublanguage?: boolean}} Node */
196
- /** @typedef {{walk: (r: Renderer) => void}} Tree */
197
- /** */
198
- const SPAN_CLOSE = '</span>';
199
- /**
200
- * Determines if a node needs to be wrapped in <span>
201
- *
202
- * @param node */
203
- const emitsWrappingTags = (node) =>
204
- // rarely we can have a sublanguage where language is undefined
205
- // TODO: track down why
206
- !!node.scope;
207
- /**
208
- *
209
- * @param name
210
- * @param options
211
- */
25
+ var TokenTree = class TokenTree {
26
+ constructor() {
27
+ this.rootNode = newNode();
28
+ this.stack = [this.rootNode];
29
+ }
30
+ get top() {
31
+ return this.stack[this.stack.length - 1];
32
+ }
33
+ get root() {
34
+ return this.rootNode;
35
+ }
36
+ add(node) {
37
+ this.top.children.push(node);
38
+ }
39
+ openNode(scope) {
40
+ const node = newNode({ scope });
41
+ this.add(node);
42
+ this.stack.push(node);
43
+ }
44
+ closeNode() {
45
+ if (this.stack.length > 1) return this.stack.pop();
46
+ }
47
+ closeAllNodes() {
48
+ while (this.closeNode());
49
+ }
50
+ toJSON() {
51
+ return JSON.stringify(this.rootNode, void 0, 4);
52
+ }
53
+ walk(builder) {
54
+ return this.constructor._walk(builder, this.rootNode);
55
+ }
56
+ static _walk(builder, node) {
57
+ if (typeof node === "string") builder.addText(node);
58
+ else if (node.children) {
59
+ builder.openNode(node);
60
+ node.children.forEach((child) => this._walk(builder, child));
61
+ builder.closeNode(node);
62
+ }
63
+ return builder;
64
+ }
65
+ static _collapse(node) {
66
+ if (typeof node === "string") return;
67
+ if (!node.children) return;
68
+ if (node.children.every((el) => typeof el === "string")) node.children = [node.children.join("")];
69
+ else node.children.forEach((child) => {
70
+ TokenTree._collapse(child);
71
+ });
72
+ }
73
+ };
74
+ var TokenTreeEmitter = class extends TokenTree {
75
+ constructor(options) {
76
+ super();
77
+ this.options = options;
78
+ }
79
+ addText(text) {
80
+ if (text === "") return;
81
+ this.add(text);
82
+ }
83
+ startScope(scope) {
84
+ this.openNode(scope);
85
+ }
86
+ endScope() {
87
+ this.closeNode();
88
+ }
89
+ __addSublanguage(emitter, name) {
90
+ const node = emitter.root;
91
+ if (name) node.scope = `language:${name}`;
92
+ this.add(node);
93
+ }
94
+ toHTML() {
95
+ return new HTMLRenderer(this, this.options).value();
96
+ }
97
+ finalize() {
98
+ this.closeAllNodes();
99
+ return true;
100
+ }
101
+ };
102
+ const SPAN_CLOSE = "</span>";
103
+ const emitsWrappingTags = (node) => !!node.scope;
212
104
  const scopeToCSSClass = (name, { prefix }) => {
213
- // sub-language
214
- if (name.startsWith('language:')) {
215
- return name.replace('language:', 'language-');
216
- }
217
- // tiered scope: comment.line
218
- if (name.includes('.')) {
219
- const pieces = name.split('.');
220
- return [
221
- `${prefix}${pieces.shift()}`,
222
- ...(pieces.map((x, i) => `${x}${'_'.repeat(i + 1)}`))
223
- ].join(' ');
224
- }
225
- // simple scope
226
- return `${prefix}${name}`;
105
+ if (name.startsWith("language:")) return name.replace("language:", "language-");
106
+ if (name.includes(".")) {
107
+ const pieces = name.split(".");
108
+ return [`${prefix}${pieces.shift()}`, ...pieces.map((x, i) => `${x}${"_".repeat(i + 1)}`)].join(" ");
109
+ }
110
+ return `${prefix}${name}`;
111
+ };
112
+ var HTMLRenderer = class {
113
+ constructor(parseTree, options) {
114
+ this.buffer = "";
115
+ this.classPrefix = options.classPrefix;
116
+ this.openStack = [];
117
+ parseTree.walk(this);
118
+ }
119
+ addText(text) {
120
+ const lines = text.split(BREAK_LINE_REGEXP).map(escapeHTML);
121
+ const lineBreak = new Array(this.openStack.length).fill(SPAN_CLOSE).join("") + "\n" + this.openStack.join("");
122
+ this.buffer += lines.join(lineBreak);
123
+ }
124
+ openNode(node) {
125
+ if (!emitsWrappingTags(node)) return;
126
+ const className = scopeToCSSClass(node.scope, { prefix: this.classPrefix });
127
+ this.span(className);
128
+ }
129
+ closeNode(node) {
130
+ if (!emitsWrappingTags(node)) return;
131
+ this.buffer += SPAN_CLOSE;
132
+ this.openStack.pop();
133
+ }
134
+ value() {
135
+ return this.buffer;
136
+ }
137
+ span(className) {
138
+ const span = `<span class="${className}">`;
139
+ this.buffer += span;
140
+ this.openStack.push(span);
141
+ }
227
142
  };
228
- /** @type {Renderer} */
229
- class HTMLRenderer {
230
- /**
231
- * Creates a new HTMLRenderer
232
- *
233
- * @param parseTree - the parse tree (must support `walk` API)
234
- * @param options
235
- */
236
- constructor(parseTree, options) {
237
- this.buffer = '';
238
- this.classPrefix = options.classPrefix;
239
- this.openStack = [];
240
- parseTree.walk(this);
241
- }
242
- /**
243
- * Adds texts to the output stream
244
- *
245
- * @param text */
246
- addText(text) {
247
- const lines = text.split(BREAK_LINE_REGEXP).map(escapeHTML);
248
- const lineBreak = new Array(this.openStack.length).fill(SPAN_CLOSE).join('') + '\n' + this.openStack.join('');
249
- this.buffer += lines.join(lineBreak);
250
- }
251
- /**
252
- * Adds a node open to the output stream (if needed)
253
- *
254
- * @param node */
255
- openNode(node) {
256
- if (!emitsWrappingTags(node))
257
- return;
258
- const className = scopeToCSSClass(node.scope, { prefix: this.classPrefix });
259
- this.span(className);
260
- }
261
- /**
262
- * Adds a node close to the output stream (if needed)
263
- *
264
- * @param node */
265
- closeNode(node) {
266
- if (!emitsWrappingTags(node))
267
- return;
268
- this.buffer += SPAN_CLOSE;
269
- this.openStack.pop();
270
- }
271
- /**
272
- * returns the accumulated buffer
273
- */
274
- value() {
275
- return this.buffer;
276
- }
277
- // helpers
278
- /**
279
- * Builds a span element
280
- *
281
- * @param className */
282
- span(className) {
283
- const span = `<span class="${className}">`;
284
- this.buffer += span;
285
- this.openStack.push(span);
286
- }
287
- }
288
- /**
289
- * @param value
290
- * @returns
291
- */
292
143
  function escapeHTML(value) {
293
- return value
294
- .replace(/&/g, '&amp;')
295
- .replace(/</g, '&lt;')
296
- .replace(/>/g, '&gt;')
297
- .replace(/"/g, '&quot;')
298
- .replace(/'/g, '&#x27;');
299
- }
300
-
301
- class HighlightLoader {
302
- constructor() {
303
- this.document = inject(DOCUMENT);
304
- this.options = inject(NXT_HIGHLIGHT_OPTIONS, { optional: true });
305
- this.pendingTasks = inject(PendingTasks);
306
- this._t = this.pendingTasks.add();
307
- this.hljs = resource({
308
- loader: () => this.loadLibrary()
309
- .then(async (hljs) => {
310
- if (this.options?.lineNumbersLoader) {
311
- const plugin = await this.options.lineNumbersLoader();
312
- hljs.lineNumbersBlock = plugin.activateLineNumbers(this.document);
313
- }
314
- hljs.configure({
315
- __emitter: TokenTreeEmitter
316
- });
317
- if (this.options?.highlightOptions)
318
- hljs.configure(this.options.highlightOptions);
319
- return hljs;
320
- })
321
- .finally(this._t)
322
- }).asReadonly();
323
- if (this.options?.themePath) {
324
- this.loadTheme(this.options.themePath);
325
- }
326
- }
327
- /**
328
- * Lazy-Load highlight.js library
329
- */
330
- async loadLibrary() {
331
- if (this.options) {
332
- if (this.options.fullLibraryLoader && this.options.coreLibraryLoader) {
333
- throw new Error(LoaderErrors.FULL_WITH_CORE_LIBRARY_IMPORTS);
334
- }
335
- if (this.options.fullLibraryLoader && this.options.languages) {
336
- throw new Error(LoaderErrors.FULL_WITH_LANGUAGE_IMPORTS);
337
- }
338
- if (this.options.coreLibraryLoader && !this.options.languages) {
339
- throw new Error(LoaderErrors.CORE_WITHOUT_LANGUAGE_IMPORTS);
340
- }
341
- if (!this.options.coreLibraryLoader && this.options.languages) {
342
- throw new Error(LoaderErrors.LANGUAGE_WITHOUT_CORE_IMPORTS);
343
- }
344
- if (this.options.fullLibraryLoader) {
345
- return this.options.fullLibraryLoader();
346
- }
347
- if (this.options.coreLibraryLoader && this.options.languages && Object.keys(this.options.languages).length) {
348
- return this.options.coreLibraryLoader().then(hljs => this.loadLanguages(hljs));
349
- }
350
- }
351
- throw new Error(LoaderErrors.NO_FULL_AND_NO_CORE_IMPORTS);
352
- }
353
- /**
354
- * Lazy-load highlight.js languages
355
- */
356
- async loadLanguages(hljs) {
357
- await Promise.all(Object.entries(this.options?.languages || {})
358
- .map(([langName, langLoader]) => langLoader().then(langFunc => hljs.registerLanguage(langName, langFunc))));
359
- return hljs;
360
- }
361
- /**
362
- * Reload theme styles
363
- */
364
- setTheme(path) {
365
- if (this.themeLinkElement) {
366
- this.themeLinkElement.href = path;
367
- }
368
- else {
369
- this.loadTheme(path);
370
- }
371
- }
372
- /**
373
- * Load theme
374
- */
375
- loadTheme(path) {
376
- this.themeLinkElement = this.document.createElement('link');
377
- this.themeLinkElement.href = path;
378
- this.themeLinkElement.type = 'text/css';
379
- this.themeLinkElement.rel = 'stylesheet';
380
- this.themeLinkElement.media = 'screen,print';
381
- this.document.head.appendChild(this.themeLinkElement);
382
- }
383
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightLoader, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
384
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightLoader, providedIn: 'root' }); }
385
- }
386
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightLoader, decorators: [{
387
- type: Injectable,
388
- args: [{
389
- providedIn: 'root'
390
- }]
391
- }], ctorParameters: () => [] });
392
-
393
- class HighlightJS {
394
- constructor() {
395
- this.loader = inject(HighlightLoader);
396
- this.hljs = this.loader.hljs.value;
397
- }
398
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightJS, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
399
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightJS, providedIn: 'root' }); }
144
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;");
400
145
  }
401
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightJS, decorators: [{
402
- type: Injectable,
403
- args: [{
404
- providedIn: 'root'
405
- }]
406
- }] });
407
-
408
- /**
409
- * Enable usage of the library together with "trusted-types" HTTP Content-Security-Policy (CSP)
410
- *
411
- * Can be added to angular.json -> serve -> options -> headers to try it out in DEV mode
412
- * "Content-Security-Policy": "trusted-types nxt-highlightjs; require-trusted-types-for 'script'"
413
- *
414
- * Read more...
415
- * Angular Security: https://angular.io/guide/security#enforcing-trusted-types
416
- * Trusted Types: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
417
- *
418
- * @internal
419
- */
146
+ var HighlightLoader = class HighlightLoader {
147
+ constructor() {
148
+ this.document = inject(DOCUMENT);
149
+ this.options = inject(NXT_HIGHLIGHT_OPTIONS, { optional: true });
150
+ this.pendingTasks = inject(PendingTasks);
151
+ this._t = this.pendingTasks.add();
152
+ this.hljs = resource({ loader: () => this.loadLibrary().then(async (hljs) => {
153
+ if (this.options?.lineNumbersLoader) hljs.lineNumbersBlock = (await this.options.lineNumbersLoader()).activateLineNumbers(this.document);
154
+ hljs.configure({ __emitter: TokenTreeEmitter });
155
+ if (this.options?.highlightOptions) hljs.configure(this.options.highlightOptions);
156
+ return hljs;
157
+ }).finally(this._t) }).asReadonly();
158
+ if (this.options?.themePath) this.loadTheme(this.options.themePath);
159
+ }
160
+ async loadLibrary() {
161
+ if (this.options) {
162
+ if (this.options.fullLibraryLoader && this.options.coreLibraryLoader) throw new Error(LoaderErrors.FULL_WITH_CORE_LIBRARY_IMPORTS);
163
+ if (this.options.fullLibraryLoader && this.options.languages) throw new Error(LoaderErrors.FULL_WITH_LANGUAGE_IMPORTS);
164
+ if (this.options.coreLibraryLoader && !this.options.languages) throw new Error(LoaderErrors.CORE_WITHOUT_LANGUAGE_IMPORTS);
165
+ if (!this.options.coreLibraryLoader && this.options.languages) throw new Error(LoaderErrors.LANGUAGE_WITHOUT_CORE_IMPORTS);
166
+ if (this.options.fullLibraryLoader) return this.options.fullLibraryLoader();
167
+ if (this.options.coreLibraryLoader && this.options.languages && Object.keys(this.options.languages).length) return this.options.coreLibraryLoader().then((hljs) => this.loadLanguages(hljs));
168
+ }
169
+ throw new Error(LoaderErrors.NO_FULL_AND_NO_CORE_IMPORTS);
170
+ }
171
+ async loadLanguages(hljs) {
172
+ await Promise.all(Object.entries(this.options?.languages || {}).map(([langName, langLoader]) => langLoader().then((langFunc) => hljs.registerLanguage(langName, langFunc))));
173
+ return hljs;
174
+ }
175
+ setTheme(path) {
176
+ if (this.themeLinkElement) this.themeLinkElement.href = path;
177
+ else this.loadTheme(path);
178
+ }
179
+ loadTheme(path) {
180
+ this.themeLinkElement = this.document.createElement("link");
181
+ this.themeLinkElement.href = path;
182
+ this.themeLinkElement.type = "text/css";
183
+ this.themeLinkElement.rel = "stylesheet";
184
+ this.themeLinkElement.media = "screen,print";
185
+ this.document.head.appendChild(this.themeLinkElement);
186
+ }
187
+ static {
188
+ this.ɵfac = i0.ɵɵngDeclareFactory({
189
+ minVersion: "12.0.0",
190
+ version: "22.2.5",
191
+ ngImport: i0,
192
+ type: HighlightLoader,
193
+ deps: [],
194
+ target: i0.ɵɵFactoryTarget.Injectable
195
+ });
196
+ }
197
+ static {
198
+ this.ɵprov = i0.ɵɵngDeclareInjectable({
199
+ minVersion: "12.0.0",
200
+ version: "22.2.5",
201
+ ngImport: i0,
202
+ type: HighlightLoader,
203
+ providedIn: "root"
204
+ });
205
+ }
206
+ };
207
+ i0.ɵɵngDeclareClassMetadata({
208
+ minVersion: "12.0.0",
209
+ version: "22.2.5",
210
+ ngImport: i0,
211
+ type: HighlightLoader,
212
+ decorators: [{
213
+ type: Injectable,
214
+ args: [{ providedIn: "root" }]
215
+ }],
216
+ ctorParameters: () => []
217
+ });
218
+ var HighlightJS = class HighlightJS {
219
+ constructor() {
220
+ this.loader = inject(HighlightLoader);
221
+ this.hljs = this.loader.hljs.value;
222
+ }
223
+ static {
224
+ this.ɵfac = i0.ɵɵngDeclareFactory({
225
+ minVersion: "12.0.0",
226
+ version: "22.2.5",
227
+ ngImport: i0,
228
+ type: HighlightJS,
229
+ deps: [],
230
+ target: i0.ɵɵFactoryTarget.Injectable
231
+ });
232
+ }
233
+ static {
234
+ this.ɵprov = i0.ɵɵngDeclareInjectable({
235
+ minVersion: "12.0.0",
236
+ version: "22.2.5",
237
+ ngImport: i0,
238
+ type: HighlightJS,
239
+ providedIn: "root"
240
+ });
241
+ }
242
+ };
243
+ i0.ɵɵngDeclareClassMetadata({
244
+ minVersion: "12.0.0",
245
+ version: "22.2.5",
246
+ ngImport: i0,
247
+ type: HighlightJS,
248
+ decorators: [{
249
+ type: Injectable,
250
+ args: [{ providedIn: "root" }]
251
+ }]
252
+ });
420
253
  let policy;
421
- /**
422
- * @internal
423
- */
424
254
  function getPolicy() {
425
- if (!policy) {
426
- try {
427
- policy = window?.trustedTypes?.createPolicy('nxt-highlightjs', {
428
- createHTML: (s) => s
429
- });
430
- }
431
- catch {
432
- // trustedTypes.createPolicy throws if called with a name that is
433
- // already registered, even in report-only mode. Until the API changes,
434
- // catch the error not to break the applications functionally. In such
435
- // cases, the code will fall back to using strings.
436
- }
437
- }
438
- return policy;
255
+ if (!policy) try {
256
+ policy = window?.trustedTypes?.createPolicy("nxt-highlightjs", { createHTML: (s) => s });
257
+ } catch {}
258
+ return policy;
439
259
  }
440
- /**
441
- * @internal
442
- */
443
260
  function trustedHTMLFromStringBypass(html) {
444
- return getPolicy()?.createHTML(html) || html;
445
- }
446
-
447
- class HighlightBase {
448
- constructor() {
449
- this.hljs = inject(HighlightJS);
450
- this.nativeElement = inject((ElementRef)).nativeElement;
451
- this.sanitizer = inject(DomSanitizer);
452
- effect(() => {
453
- const code = this.code();
454
- if (code) {
455
- const res = this.highlightElement(code);
456
- if (res) {
457
- this.setInnerHTML(res?.value ?? undefined);
458
- this.highlightResult.set(res);
459
- // Forward highlight response to the highlighted output
460
- this.highlighted.emit(res);
461
- }
462
- }
463
- else {
464
- this.setTextContent('');
465
- }
466
- });
467
- }
468
- setTextContent(content) {
469
- this.nativeElement.textContent = content;
470
- }
471
- setInnerHTML(content) {
472
- this.nativeElement.innerHTML = trustedHTMLFromStringBypass(this.sanitizer.sanitize(SecurityContext.HTML, content || null) || '');
473
- }
474
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightBase, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
475
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "22.0.6", type: HighlightBase, isStandalone: true, ngImport: i0 }); }
476
- }
477
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightBase, decorators: [{
478
- type: Directive
479
- }], ctorParameters: () => [] });
480
-
481
- class HighlightAutoDirective extends HighlightBase {
482
- constructor() {
483
- super(...arguments);
484
- /**
485
- * Code to highlight
486
- */
487
- this.code = input(undefined, { ...(ngDevMode ? { debugName: "code" } : /* istanbul ignore next */ {}), alias: 'nxtHighlightAuto' });
488
- /**
489
- * Highlighted result
490
- */
491
- this.highlightResult = signal(undefined, /* @ts-ignore */
492
- ...(ngDevMode ? [{ debugName: "highlightResult" }] : /* istanbul ignore next */ []));
493
- /**
494
- * An optional array of language names and aliases restricting detection to only those languages
495
- *
496
- * The subset can also be set with configure, but the local parameter overrides the option if set.
497
- */
498
- this.languages = input(/* @ts-ignore */
499
- ...(ngDevMode ? [undefined, { debugName: "languages" }] : /* istanbul ignore next */ []));
500
- /**
501
- * Stream that emits when code string is highlighted
502
- */
503
- this.highlighted = output();
504
- }
505
- highlightElement(code) {
506
- const hljs = this.hljs.hljs();
507
- return hljs?.highlightAuto(code, this.languages());
508
- }
509
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightAutoDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); }
510
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.6", type: HighlightAutoDirective, isStandalone: true, selector: "[nxtHighlightAuto]", inputs: { code: { classPropertyName: "code", publicName: "nxtHighlightAuto", isSignal: true, isRequired: false, transformFunction: null }, languages: { classPropertyName: "languages", publicName: "languages", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { highlighted: "highlighted" }, host: { properties: { "class.hljs": "true" } }, providers: [{ provide: HighlightBase, useExisting: HighlightAutoDirective }], usesInheritance: true, ngImport: i0 }); }
261
+ return getPolicy()?.createHTML(html) || html;
511
262
  }
512
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightAutoDirective, decorators: [{
513
- type: Directive,
514
- args: [{
515
- selector: '[nxtHighlightAuto]',
516
- providers: [{ provide: HighlightBase, useExisting: HighlightAutoDirective }],
517
- host: {
518
- '[class.hljs]': 'true'
519
- }
520
- }]
521
- }], propDecorators: { code: [{ type: i0.Input, args: [{ isSignal: true, alias: "nxtHighlightAuto", required: false }] }], languages: [{ type: i0.Input, args: [{ isSignal: true, alias: "languages", required: false }] }], highlighted: [{ type: i0.Output, args: ["highlighted"] }] } });
522
-
523
- class HighlightDirective extends HighlightBase {
524
- constructor() {
525
- super(...arguments);
526
- /**
527
- * Code to highlight
528
- */
529
- this.code = input(undefined, { ...(ngDevMode ? { debugName: "code" } : /* istanbul ignore next */ {}), alias: 'nxtHighlight' });
530
- /**
531
- * Highlighted result
532
- */
533
- this.highlightResult = signal(undefined, /* @ts-ignore */
534
- ...(ngDevMode ? [{ debugName: "highlightResult" }] : /* istanbul ignore next */ []));
535
- /**
536
- * The language name highlight only one language
537
- */
538
- this.language = input.required(/* @ts-ignore */
539
- ...(ngDevMode ? [{ debugName: "language" }] : /* istanbul ignore next */ []));
540
- /**
541
- * An optional flag, when set to true it forces highlighting to finish even in case of detecting
542
- * illegal syntax for the language instead of throwing an exception
543
- */
544
- this.ignoreIllegals = input(undefined, { ...(ngDevMode ? { debugName: "ignoreIllegals" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
545
- /**
546
- * Stream that emits when code string is highlighted
547
- */
548
- this.highlighted = output();
549
- }
550
- highlightElement(code) {
551
- const hljs = this.hljs.hljs();
552
- return hljs?.highlight(code, {
553
- language: this.language(),
554
- ignoreIllegals: this.ignoreIllegals()
555
- });
556
- }
557
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive }); }
558
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.6", type: HighlightDirective, isStandalone: true, selector: "[nxtHighlight]", inputs: { code: { classPropertyName: "code", publicName: "nxtHighlight", isSignal: true, isRequired: false, transformFunction: null }, language: { classPropertyName: "language", publicName: "language", isSignal: true, isRequired: true, transformFunction: null }, ignoreIllegals: { classPropertyName: "ignoreIllegals", publicName: "ignoreIllegals", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { highlighted: "highlighted" }, host: { properties: { "class.hljs": "true" } }, providers: [{ provide: HighlightBase, useExisting: HighlightDirective }], usesInheritance: true, ngImport: i0 }); }
559
- }
560
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: HighlightDirective, decorators: [{
561
- type: Directive,
562
- args: [{
563
- selector: '[nxtHighlight]',
564
- providers: [{ provide: HighlightBase, useExisting: HighlightDirective }],
565
- host: {
566
- '[class.hljs]': 'true'
567
- }
568
- }]
569
- }], propDecorators: { code: [{ type: i0.Input, args: [{ isSignal: true, alias: "nxtHighlight", required: false }] }], language: [{ type: i0.Input, args: [{ isSignal: true, alias: "language", required: true }] }], ignoreIllegals: [{ type: i0.Input, args: [{ isSignal: true, alias: "ignoreIllegals", required: false }] }], highlighted: [{ type: i0.Output, args: ["highlighted"] }] } });
570
-
571
- /**
572
- * Generated bundle index. Do not edit.
573
- */
574
-
263
+ var HighlightBase = class HighlightBase {
264
+ constructor() {
265
+ this.hljs = inject(HighlightJS);
266
+ this.nativeElement = inject(ElementRef).nativeElement;
267
+ this.sanitizer = inject(DomSanitizer);
268
+ effect(() => {
269
+ const code = this.code();
270
+ if (!code) {
271
+ this.setTextContent("");
272
+ return;
273
+ }
274
+ if (!this.hljs.hljs()) {
275
+ if (this.nativeElement.textContent !== code) this.setTextContent(code);
276
+ return;
277
+ }
278
+ const res = this.highlightElement(code);
279
+ if (res) {
280
+ this.setInnerHTML(res.value);
281
+ this.highlightResult.set(res);
282
+ this.highlighted.emit(res);
283
+ } else {
284
+ this.setTextContent(code);
285
+ this.highlightResult.set(void 0);
286
+ }
287
+ });
288
+ }
289
+ setTextContent(content) {
290
+ this.nativeElement.textContent = content;
291
+ }
292
+ setInnerHTML(content) {
293
+ this.nativeElement.innerHTML = trustedHTMLFromStringBypass(this.sanitizer.sanitize(SecurityContext.HTML, content || null) || "");
294
+ }
295
+ static {
296
+ this.ɵfac = i0.ɵɵngDeclareFactory({
297
+ minVersion: "12.0.0",
298
+ version: "22.2.5",
299
+ ngImport: i0,
300
+ type: HighlightBase,
301
+ deps: [],
302
+ target: i0.ɵɵFactoryTarget.Directive
303
+ });
304
+ }
305
+ static {
306
+ this.ɵdir = i0.ɵɵngDeclareDirective({
307
+ minVersion: "14.0.0",
308
+ version: "22.2.5",
309
+ type: HighlightBase,
310
+ isStandalone: true,
311
+ ngImport: i0
312
+ });
313
+ }
314
+ };
315
+ i0.ɵɵngDeclareClassMetadata({
316
+ minVersion: "12.0.0",
317
+ version: "22.2.5",
318
+ ngImport: i0,
319
+ type: HighlightBase,
320
+ decorators: [{ type: Directive }],
321
+ ctorParameters: () => []
322
+ });
323
+ var HighlightAutoDirective = class HighlightAutoDirective extends HighlightBase {
324
+ constructor() {
325
+ super(...arguments);
326
+ this.code = input(void 0, {
327
+ ...ngDevMode ? { debugName: "code" } : /* istanbul ignore next */ {},
328
+ alias: "nxtHighlightAuto"
329
+ });
330
+ this.highlightResult = signal(void 0, ...ngDevMode ? [{ debugName: "highlightResult" }] : /* istanbul ignore next */ []);
331
+ this.languages = input(...ngDevMode ? [void 0, { debugName: "languages" }] : /* istanbul ignore next */ []);
332
+ this.highlighted = output();
333
+ }
334
+ highlightElement(code) {
335
+ return this.hljs.hljs()?.highlightAuto(code, this.languages());
336
+ }
337
+ static {
338
+ this.ɵfac = i0.ɵɵngDeclareFactory({
339
+ minVersion: "12.0.0",
340
+ version: "22.2.5",
341
+ ngImport: i0,
342
+ type: HighlightAutoDirective,
343
+ deps: null,
344
+ target: i0.ɵɵFactoryTarget.Directive
345
+ });
346
+ }
347
+ static {
348
+ this.ɵdir = i0.ɵɵngDeclareDirective({
349
+ minVersion: "17.1.0",
350
+ version: "22.2.5",
351
+ type: HighlightAutoDirective,
352
+ isStandalone: true,
353
+ selector: "[nxtHighlightAuto]",
354
+ inputs: {
355
+ code: {
356
+ classPropertyName: "code",
357
+ publicName: "nxtHighlightAuto",
358
+ isSignal: true,
359
+ isRequired: false,
360
+ transformFunction: null
361
+ },
362
+ languages: {
363
+ classPropertyName: "languages",
364
+ publicName: "languages",
365
+ isSignal: true,
366
+ isRequired: false,
367
+ transformFunction: null
368
+ }
369
+ },
370
+ outputs: { highlighted: "highlighted" },
371
+ host: { properties: { "class.hljs": "true" } },
372
+ providers: [{
373
+ provide: HighlightBase,
374
+ useExisting: HighlightAutoDirective
375
+ }],
376
+ usesInheritance: true,
377
+ ngImport: i0
378
+ });
379
+ }
380
+ };
381
+ i0.ɵɵngDeclareClassMetadata({
382
+ minVersion: "12.0.0",
383
+ version: "22.2.5",
384
+ ngImport: i0,
385
+ type: HighlightAutoDirective,
386
+ decorators: [{
387
+ type: Directive,
388
+ args: [{
389
+ selector: "[nxtHighlightAuto]",
390
+ providers: [{
391
+ provide: HighlightBase,
392
+ useExisting: HighlightAutoDirective
393
+ }],
394
+ host: { "[class.hljs]": "true" }
395
+ }]
396
+ }],
397
+ propDecorators: {
398
+ code: [{
399
+ type: i0.Input,
400
+ args: [{
401
+ isSignal: true,
402
+ alias: "nxtHighlightAuto",
403
+ required: false
404
+ }]
405
+ }],
406
+ languages: [{
407
+ type: i0.Input,
408
+ args: [{
409
+ isSignal: true,
410
+ alias: "languages",
411
+ required: false
412
+ }]
413
+ }],
414
+ highlighted: [{
415
+ type: i0.Output,
416
+ args: ["highlighted"]
417
+ }]
418
+ }
419
+ });
420
+ var HighlightDirective = class HighlightDirective extends HighlightBase {
421
+ constructor() {
422
+ super(...arguments);
423
+ this.code = input(void 0, {
424
+ ...ngDevMode ? { debugName: "code" } : /* istanbul ignore next */ {},
425
+ alias: "nxtHighlight"
426
+ });
427
+ this.highlightResult = signal(void 0, ...ngDevMode ? [{ debugName: "highlightResult" }] : /* istanbul ignore next */ []);
428
+ this.language = input(...ngDevMode ? [void 0, { debugName: "language" }] : /* istanbul ignore next */ []);
429
+ this.ignoreIllegals = input(void 0, {
430
+ ...ngDevMode ? { debugName: "ignoreIllegals" } : /* istanbul ignore next */ {},
431
+ transform: booleanAttribute
432
+ });
433
+ this.highlighted = output();
434
+ }
435
+ highlightElement(code) {
436
+ const hljs = this.hljs.hljs();
437
+ const language = this.language();
438
+ if (!language || !hljs?.getLanguage(language)) return;
439
+ return hljs.highlight(code, {
440
+ language,
441
+ ignoreIllegals: this.ignoreIllegals()
442
+ });
443
+ }
444
+ static {
445
+ this.ɵfac = i0.ɵɵngDeclareFactory({
446
+ minVersion: "12.0.0",
447
+ version: "22.2.5",
448
+ ngImport: i0,
449
+ type: HighlightDirective,
450
+ deps: null,
451
+ target: i0.ɵɵFactoryTarget.Directive
452
+ });
453
+ }
454
+ static {
455
+ this.ɵdir = i0.ɵɵngDeclareDirective({
456
+ minVersion: "17.1.0",
457
+ version: "22.2.5",
458
+ type: HighlightDirective,
459
+ isStandalone: true,
460
+ selector: "[nxtHighlight]",
461
+ inputs: {
462
+ code: {
463
+ classPropertyName: "code",
464
+ publicName: "nxtHighlight",
465
+ isSignal: true,
466
+ isRequired: false,
467
+ transformFunction: null
468
+ },
469
+ language: {
470
+ classPropertyName: "language",
471
+ publicName: "language",
472
+ isSignal: true,
473
+ isRequired: false,
474
+ transformFunction: null
475
+ },
476
+ ignoreIllegals: {
477
+ classPropertyName: "ignoreIllegals",
478
+ publicName: "ignoreIllegals",
479
+ isSignal: true,
480
+ isRequired: false,
481
+ transformFunction: null
482
+ }
483
+ },
484
+ outputs: { highlighted: "highlighted" },
485
+ host: { properties: { "class.hljs": "true" } },
486
+ providers: [{
487
+ provide: HighlightBase,
488
+ useExisting: HighlightDirective
489
+ }],
490
+ usesInheritance: true,
491
+ ngImport: i0
492
+ });
493
+ }
494
+ };
495
+ i0.ɵɵngDeclareClassMetadata({
496
+ minVersion: "12.0.0",
497
+ version: "22.2.5",
498
+ ngImport: i0,
499
+ type: HighlightDirective,
500
+ decorators: [{
501
+ type: Directive,
502
+ args: [{
503
+ selector: "[nxtHighlight]",
504
+ providers: [{
505
+ provide: HighlightBase,
506
+ useExisting: HighlightDirective
507
+ }],
508
+ host: { "[class.hljs]": "true" }
509
+ }]
510
+ }],
511
+ propDecorators: {
512
+ code: [{
513
+ type: i0.Input,
514
+ args: [{
515
+ isSignal: true,
516
+ alias: "nxtHighlight",
517
+ required: false
518
+ }]
519
+ }],
520
+ language: [{
521
+ type: i0.Input,
522
+ args: [{
523
+ isSignal: true,
524
+ alias: "language",
525
+ required: false
526
+ }]
527
+ }],
528
+ ignoreIllegals: [{
529
+ type: i0.Input,
530
+ args: [{
531
+ isSignal: true,
532
+ alias: "ignoreIllegals",
533
+ required: false
534
+ }]
535
+ }],
536
+ highlighted: [{
537
+ type: i0.Output,
538
+ args: ["highlighted"]
539
+ }]
540
+ }
541
+ });
575
542
  export { HighlightAutoDirective, HighlightBase, HighlightDirective, HighlightJS, HighlightLoader, NXT_HIGHLIGHT_OPTIONS, provideHighlightOptions };
576
- //# sourceMappingURL=nxt-highlightjs.mjs.map
543
+
544
+ //# sourceMappingURL=nxt-highlightjs.mjs.map