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

Importing all languages in node.js #972

Closed
felixsanz opened this issue Jun 11, 2016 · 16 comments
Closed

Importing all languages in node.js #972

felixsanz opened this issue Jun 11, 2016 · 16 comments

Comments

@felixsanz
Copy link

This is related to: #593

The components are injected when you require the module, so can we have (for the node users) an index.js inside components folder, that does this?

require './prism-abap.js';
require './prism-actionscript.js';
require './prism-apacheconf.js';
// ...

So then we only have to use:

import prismjs from 'prismjs';
import 'prismjs/components';

Or even something like:

function loadComponents(arr) {
  arr.foreach(function(language) {
    require('./prism-' + language);
  });
}

module.exports = loadComponents;
import prismjs from 'prismjs';
import loadComponents from 'prismjs/components';
loadComponents(['php', 'ruby']);

This won't work in the browser i think, but why not have this simple helper for the node users?

@LeaVerou
Copy link
Member

It would be nice to have this indeed, and it can be automatically generated via gulp…

@Golmote
Copy link
Contributor

Golmote commented Jul 3, 2016

I digged into this last night, and the loadComponent() option works great.

It might even be able to handle dependencies by reading the components.js file. But here is the problem: the components.js file can not be require()'d in Node.js without a module.exports at the end. This would break any script that tried to parse the components.js file (like we currently do in the gulpfile).
Is that a BC break?
Should we add the module.exports line to the components.js to require() it or parse the file?
Should we handle dependencies at all in this helper?

@LeaVerou
Copy link
Member

Would it help if we split components.js to a components.json which can be easily parsed and then create components.js + module.exports with gulp?

@Golmote
Copy link
Contributor

Golmote commented Sep 1, 2016

@LeaVerou Indeed that would be nice! But shouldn't it be considered a BC break anyway, since people relying on parsing the components.js file would have to change to components.json?

@LeaVerou
Copy link
Member

LeaVerou commented Sep 6, 2016

No, because components.js would still exist, it would just be generated with gulp! :)

Also I don't think that many people are relying on components.js :P

@mAAdhaTTah
Copy link
Member

@LeaVerou I'm working on an integration that might require access to a component.js{on}. I'm bundling with webpack and am looking to use its async loading to load up the languages & their dependencies, rather than using the autoloader, as this would simplify my build setup and allow me to run everything through webpack (including the CSS, which is pretty useful).

Currently, the component.js is not distributed on npm, which is what I was planning on using to determine the language dependencies and load them at runtime. Converting it to a JSON would be helpful for me, as the current, undistributed component.js is not require-able as-is. Any chance that update could be made? Would a PR for that be accepted? And does the original component.js still need to be generated?

@scottbedard
Copy link

Are there any updates on this? Being able to pull in additional languages via npm would be great!

@KyleAMathews
Copy link

This pattern works well #593 (comment)

@sumitsahu28
Copy link

sumitsahu28 commented Aug 22, 2017

i am using prismjs highlighter in nodejs, able to highlight the code but it fails to give line number although i have added the line number plugin and line-numbers class to the target <pre> element.
Please suggest a method to implement line number via prismjs in nodejs.

@Golmote
Copy link
Contributor

Golmote commented Mar 20, 2018

Ok, we added a loadLanguages() function that can be used as suggested here:

var Prism = require('./components/prism-core.js');
var loadLanguages = require('./components/index.js');
loadLanguages(['php', 'ruby']);

Prism.highlight(...);

It will load the required dependencies.

I still need to document this somewhere. I'll close the issue once it's done.

Shivang44 added a commit to Shivang44/prism that referenced this issue Jul 6, 2018
Requiring components without the "index.js" part and calling "loadLanguages" results in a function not found error for me. Adding index.js, as commented by @Golmote, fixes that error.

Ref: PrismJS#972
Golmote pushed a commit that referenced this issue Jul 6, 2018
* Updated index.html to fix broken instructions

Requiring components without the "index.js" part and calling "loadLanguages" results in a function not found error for me. Adding index.js, as commented by @Golmote, fixes that error.

Ref: #972

* Updated index.html to fix nodejs instructions

Fixed the require so that nodejs does not give an error when requiring the loadLanguages function.
@muuvmuuv
Copy link

@Golmote Could you add the link to the documentation?

@gauntface
Copy link

gauntface commented Oct 11, 2018

Struggling to get this to work in Typescript as well.

Update: Could get Typescript to work on Node using require like so:

import * as prism from 'prismjs';

const loadLanguages = require('prismjs/components/index');
loadLanguages(['lang-1', 'lang-2]);

@bradymholt
Copy link

I found that if you don't specify an array of languages, all of them will be loaded.

In TypeScript, I was able to load all languages using this syntax:

import * as prism from 'prismjs';
require("prismjs/components/")();

@rodrigoslayertech
Copy link

rodrigoslayertech commented Apr 5, 2023

You can easily import it in NodeJS:

import Prism from 'prismjs'
// @ Load Prism languages
import 'prismjs/components/prism-markup-templating' // dependency for prism-php extension
// PHP
import 'prismjs/components/prism-php'

@BlackBearFTW
Copy link

Could this issue be reopened?

I would still like to see a language loader method similar to prismjs

@LeaVerou
Copy link
Member

I believe this will be a lot easier in v2, which means there's less motivation to fix it in v1.

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