mirror of
https://github.com/comatory/fb2iCal
synced 2025-06-05 22:09:25 +02:00
add specs for LD JSON parser
This commit is contained in:
@@ -22,7 +22,7 @@ const parseEventData = (eventData) => {
|
||||
locationName,
|
||||
addressStr,
|
||||
].join(' ')
|
||||
const cleanedLocationStr = locationStr.replace(/\r?\n|\r/g, ' ')
|
||||
const cleanedLocationStr = locationStr.trim().replace(/\r?\n|\r/g, ' ')
|
||||
const title = eventData.name || ''
|
||||
const url = eventData.url || ''
|
||||
const description = eventData.description || ''
|
||||
@@ -46,36 +46,37 @@ const parseUsingLDJSONData = (html, { logger }) => {
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
// NOTE: Mobile web should have serialized
|
||||
// event info in one of the script tags
|
||||
const $ = cheerio.load(html)
|
||||
const $scripts = $('head script[type="application/ld+json"]')
|
||||
const rawData = $scripts.toArray().reduce((data, node) => {
|
||||
const firstNode = node.children[0]
|
||||
|
||||
if (!firstNode || !firstNode.data) {
|
||||
return data
|
||||
}
|
||||
|
||||
if (firstNode.data.startsWith('//<![CDATA')) {
|
||||
return firstNode.data
|
||||
}
|
||||
// NOTE: Mobile web should have serialized
|
||||
// event info in one of the script tags
|
||||
const $ = cheerio.load(html)
|
||||
const $scripts = $('head script[type="application/ld+json"]')
|
||||
const rawData = $scripts.toArray().reduce((data, node) => {
|
||||
const firstNode = node.children[0]
|
||||
|
||||
if (!firstNode || !firstNode.data) {
|
||||
return data
|
||||
}, null)
|
||||
|
||||
if (!rawData) {
|
||||
return null
|
||||
}
|
||||
|
||||
const eventData = JSON.parse(rawData.slice(12, -5))
|
||||
const data = parseEventData(eventData)
|
||||
// NOTE: Handle prefix
|
||||
if (firstNode.data.startsWith('//<![CDATA')) {
|
||||
return firstNode.data.slice(12, -5)
|
||||
}
|
||||
|
||||
if (firstNode.data) {
|
||||
return firstNode.data.trim()
|
||||
}
|
||||
|
||||
return data
|
||||
} catch (err) {
|
||||
throw err
|
||||
}, null)
|
||||
|
||||
if (!rawData) {
|
||||
return null
|
||||
}
|
||||
|
||||
const eventData = JSON.parse(rawData)
|
||||
const data = parseEventData(eventData)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
module.exports = parseUsingLDJSONData
|
||||
|
Reference in New Issue
Block a user