Vitaly Puzrin
1 year ago
3 changed files with 0 additions and 100 deletions
@ -1 +0,0 @@ |
|||||
web: node support/babelmark-responder.mjs |
|
@ -1,44 +0,0 @@ |
|||||
#!/usr/bin/env node
|
|
||||
|
|
||||
/* eslint-disable no-console */ |
|
||||
|
|
||||
import markdownit from '../index.mjs' |
|
||||
import express from 'express' |
|
||||
import { readFileSync } from 'fs' |
|
||||
|
|
||||
const md = markdownit('commonmark') |
|
||||
const app = express() |
|
||||
const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url))).version |
|
||||
|
|
||||
const banner = `<!doctype html>
|
|
||||
<html lang="en"> |
|
||||
<head> |
|
||||
<meta charset="utf-8"> |
|
||||
<title>markdown-it responder for babelmark</title> |
|
||||
</head> |
|
||||
<body> |
|
||||
<p><a href="https://github.com/markdown-it/markdown-it" target="_blank">markdown-it</a> |
|
||||
responder for <a href="http://johnmacfarlane.net/babelmark2/" target="_blank">Babelmark2</a></p> |
|
||||
<p>Usage: /?text=...</p> |
|
||||
</body> |
|
||||
</html> |
|
||||
` |
|
||||
|
|
||||
app.set('port', (process.env.PORT || 5000)) |
|
||||
|
|
||||
app.get('/', function (req, res) { |
|
||||
if (typeof req.query.text === 'string') { |
|
||||
res.json({ |
|
||||
name: 'markdown-it', |
|
||||
html: md.render(req.query.text.slice(0, 1000)), |
|
||||
version |
|
||||
}) |
|
||||
return |
|
||||
} |
|
||||
res.setHeader('Content-Type', 'text/html') |
|
||||
res.send(banner) |
|
||||
}) |
|
||||
|
|
||||
app.listen(app.get('port'), function () { |
|
||||
console.log(`Node app is running on port ${app.get('port')}`) |
|
||||
}) |
|
@ -1,55 +0,0 @@ |
|||||
import supertest from 'supertest' |
|
||||
import { execFile } from 'child_process' |
|
||||
import { readFileSync } from 'fs' |
|
||||
import { setTimeout as sleep } from 'node:timers/promises' |
|
||||
|
|
||||
describe('babelmark responder app', function () { |
|
||||
let app |
|
||||
|
|
||||
const PORT = 5005 |
|
||||
const request = supertest('http://127.0.0.1:' + PORT) |
|
||||
|
|
||||
before(async () => { |
|
||||
app = execFile( |
|
||||
'node', |
|
||||
['../support/babelmark-responder.mjs'], |
|
||||
{ |
|
||||
cwd: new URL('.', import.meta.url), |
|
||||
env: Object.assign({}, process.env, { PORT }) |
|
||||
} |
|
||||
) |
|
||||
|
|
||||
// Wait until app bind port
|
|
||||
for (let i = 0; i < 50; i++) { |
|
||||
try { |
|
||||
await request.get('/').expect(200) |
|
||||
break |
|
||||
} catch (e) {} |
|
||||
await sleep(100) |
|
||||
} |
|
||||
}) |
|
||||
|
|
||||
it('ping root', () => { |
|
||||
return request |
|
||||
.get('/') |
|
||||
.expect(200) |
|
||||
.expect(/<!DOCTYPE html>/i) |
|
||||
}) |
|
||||
|
|
||||
it('do request', () => { |
|
||||
const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url))).version |
|
||||
|
|
||||
return request |
|
||||
.get('/?text=foo') |
|
||||
.expect(200) |
|
||||
.expect({ |
|
||||
html: '<p>foo</p>\n', |
|
||||
name: 'markdown-it', |
|
||||
version |
|
||||
}) |
|
||||
}) |
|
||||
|
|
||||
after(() => { |
|
||||
if (app) app.kill() |
|
||||
}) |
|
||||
}) |
|
Loading…
Reference in new issue