Add gutenberg utility functions

This commit is contained in:
Alfred Melch 2019-12-28 12:18:38 +01:00
parent 8ce324cd04
commit e98ea6eb87

23
src/lib/gutenberg.js Normal file
View File

@ -0,0 +1,23 @@
import Axios from 'axios'
export async function search(searchTerm, maxResults = Infinity) {
const regex = new RegExp(searchTerm, 'i')
const result = []
const data = await import('../../data/gutenberg.json').then(
module => module.default
)
for (let entry of data) {
if (regex.test(entry.title[0]) || regex.test(entry.author[0])) {
result.push(entry)
}
if (result.length >= maxResults) break
}
return result
}
export async function getBook(bookId) {
const url = `https://gutenberg.muperfredi.de/texts/${bookId}/stripped-body`
const text = await Axios.get(url).then(res => res.data.body)
return text
}