Code Monkey home page Code Monkey logo

Comments (12)

LeaVerou avatar LeaVerou commented on August 18, 2024

No, as I have no idea how to do that :P
But if you want to help, that would be welcome :)

from prism.

mathiasbynens avatar mathiasbynens commented on August 18, 2024

@jzaefferer How would that work exactly, given Prism.js heavily relies on (non-ECMAScript) DOM extensions for most of its core methods?

from prism.

jzaefferer avatar jzaefferer commented on August 18, 2024

@mathiasbynens no idea, haven't looked at the code yet, but that sounds like it would become a problem. Though jsdom, or even better yet, cheeriosjs, might help there: https://github.com/MatthewMueller/cheerio

from prism.

LeaVerou avatar LeaVerou commented on August 18, 2024

@mathiasbynens The heart of Prism is Prism.highlight(), Prism.tokenize() and Prism.Token.stringify(). Neither of those use DOM methods.

from prism.

mathiasbynens avatar mathiasbynens commented on August 18, 2024

@LeaVerou Oh, that’s good news, then!

from prism.

tbranyen avatar tbranyen commented on August 18, 2024

https://github.com/LeaVerou/prism/blob/gh-pages/prism.js#L300

would need to be changed before this script could run in node without a ReferenceError

from prism.

jzaefferer avatar jzaefferer commented on August 18, 2024

Also usage of self would need to be replaced. Within the node context, module.export = Prism would be the right way to expose that object.

from prism.

buymeasoda avatar buymeasoda commented on August 18, 2024

Hi folks,

I've done some work to adapt the current prism.js code to work within a node context, as well as the browser.

To see the changes involved, have a look at the following branch / diff for the details:

Diff: https://github.com/buymeasoda/prism/compare/npm-node-support
Branch: https://github.com/buymeasoda/prism/tree/npm-node-support

This version allows you to do the following:

$ node

> var prism = require('./prism.js');
> prism.highlight('var test = "my string";', prism.languages.javascript);

'<span class="token keyword" >var</span> test <span class="token operator" >=</span> <span class="token string" >"my string"</span><span class="token punctuation" >;</span>'

I've included a sample package.json file for publishing to npm and will add a pull request to give you some more visibility of the merge results.

Although this adaption is working fine for providing require support, I think the node / npm side of things could be improved further with some changes to the way the base project files are organised.

The plugins that work in a node (non-browser) context, have also had a couple of minor changes to allow them to work. For example, Show Invisibles and Autolinker are both usable scripts in a node context.

Let me know what you think and if there's anything else I can do to help.

from prism.

buymeasoda avatar buymeasoda commented on August 18, 2024

Based on feedback from Lea, I've created a new version of this pull request that includes the bare minimum needed to get Prism functioning in node.

The pull request is: #42

from prism.

buymeasoda avatar buymeasoda commented on August 18, 2024

Based on further discussion, a new version has been submitted that creates a dedicated node version of prism.js.

The pull request is: #45

from prism.

adam-lynch avatar adam-lynch commented on August 18, 2024

This can be closed since #179 was merged, right?

from prism.

LeaVerou avatar LeaVerou commented on August 18, 2024

I think so :)

from prism.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.