This repository was archived by the owner on Nov 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·53 lines (44 loc) · 1.32 KB
/
index.js
File metadata and controls
executable file
·53 lines (44 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env node
const { readFileSync, statSync } = require('fs')
const { resolve } = require('path')
const toMd = require('to-markdown')
const ww = require('wordwrap')
const checkForFile = (filePath) => {
try {
statSync(resolve(filePath))
return true
} catch {
return false
}
}
const url = process.argv[2] || 'https://example.com'
const { rows } = process.stdout
const len = rows < 80 ? 120 : rows
const wrapper = ww(len)
const converters = [
{
filter: ['style', 'script'],
replacement: (_) => ''
}
]
const opts = { gfm: true, converters }
const conv = (a) => toMd(a, opts)
const wrap = (a) => wrapper(a)
const log = console.log
const removeTags = (s = '') => s.replace(/(<([^>]+)>)/gi, '')
const collapseNewlines = (s = '') => s.replace(/\n\s*\n/g, '\n\n')
const strip = (a = '') => collapseNewlines(removeTags(a))
const handleEnds = (a = '') => a.trim() + '\n'
const doTheThing = (s = '') => log(handleEnds(wrap(strip(conv(s)))))
const runIfUrl = (a) =>
fetch(a.includes('://') ? a : `http://${a}`)
.then((res) => res.text())
.then(doTheThing)
.catch((err) => {
console.error(err)
})
const runIfFile = (a) => doTheThing(readFileSync(resolve(a)).toString())
const getTheString = (a) =>
checkForFile(a) ? runIfFile(a) : runIfUrl(a)
const main = (a) => getTheString(a)
main(url)