Skip to content

Commit 9e2008c

Browse files
committed
feat: import core of highlightjs without all languages
When import main package (`'highlight.js'`), all avalilable languages are loaded automatically. To import only core we need to import from `'highlight.js/lib/core'` For more information see docs: https://highlightjs.org/#as-a-module
1 parent 57b9f16 commit 9e2008c

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/transform/highlight.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
import type {HLJSApi} from 'highlight.js';
12
import {escapeHtml} from 'markdown-it/lib/common/utils';
2-
import {HighlightLangMap} from './typings';
3+
import type {HighlightLangMap} from './typings';
34

45
export = function makeHighlight(langs: HighlightLangMap = {}) {
56
try {
67
// Important require.
78
// Because we want to have a posibility to run in projects without hljs dependency
8-
const hljs = require('highlight.js');
9+
const hljs: HLJSApi = require('highlight.js/lib/core');
910

1011
Object.keys(langs).forEach((lang) => {
1112
hljs.registerLanguage(lang, langs[lang]);
1213
});
1314

1415
return function highlight(str: string, lang: string) {
15-
let highlightedStr;
16+
let highlightedStr: string | undefined;
1617
const classNames = ['hljs'];
1718

1819
if (lang && hljs.getLanguage(lang)) {

src/transform/typings.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {LanguageFn} from 'highlight.js';
2-
import DefaultMarkdownIt from 'markdown-it';
3-
import DefaultStateCore from 'markdown-it/lib/rules_core/state_core';
4-
import {SanitizeOptions} from './sanitize';
5-
import {MarkdownItPluginCb} from './plugins/typings';
6-
import {LogLevels} from './log';
7-
import {ChangelogItem} from './plugins/changelog/types';
1+
import type {LanguageFn} from 'highlight.js';
2+
import type DefaultMarkdownIt from 'markdown-it';
3+
import type DefaultStateCore from 'markdown-it/lib/rules_core/state_core';
4+
import type {SanitizeOptions} from './sanitize';
5+
import type {MarkdownItPluginCb} from './plugins/typings';
6+
import type {LogLevels} from './log';
7+
import type {ChangelogItem} from './plugins/changelog/types';
88

99
export interface MarkdownIt extends DefaultMarkdownIt {
1010
assets?: string[];

test/highlight-code.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {dirname} from 'path';
1+
import {dirname} from 'node:path';
2+
import typescript from 'highlight.js/lib/languages/typescript';
23
import transform from '../src/transform';
34

45
const mocksPath = require.resolve('./utils.ts');
@@ -7,6 +8,7 @@ const transformYfm = (text: string) => {
78
result: {html},
89
} = transform(text, {
910
plugins: [],
11+
highlightLangs: {ts: typescript},
1012
path: mocksPath,
1113
root: dirname(mocksPath),
1214
});

0 commit comments

Comments
 (0)