Skip to content

Commit 056bfcc

Browse files
Adding a new convert markdown-to-html block that's powered by the GitHub API (#1)
Created a GitHub API block to handle Markdown-to-HTML conversion. Includes a simple (and somewhat sloppy) node HTTP wrapper.
1 parent 5803121 commit 056bfcc

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { post } = require('../../helpers/http')
2+
const { Block } = require('node-webpipe')
3+
4+
new Block()
5+
.name('Convert Markdown to HTML')
6+
.description('Determine whether or not a hostname exists.')
7+
.input(
8+
'text',
9+
'string',
10+
'Required. The Markdown text to render in HTML. Markdown content must be 400 KB or less.'
11+
)
12+
.input(
13+
'mode',
14+
'string',
15+
'The rendering mode. Can be either: [`gfm`,`markdown`]'
16+
)
17+
.input(
18+
'context',
19+
'string',
20+
'The repository context to use when creating references in gfm mode. Omit this parameter when using markdown mode.'
21+
)
22+
.output(
23+
'html',
24+
'string',
25+
'Returns an HTML version preferred flavor of Markdown.'
26+
)
27+
.handle(async function (inputs, cb) {
28+
const options = {
29+
hostname: 'api.github.com',
30+
path: '/markdown',
31+
method: 'POST'
32+
}
33+
const html = await post(options, inputs)
34+
cb(null, { html })
35+
})
36+
.listen()

helpers/http.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const https = require('https')
2+
3+
const post = (options, params) => {
4+
const paramsified = JSON.stringify(params)
5+
options.headers = {
6+
'Content-Type': 'text/plain',
7+
'Content-Length': Buffer.byteLength(paramsified),
8+
'User-Agent': 'WebPipes-Block-Examples'
9+
}
10+
return new Promise((resolve, reject) => {
11+
const req = https.request(options, res => {
12+
let body = ''
13+
res.setEncoding('utf8')
14+
res.on('data', chunk => (body += chunk.toString()))
15+
res.on('error', reject)
16+
res.on('end', () => {
17+
if (res.statusCode >= 200 && res.statusCode <= 299) {
18+
resolve(body)
19+
} else {
20+
reject(`Request failed. status: ${res.statusCode}, body: ${body}`)
21+
}
22+
})
23+
})
24+
25+
req.on('error', e => {
26+
console.error(`problem with request: ${e.message}`)
27+
})
28+
29+
req.write(paramsified)
30+
req.end()
31+
})
32+
}
33+
const get = (options, params) => {}
34+
35+
module.exports = { get, post }

0 commit comments

Comments
 (0)