Skip to content

Commit 6c88b62

Browse files
meodaizeke
andauthored
feature/browser compatibility (#6)
Co-authored-by: Zeke Sikelianos <[email protected]>
1 parent 0d0340b commit 6c88b62

13 files changed

+15345
-4067
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

build.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs')
2+
3+
const body = fs.readFileSync('index.js', 'utf8')
4+
const nodeTpl = fs.readFileSync('index.node.js.tpl', 'utf8')
5+
const browserTpl = fs.readFileSync('index.js.tpl', 'utf8')
6+
7+
fs.writeFileSync('dist/index.node.js', nodeTpl.replace('{{body}}', body));
8+
fs.writeFileSync('dist/index.js', browserTpl.replace('{{body}}', body));

cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const lookup = require('.')
3+
const lookup = require('./dist/index.node')
44
const query = process.argv.slice(2)
55
const wrap = require('wordwrap')(80)
66

demo/index.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html>
2+
3+
<html lang=en>
4+
5+
<head>
6+
<meta charset=utf-8>
7+
<title>Wikipedia TLDR Browser Demo</title>
8+
</head>
9+
10+
<body>
11+
<div class="result">
12+
13+
</div>
14+
15+
<script type="module">
16+
import wikipediatldr from '../dist/index.js'
17+
(async () => {
18+
const results = await wikipediatldr('pink');
19+
console.log(results);
20+
document.querySelector('.result').innerHTML = `
21+
<h1>${results.displaytitle}</h1>
22+
<div>${results.extract_html}</div>
23+
<img src="${results.originalimage.source}" alt="${results.displaytitle}" />
24+
`
25+
})()
26+
</script>
27+
</body>
28+
29+
</html>

dist/index.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const defaultUnwantedProps = [
2+
'content_urls',
3+
'dir',
4+
'revision',
5+
'tid',
6+
'timestamp',
7+
'pageid',
8+
'namespace',
9+
'titles',
10+
'api_urls'
11+
]
12+
13+
async function lookup (
14+
query,
15+
locale = 'en',
16+
followRedirects = false,
17+
unwantedProps = defaultUnwantedProps
18+
) {
19+
const url = new URL(`https://${locale}.wikipedia.org`)
20+
const params = { followRedirects }
21+
22+
url.pathname = `/api/rest_v1/page/summary/${encodeURIComponent(query)}`
23+
url.search = new URLSearchParams(params).toString()
24+
25+
let body
26+
27+
try {
28+
const res = await fetch(url).then(res => {
29+
if (!res.ok) {
30+
throw Error(res.statusText)
31+
}
32+
return res
33+
})
34+
body = await res.json()
35+
} catch (err) {
36+
return null
37+
}
38+
39+
unwantedProps.forEach(prop => {
40+
delete body[prop]
41+
})
42+
43+
if (body.text) {
44+
body.text = body.text
45+
.trim()
46+
.replace(/\[\d+\]/g, '')
47+
} // remove footnotes like [1]
48+
49+
if (body.html) body.html = body.html.trim()
50+
51+
return Object.assign({}, { query: query }, body)
52+
}
53+
54+
export default lookup

dist/index.node.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const fetch = require('node-fetch')
2+
const { URL, URLSearchParams } = require('url')
3+
4+
const defaultUnwantedProps = [
5+
'content_urls',
6+
'dir',
7+
'revision',
8+
'tid',
9+
'timestamp',
10+
'pageid',
11+
'namespace',
12+
'titles',
13+
'api_urls'
14+
]
15+
16+
async function lookup (
17+
query,
18+
locale = 'en',
19+
followRedirects = false,
20+
unwantedProps = defaultUnwantedProps
21+
) {
22+
const url = new URL(`https://${locale}.wikipedia.org`)
23+
const params = { followRedirects }
24+
25+
url.pathname = `/api/rest_v1/page/summary/${encodeURIComponent(query)}`
26+
url.search = new URLSearchParams(params).toString()
27+
28+
let body
29+
30+
try {
31+
const res = await fetch(url).then(res => {
32+
if (!res.ok) {
33+
throw Error(res.statusText)
34+
}
35+
return res
36+
})
37+
body = await res.json()
38+
} catch (err) {
39+
return null
40+
}
41+
42+
unwantedProps.forEach(prop => {
43+
delete body[prop]
44+
})
45+
46+
if (body.text) {
47+
body.text = body.text
48+
.trim()
49+
.replace(/\[\d+\]/g, '')
50+
} // remove footnotes like [1]
51+
52+
if (body.html) body.html = body.html.trim()
53+
54+
return Object.assign({}, { query: query }, body)
55+
}
56+
57+
module.exports = lookup

index.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const get = require('got')
2-
const URL = require('url')
3-
const unwantedProps = [
1+
const defaultUnwantedProps = [
42
'content_urls',
53
'dir',
64
'revision',
@@ -12,17 +10,28 @@ const unwantedProps = [
1210
'api_urls'
1311
]
1412

15-
async function lookup (query, locale = 'en') {
16-
const url = URL.format({
17-
protocol: 'https',
18-
hostname: `${locale}.wikipedia.org`,
19-
pathname: `/api/rest_v1/page/summary/${encodeURIComponent(query)}`
20-
})
13+
async function lookup (
14+
query,
15+
locale = 'en',
16+
followRedirects = false,
17+
unwantedProps = defaultUnwantedProps
18+
) {
19+
const url = new URL(`https://${locale}.wikipedia.org`)
20+
const params = { followRedirects }
21+
22+
url.pathname = `/api/rest_v1/page/summary/${encodeURIComponent(query)}`
23+
url.search = new URLSearchParams(params).toString()
2124

2225
let body
26+
2327
try {
24-
const res = await get(url, { json: true })
25-
body = res.body
28+
const res = await fetch(url).then(res => {
29+
if (!res.ok) {
30+
throw Error(res.statusText)
31+
}
32+
return res
33+
})
34+
body = await res.json()
2635
} catch (err) {
2736
return null
2837
}
@@ -41,5 +50,3 @@ async function lookup (query, locale = 'en') {
4150

4251
return Object.assign({}, { query: query }, body)
4352
}
44-
45-
module.exports = lookup

index.js.tpl

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{body}}
2+
export default lookup

index.node.js.tpl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fetch = require('node-fetch')
2+
const { URL, URLSearchParams } = require('url')
3+
4+
{{body}}
5+
module.exports = lookup

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://jestjs.io/docs/en/configuration.html
22

33
module.exports = {
4-
testEnvironment: 'node'
4+
testEnvironment: 'node',
55
}

0 commit comments

Comments
 (0)