google-nextcloud-ical-convert/convert.ts

44 lines
895 B
TypeScript
Raw Permalink Normal View History

2024-11-18 16:41:33 +00:00
const decoder = new TextDecoder("utf-8");
const data = await Deno.readFile("ical.ics");
const text = decoder.decode(data);
function chunk(str, size) {
const numChunks = Math.ceil(str.length / size)
const chunks = new Array(numChunks)
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size)
}
return chunks
}
const lines = text
.split('\r\n')
.reduce((newics, line) => {
if (line.startsWith(' ')) {
return newics + line.replace(/^ /, '');
}
return newics + '\n' + line;
}, "")
.split('\n')
.map(line => line
.replace(/<br>/g, '\\n')
.replace(/<[^>]*>/g, '')
.replace(/&gt\\;/g, '>')
.replace(/&lt\\;/g, '<')
.replace(/&nbsp\\;/g, ' ')
.replace(/&amp\\;/g, '&')
.replace(/&quot\\;/g, '"')
)
.reduce((allLines, line) => [
...allLines,
chunk(line, 40).join('\r\n '),
], [])
.join('\r\n');
console.log(lines);