fix: error due to inability to parse invalid unicode sequences

This commit is contained in:
Ondřej Synáček 2020-12-26 20:57:19 +01:00
parent cfa7db4f9e
commit 0683ef9666
2 changed files with 19 additions and 2 deletions

View File

@ -1,7 +1,12 @@
import { postURL } from '../services'
import { eventStore, parseStatusStore, requestStore } from '../stores'
import { Request } from '../records'
import { uuidv4, parseStartTimeFromiCalString, promptDownload } from '../utils'
import {
uuidv4,
parseStartTimeFromiCalString,
promptDownload,
encodeIcalString,
} from '../utils'
import { extractEventDataFromHTML } from '../../../lib/services/ics-retriever'
import generateICS from '../../../lib/services/ics-generator'
@ -35,7 +40,7 @@ const createICS = async (html, url, { logger }) => {
const eventData = extractEventDataFromHTML(html, url, { logger })
const text = await generateICS(eventData)
const dataUri = encodeURIComponent(text)
const dataUri = encodeIcalString(text)
const uri = `data:text/calendar;charset=utf-8,${dataUri}`
const summaryMatch = text.match(/SUMMARY:.*/)[0]

View File

@ -1,3 +1,5 @@
const UNICODE_RE = /[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g
export const migrateRecord = (record) => {
// NOTE: v3 records
const id = record.id || record.order
@ -55,3 +57,13 @@ export const promptDownload = (uri) => {
link.setAttribute('href', '')
}
export const encodeIcalString = (string) => {
try {
return encodeURIComponent(string)
} catch {
return string.replace(UNICODE_RE , ($0) => {
return $0.length > 1 ? $0 : '\ufffd';
})
}
}