Spaces:
Sleeping
Sleeping
Download index.js from pvanand/marpit-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/spaces/pvanand/marpit-backend/resolve/main/index.js
- Command line
-
hf download hf://spaces/pvanand/marpit-backend/index.js
-
curl -L -o index.js https://huggingface.co/spaces/pvanand/marpit-backend/resolve/main/index.js
1.85 kB
| const express = require('express'); | |
| const { marpCli } = require('@marp-team/marp-cli'); | |
| const fs = require('fs').promises; | |
| const path = require('path'); | |
| const app = express(); | |
| app.use(express.json()); | |
| const tmpDir = '/tmp/marp-work'; | |
| app.post('/convert', async (req, res) => { | |
| const { markdown, outputFormat, options = [] } = req.body; | |
| if (!markdown || !outputFormat) { | |
| return res.status(400).json({ error: 'Missing markdown or outputFormat' }); | |
| } | |
| try { | |
| const inputFile = path.join(tmpDir, `input_${Date.now()}.md`); | |
| const outputFile = path.join(tmpDir, `output_${Date.now()}.${outputFormat}`); | |
| await fs.writeFile(inputFile, markdown); | |
| const cliOptions = [ | |
| inputFile, | |
| '--output', | |
| outputFile, | |
| `--${outputFormat}`, | |
| '--html', | |
| ...options | |
| ]; | |
| if (outputFormat === 'pdf') { | |
| cliOptions.push( | |
| '--allow-local-files', | |
| '--pdf-notes', | |
| '--pdf-delay', '3000' | |
| ); | |
| } else if (outputFormat === 'pptx') { | |
| cliOptions.push( | |
| '--pptx-editable', | |
| '--allow-local-files', | |
| '--pptx-notes', | |
| '--pptx-delay', '3000' | |
| ); | |
| } | |
| await marpCli(cliOptions); | |
| const output = await fs.readFile(outputFile); | |
| await fs.unlink(inputFile); | |
| await fs.unlink(outputFile); | |
| res.contentType(outputFormat); | |
| res.send(output); | |
| } catch (error) { | |
| console.error('Conversion error:', error); | |
| res.status(500).json({ error: 'Conversion failed', details: error.message }); | |
| } | |
| }); | |
| const port = process.env.PORT || 7860; | |
| app.listen(port, () => { | |
| console.log(`Marp API server listening on port ${port}`); | |
| }); |