chialab / math-api

➗ Serverless API to render maths using MathJax for Node.
MIT License
43 stars 8 forks source link
aws-apigateway aws-lambda docker hacktoberfest latex mathml nodejs serverless

Math API logo

Math API • Render LaTeX and MathML formulas as SVG or PNG.


A REST API to do fancy things with formulas, like rendering LaTeX or MathML to SVG or PNG on the server side using MathJax for Node, while leveraging expensive computations on the client.

As a Serverless application

You can deploy this repository as a serverless application using an AWS CloudFormation Template to create an AWS API Gateway that invokes Lambda functions to serve requests.

Launch this stack on AWS

As a Docker image

You can pull and run a Docker container to deploy the API on your local machine, server, Kubernetes cluster, whatever!

To start the container (it will bind on http://localhost:3000/):

docker run --name math-api -d -p 3000:3000 chialab/math-api

Endpoints

GET /render

An endpoint to render LaTeX and MathML formulas to SVG or PNG.

Query parameters:

Examples:

GET /render?input=latex&output=svg&source=x^2 HTTP/1.1
Accept: image/svg+xml
GET /render?input=latex&inline=1&output=png&source=x^2&width=512 HTTP/1.1
Accept: image/png

POST /render

An endpoint to render LaTeX and MathML formulas to SVG or PNG.

Request body (JSON):

Examples:

POST /render
Accept: image/svg+xml
Content-Type: application/json

{
    "input": "latex",
    "output": "svg",
    "source": "e^{i \\pi} + 1 = 0"
}
POST /render
Accept: image/png
Content-Type: application/json

{
    "input": "latex",
    "inline": true,
    "output": "png",
    "source": "e^{i \\pi} + 1 = 0",
    "width": 512
}

Development

All the following instructions assume you have at least NodeJS and Yarn installed.

Start the application locally:

yarn start

Run unit tests:

yarn run test

Start a simulated AWS API Gateway (provided you have AWS SAM Local and Docker installed):

yarn run api-gateway

Validate CloudFormation template (provided you have AWS CLI installed)

make validate

Package CloudFormation template (provided you have AWS CLI and Docker installed)

make layers (this is needed only the first time, then when updating MathJax version) make package

Deploy CloudFormation template (provided you have AWS CLI and Docker installed)

make deploy make deploy ENVIRONMENT=Production


License

Math API is released under the MIT license.