Add gutenberg utility functions
This commit is contained in:
parent
8ce324cd04
commit
e98ea6eb87
23
src/lib/gutenberg.js
Normal file
23
src/lib/gutenberg.js
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user