question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Build prism.js with support of AMD environments

See original GitHub issue

It would be nice that Prism supports AMD/UMD environments. Now I use the following gulp script to wrap prism.js into AMD module.

var gulp = require('gulp');
var fs = require('fs');
var path = require('path');
var _ = require("lodash");

var header = require('gulp-header');
var wrapper = require('gulp-wrapper');
var concat = require('gulp-concat');
var git = require('gulp-git');
var runSequence = require('run-sequence');

var prism_outdir = './lib/prism';

// task to build prism package
gulp.task("build-prism", function() {
  return runSequence("clone-prism", ["build-prism.css", "build-prism.js"]);
});

gulp.task("clone-prism", function(done) {
  if (fs.existsSync("prism")) {
    done();
  } else {
    git.clone("https://github.com/LeaVerou/prism", function(err) {
      if (err) throw err;
      done();
    });
  }
});

gulp.task("build-prism.css", function() {
  // lets try okaidia.css
  return gulp.src("prism/themes/prism.css")
    .pipe(rename("prism.css"))
    .pipe(gulp.dest(prism_outdir));
});

gulp.task("build-prism.js", function() {
  // make prism/components.js to be a module so we could require it
  var src = fs.readFileSync("prism/components.js", "utf8");
  src += "\nmodule.exports = components;\n";
  var out = "./prism-components.js";
  fs.writeFileSync(out, src, "utf8");
  var components = require(out);
  fs.unlinkSync(out);

  // replaces {id} tokens
  function replace(format, data) {
    return format.replace(/{(\w+)}/g, function(m, name) {
      return data[name] ? data[name] : "";
    });
  }

  // exclude languages that we won't use
  var excludedLanguages = [];

  // TODO add plugins
  var glob = [components.core.meta.path]
  .concat(
    _.pairs(components.languages)
      .filter(function(p) {
        var k = p[0].toLowerCase();
        return k != "meta" && excludedLanguages.indexOf(k) < 0;
      })
      .map(function(p) {
        return replace(components.languages.meta.path, {
          id: p[0]
        }) + ".js";
      })
  ).map(function(p) {
    return path.join("prism", p);
  });

  var exportFooter = 
    "if (typeof define === 'function' && define.amd) {\n"
    + "\tdefine(function() { return prism({}, typeof window !== 'undefined' ? window : global); });\n"
    + "} else {\n"
    + "\tvar w = typeof window !== 'undefined' ? window : global;\n"
    + "\tprism(this || w, w);\n" // browser export
    + "}\n";

  return gulp.src(glob)
      .pipe(header('\n/* **********************************************\n' +
        '     Begin <%= file.relative %>\n' +
        '********************************************** */\n\n'))
      .pipe(concat('prism.js'))
      .pipe(wrapper({
        // wrap prism code into function
        header: "var prism = function (self, window) {\n",
        footer: "\n\nreturn Prism;\n};\n\n" + exportFooter
      }))
      .pipe(gulp.dest(prism_outdir))
});

I could try to PR if you want.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
davidkuderacommented, Aug 23, 2015

👍

0reactions
mAAdhaTTahcommented, May 26, 2018

I don’t think we’re going to support AMD. As mentioned, this is a big change, so we’re just going to still w/ the globals we’ve been using.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prism.js
Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind. It's used in millions of websites, including some of...
Read more >
The guide to syntax highlighting in React - LogRocket Blog
To apply syntax highlighting in the browser environment, libraries such as Prism, Highlight, and React syntax highlighter exist.
Read more >
Markdown Syntax Highlighting with PrismJS using Unified ...
We'll explore how to use Unified, Remark, Rehype, and PrismJS to create markdown ... PrismJS but ran into issues supporting that within a...
Read more >
Prisma | Next-generation ORM for Node.js & TypeScript
Prisma is a next-generation Node.js and TypeScript ORM for PostgreSQL, MySQL, SQL Server, SQLite, MongoDB, and CockroachDB. It provides type-safety, ...
Read more >
How to make PrismJS editable - Educative.io
You can find a full list of supported languages here. To allow users to paste and edit the code block, we add contenteditable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found