Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const { marpCli } = require('@marp-team/marp-cli');
|
| 3 |
const fs = require('fs').promises;
|
|
@@ -6,54 +7,51 @@ const path = require('path');
|
|
| 6 |
const app = express();
|
| 7 |
app.use(express.json());
|
| 8 |
|
| 9 |
-
// Use the dedicated temporary directory
|
| 10 |
const tmpDir = '/tmp/marp-work';
|
| 11 |
|
| 12 |
app.post('/convert', async (req, res) => {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
try {
|
| 20 |
-
const inputFile = path.join(tmpDir, `input_${Date.now()}.md`);
|
| 21 |
-
const outputFile = path.join(tmpDir, `output_${Date.now()}.${outputFormat}`);
|
| 22 |
-
|
| 23 |
-
await fs.writeFile(inputFile, markdown);
|
| 24 |
-
|
| 25 |
-
const cliOptions = [
|
| 26 |
-
inputFile,
|
| 27 |
-
'--output', outputFile,
|
| 28 |
-
`--${outputFormat}`,
|
| 29 |
-
'--html', // Added --html flag for all conversions
|
| 30 |
-
...options
|
| 31 |
-
];
|
| 32 |
-
|
| 33 |
-
// Add options for PDF and PPTX conversion
|
| 34 |
-
if (outputFormat === 'pdf' || outputFormat === 'pptx') {
|
| 35 |
-
cliOptions.push('--allow-local-files');
|
| 36 |
-
cliOptions.push(`--${outputFormat}-notes`);
|
| 37 |
-
cliOptions.push(`--${outputFormat}-delay`, '3000'); // 3 seconds delay
|
| 38 |
}
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
await fs.unlink(inputFile);
|
| 46 |
-
await fs.unlink(outputFile);
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
});
|
| 55 |
|
| 56 |
const port = process.env.PORT || 7860;
|
| 57 |
app.listen(port, () => {
|
| 58 |
-
|
| 59 |
});
|
|
|
|
| 1 |
+
# Node.js Server Script
|
| 2 |
const express = require('express');
|
| 3 |
const { marpCli } = require('@marp-team/marp-cli');
|
| 4 |
const fs = require('fs').promises;
|
|
|
|
| 7 |
const app = express();
|
| 8 |
app.use(express.json());
|
| 9 |
|
|
|
|
| 10 |
const tmpDir = '/tmp/marp-work';
|
| 11 |
|
| 12 |
app.post('/convert', async (req, res) => {
|
| 13 |
+
const { markdown, outputFormat, options = [] } = req.body;
|
| 14 |
+
|
| 15 |
+
if (!markdown || !outputFormat) {
|
| 16 |
+
return res.status(400).json({ error: 'Missing markdown or outputFormat' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
+
try {
|
| 20 |
+
const inputFile = path.join(tmpDir, `input_${Date.now()}.md`);
|
| 21 |
+
const outputFile = path.join(tmpDir, `output_${Date.now()}.${outputFormat}`);
|
| 22 |
+
|
| 23 |
+
await fs.writeFile(inputFile, markdown);
|
| 24 |
+
|
| 25 |
+
const cliOptions = [
|
| 26 |
+
inputFile,
|
| 27 |
+
'--output', outputFile,
|
| 28 |
+
`--${outputFormat}`,
|
| 29 |
+
'--html',
|
| 30 |
+
...options
|
| 31 |
+
];
|
| 32 |
|
| 33 |
+
if (outputFormat === 'pdf' || outputFormat === 'pptx') {
|
| 34 |
+
cliOptions.push('--allow-local-files');
|
| 35 |
+
cliOptions.push(`--${outputFormat}-notes`);
|
| 36 |
+
cliOptions.push(`--${outputFormat}-delay`, '3000');
|
| 37 |
+
}
|
| 38 |
|
| 39 |
+
await marpCli(cliOptions);
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
const output = await fs.readFile(outputFile);
|
| 42 |
+
|
| 43 |
+
await fs.unlink(inputFile);
|
| 44 |
+
await fs.unlink(outputFile);
|
| 45 |
+
|
| 46 |
+
res.contentType(outputFormat);
|
| 47 |
+
res.send(output);
|
| 48 |
+
} catch (error) {
|
| 49 |
+
console.error('Conversion error:', error);
|
| 50 |
+
res.status(500).json({ error: 'Conversion failed', details: error.message });
|
| 51 |
+
}
|
| 52 |
});
|
| 53 |
|
| 54 |
const port = process.env.PORT || 7860;
|
| 55 |
app.listen(port, () => {
|
| 56 |
+
console.log(`Marp API server listening on port ${port}`);
|
| 57 |
});
|