Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rintaroutw/latex2svg

LaTeX to SVG
https://github.com/rintaroutw/latex2svg

lambda-functions latex netlify nodejs svg

Last synced: 4 months ago
JSON representation

LaTeX to SVG

Awesome Lists containing this project

README

        

# latex2svg

simple node.js api to convert LaTeX to svg

to

[Demo Site](https://rintaroutw-latex2svg.netlify.com/)

(https://github.com/RintarouTW/latex2svg)

[![Netlify Status](https://api.netlify.com/api/v1/badges/28ed90bb-aaf5-4bdb-9025-61a8f31a9549/deploy-status)](https://app.netlify.com/sites/rintaroutw-latex2svg/deploys)

| API | /.netlify/functions/latex2svg |
|-----|----|
| METHOD | POST |
| BODY | { "latex" : "\$ $" } |

Example:

```python!
requests.post('https://rintaroutw-latex2svg.netlify.com/.netlify/functions/latex2svg',
json = {"latex": "$$\\frac{1}{6}$$"})

{
"svg": "

\n\\frac{1}{6}
\n"
}
```

## Protocol

Request : { latex: \$$ }
Response: { svg: \$SVG_TAG$ }

## npm
node package manager

> npm (un)install module

package.json required pacakges list

pacakge-lock.json snapshot of all required packages info.

## Useful pacakges

## express

request router

> express.use(path, module_handler);
> express.get(path, request_handler);

### nodemon

node monitor, restart service automatically after source was modified.

```shell!
nodemon app.js
```

## Testing

### Auto Test via axios inside Node.js itself.

npm install axios
Promise based HTTP client for the browser and node.js

```javascript=
// Automation Testing
console.log("=== Automation Testing ===");

const axios = require('axios');

axios.post('http://localhost/latex2svg', {
latex: '$$\frac{1}{2}$$'
}).then(res => {
console.log(`Response.data : `, res.data);
});
```

### Requester (Sublime Text Package)

HTTP Requester, even support GraphQL for automation tests.
Text based, fast and portable.
https://requester.org/#documentation

### Browser Web Fetch

[Using Fetch](https://developer.mozilla.org/zh-TW/docs/Web/API/Fetch_API/Using_Fetch)

*index.html*

```javascript=

function latex2svg(latex) {

fetch('/.netlify/functions/latex2svg', {
method: 'POST',
body: JSON.stringify(latex),
headers: new Headers({
'Content-Type': 'application/json'
}),
mode: 'cors' // no-cors, cors, *same-origin
}).then(response => {
return response.json();
}).then(json => {
container = document.getElementById('svg_container');
container.innerHTML = json.svg;
}).catch( error => {
console.log("Something Wrong");
});;
}

latex2svg({ "latex" : '\$\$\\LaTeX \\frac{1}{3}\$\$'});


```

:::warning
mode: no-cors
:::
no-cors is required to cross origin access when tring to load my service directly in your own html.

## Netlify Deployment

Free serverless web service

### GitHub (Push = Auto Deploy)

git push and deploy, np at all.

### netlify-lambda functions

each function is a module.

Build command is required for lambda functions.
```
npm run netlify-lambda build functions
```

#### functions/lambda.js

source of lambda function, not compiled.

``` javascript
exports.handler = function(event, context, callback) {
// event.body
// context for dev/prod environment info
callback(null, {
statusCode: 200,
body: {svg: ""}
});
}
```

### netlify.toml

```
[build]
function = "lambda" // the compiled path of lambda functions
```

## Tests

```
nodemon app.js
or
npm run start
```

**axios** to send the test requests after launch automatically. It's possible to write the automation tests with assert too.

Sublime Text with Requester(pacakge)

tests.pyr (ctrl+r to send requests/ cmd+r to resend)
```python!
## localhost node app.js test
requests.post('http://localhost/.netlify/functions/latex2svg',
json = {"latex": "$$\frac{}$$"})

## localhost netlify-lambda serve functions test.
requests.post('http://localhost:9000/.netlify/functions/latex2svg',
json = {"latex": "$$\\frac{1}{6}$$"})

## netlify-lambda function server test
requests.post('https://rintaroutw-latex2svg.netlify.com/.netlify/functions/latex2svg',
json = {"latex": "$$\\frac{1}{6}$$"})
```

###### tags: `node.js` `netlify` `latex2svg`