-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Make it easier to consume Prism language definitions from Node #768
Comments
You can currently use this: var Prism = require('./components/prism-core.js');
require('./components/prism-markup.js');
require('./components/prism-markdown.js');
console.log(Prism.highlight('_foo_', Prism.languages.markdown, 'markdown'));
// Outputs:
// <span class="token italic" ><span class="token punctuation" >_</span>foo<span class="token punctuation" >_</span></span> The biggest flaw is that you need to handle the dependencies by yourself. |
Yeah. That is exactly what I wanted to avoid, hence the wrapper. I can just do a lookup against it when highlighting and that's it. 👍 |
If I get it right, currently your wrapper is just a concatenation of all components, isn't it? If that is the case, couldn't you build a version of Prism with all languages included and require it? You could then access the definitions using |
Yeah, that would work. I would still need to publish the result through npm given I consume this as a package dependency (highlighting plugin for a static site generator). |
I'm not sure I understand well what you expect us to do. Should we automatically build a full-featured version of Prism, with all languages included? |
Here's the problem: > prism = require('prismjs')
...
> Object.keys(prism.languages)
[ 'extend',
'insertBefore',
'DFS',
'markup',
'css',
'clike',
'javascript' ] The current distribution provides only a subset of languages through package main making it tough to consume. As you mentioned, a consumer could just require every language. That's not ideal. It would be far better if the listing above would contain all language definitions. That would render by package obsolete (good thing). Regarding #578, that might be a good idea. Just having an official npm package with the definitions in an easy to consume format would do the trick. |
Ok, so the steps described in my previous comments should do what you want. I agree that the current published package is far from ideal, as it is actually the build used for the website. I'll try to dive into #578 soon. |
Cool, let me know if I can be of any help. Feel free to reuse my script thinger as you want. It just concats the files, deals with the global and exposes the language definitions. It's possible to do something more sophisticated and generate a separate entry point in addition to a CommonJS wrapper for each. In fact you can push one more step forward from that and go UMD. If you need a library for that, maybe libumd would work. That would make it really easy to consume individual definitions from global/AMD/CommonJS environments. |
Using UMD has been suggested too, but it would require many changes so I won't do it for now. BTW, would this require publishing an npm package for each and every language definition, separately from the core? |
@Golmote Publishing a package for each and every language definition might be a little much. I would be happy with a single package that's easy to consume in various ways. Just having a nice entry point through Node would do for me personally. |
The plugins are handled via hooks and they are called from inside Any ideas on how to use plugins on node? |
Which plugins are you trying to use? |
prismjs/plugins/line-numbers/prism-line-numbers To better explain what I am trying to do, I have some documentation as markdown and build a static site with some custom code. I can pass a My highlight function now looks like this var Prism = require('prismjs/components/prism-core');
require('prismjs/components/prism-jsx');
...
...
highlight: function (str, lang) {
var grammar = lang !== undefined ? Prism.languages[lang] : Prism.languages.markup;
return Prism.highlight(str, grammar, lang);
} the line-number plugin does not expose a function it only adds a hook, and this function also takes an env object which has a dom as for I have some ideas where markdown will send me the line range as part of the |
Like you said, both plugins are made to be used in the DOM, not directly from node. |
@bebraw I notice you've marked |
@glenjamin Bummer. Two options. We can fix this on Prism side (preferable) or I can undeprecate the package. I am not maintaining it actively as I don't need it anymore. Of course if you are interested, I could transfer the package to you and you could take over. It's bit of a hack, but the definitions are there. |
I think it'd be neat if there was a I'll probably try doing something similar in my build for now. |
Yeah. |
I've encountered the same problem. Would be great if this issue could be resolved. Currently as a workaround I'm using the global scope instead of the imported Prism instance. |
See #972 (comment) for an easy way to load language definitions in Node. |
As I want to use Prism for some backend processing, I would be very interested in consuming it entirely through Node. The problem is that the entry point of the project doesn't give direct access to the language definitions. In addition the definitions seem to attach to a global. It would be great if the Node version was easier to consume.
To work around this, I set up a little wrapper. If you could provide an interface like that for Node, that would work for me.
The text was updated successfully, but these errors were encountered: