summaryrefslogtreecommitdiff
path: root/pkgs/katex-wrapper/default.nix
blob: ef1cce67292a89738925b28e1f9a1899bda6b669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{ stdenv, pkgs, ... }:

let
  katex-src = stdenv.mkDerivation rec {
    name = "katex-src";

    src = pkgs.fetchzip {
      url    = "https://github.com/KaTeX/KaTeX/releases/download/v0.10.0/katex.tar.gz";
      sha256 = "0knmbzpqikw8iafsxnar96kxp0z0vdxng9bfzzrvqkbsxfs59cfy";
    };

    buildInputs = [ pkgs.nodejs ];

    installPhase = ''
      mkdir -p $out/share/katex
      cp katex.min.js $out/share/katex/
    '';

    meta = {
      description = "KaTeX";
      homepage    = https://github.com/Khan/KaTeX;
      license     = stdenv.lib.licenses.mit;
      platforms   = stdenv.lib.platforms.all;
    };
  };
in pkgs.writeTextFile {
  name        = "katex-wrapper";
  executable  = true;
  destination = "/bin/katex";

  text = ''
    #!${pkgs.nodejs}/bin/node

    var katex = require("${katex-src}/share/katex/katex.min.js");
    var input = "";

    var args = process.argv.slice(2);

    process.stdin.on("data", function(chunk) {
        input += chunk.toString();
    });

    process.stdin.on("end", function() {
        var options = {
            displayMode: args.indexOf("--display-mode") !== -1,
            throwOnError: false
        };
        var output = katex.renderToString(input, options);
        console.log(output);
    });
  '';
}