Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatically check if a language is supported by Prism (including aliases) #2522

Closed
emmerich opened this issue Aug 17, 2020 · 4 comments
Closed

Comments

@emmerich
Copy link

I want to be able to programmatically check if a language is supported by Prism. I could do this:

typeof Prism.languages[myLang] !== 'undefined';

but this doesn't support aliases.

I've currently implemented this myself by iterating over Prism.languages and building a lookup map with support for alias, but I think this could be a useful part of Prism itself.

  1. Is this already possible in Prism by some means I'm not aware of?
  2. Would you be interested in this feature? I'd be happy to submit a PR if needed.
@RunDevelopment
Copy link
Member

Prism.languages also contains a few functions (for creating and modifying languages). You can list all currently loaded languages plus their aliases like this:

Object.keys(Prism.languages).filter(id => typeof Prism.languages[id] === "object")

If you want all available languages, you will need to use components.{js,json}. It contains a map of all languages Prism supports with their titles and aliases.
However, both files are not part of the public API and may change (breakingly) from version to version (but they probably won't, given that they haven't been changed much in the past years.)

That being said, what exactly do need Prism to be able to do?

but this doesn't support aliases.

I don't understand what you mean by that. E.g. Javascript will be in Prism.languages as javascript and as its alias js.

building a lookup map with support for alias

How do you do that? We have a similar problem in #2000, so I'm interested in your approach.

@emmerich
Copy link
Author

I see what you mean - the problem here is that Prism.languages contains the languages currently loaded into Prism. We're dynamically loading the language (for example, when a user says "here's a code block containing applescript, we dynamically load applescript).

So I guess the question is not "which languages are supported and currently loaded into Prism?" (for which Prism.languages is good) but "which languages are supported by Prism regardless of whether they're loaded?"

How do you do that? We have a similar problem in #2000, so I'm interested in your approach.

Here's a codesandbox showing what I mean: https://codesandbox.io/s/practical-cray-bruxb?file=/src/index.js

Hope this clears the usecase up a bit!

@RunDevelopment
Copy link
Member

which languages are supported by Prism regardless of whether they're loaded?

In that case, you definitely have to use components.{js,json}. They aren't part of the public API but unlikely to change much, so it's probably ok to use them.
I'm sorry this isn't a very satisfying answer.

We are planning to open up parts of them (#2146) eventually but I'm still thinking about the best way to do that.

Here's a code sandbox showing what I mean

Ahh, so you didn't resolve the aliases, you just wanted to know all available ones.

@emmerich
Copy link
Author

In that case, you definitely have to use components.{js,json}. They aren't part of the public API but unlikely to change much, so it's probably ok to use them.
I'm sorry this isn't a very satisfying answer.

It's fine - I was just concerned that I had missed some method that already existed.

We are planning to open up parts of them (#2146) eventually but I'm still thinking about the best way to do that.

Great!

Ahh, so you didn't resolve the aliases, you just wanted to know all available ones.

Exactly. Our use case is actually around importing content - we need to validate that imported code blocks have a language supported by Prism, otherwise we strip the language. So we need to know the available languages (we don't care if they're aliases or not), but not load them/resolve them.

Thanks for the help, as you're already working on this as part of #2146 I don't think it makes sense to figure out a way of putting what I've done into the Prism codebase, we'll wait for an upcoming release. Feel free to ping me if you need help on the matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants