Skip to content

[Enhancement request] Allow importing named language imports #3470

@webketje

Description

@webketje

Is your request related to a specific problem you're having?
When using a bundler on the front-end it is important to only include the required languages to reduce bundle size,
but it feels a bit clunky to register languages like this:

import hljs from 'highlight.js/lib/core';
import js from 'highlight.js/lib/languages/javascript';
import json from 'highlight.js/lib/languages/json';
import yml from 'highlight.js/lib/languages/yaml';
import md from 'highlight.js/lib/languages/markdown';

hljs.registerLanguage('javascript', js);
hljs.registerLanguage('json', json);
hljs.registerLanguage('yaml', yml);
hljs.registerLanguage('markdown', md);

The solution you'd prefer / feature you'd like to see added...
It would be much better if it could just be done like this:

import hljs from 'highlight.js/lib/core';
import { javascript, json, yaml, markdown } from 'highlight.js/lib/languages';

hljs.registerLanguage('javascript', javascript);
hljs.registerLanguage('json', json);
hljs.registerLanguage('yaml', yaml);
hljs.registerLanguage('markdown', markdown);

This only requires adding lib/languages/index.js which has lines like:

export { default as json } from './json';
export { default as javascript } from './javascript';
// aliases could be done like this:
export { default as js } from './javascript';

I could make a PR for this (please mention whether aliasing is desirable, I believe this could also be implemented in client code as import { javascript as js } from 'highlight.js/languages')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions