PrismJS / PrismJS/prism

Prism should resolve aliases for hooks

Open
#2,000 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

core enhancement
Dominant language
JavaScript
Stars
13k
Forks
1.4k
Avg merge
15h 36m
Merged PRs (30d)
3

Description

Motivation
When adding language-specific hooks the first line of the callback function is usually the language check. The problem here is that every language-specific hook has to know all the aliases of the target language.
For languages like Markup, this is quite a number of even for languages with just one alias this can cause problems.

Description
To solve this, we could add a function like this:

Prism.languages.addAlias = function (id, ...aliases) {
	for (const alias of aliases) {
		aliasMap[alias] = id;
		Prism.languages[alias] = Prism.languages[id];
	}
}

aliasMap is just a private variable which does what the name suggests. We would then use the map here and here to resolve the aliases for our hooks.

The class name of highlighted elements remains unchanged. (Or should we resolve this as well?)

Alternative
We could also use the order of the entries in Prism.languages. Aliases are always added after the language definition. This means that we can resolve aliases by looking for the first key in Prism.languages with the same value.
A bit hacky but we wouldn't have to change any of the existing language definitions.

Edit: Added alternative

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading components/prism-core.js at the two linked hook locations and the proposed Prism.languages.addAlias API. Trace how language aliases are currently represented, then resolve the open question of whether highlighted class names remain unchanged. Done means language-specific hooks consistently resolve aliases, with the chosen behavior documented and validated.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.