import { getWork } from "@fujocoded/ao3.js"
import DOM from "fauxdom"
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import themes from '@/lib/themes.js'
import baseFonts from '@/lib/baseFonts.js'
import titleFonts from '@/lib/titleFonts.js'
export default async function sanitizeData ({ type, data, props}) {
const baseFont = props.has('baseFont') ? props.get('baseFont') : process.env.DEFAULT_BASE_FONT
const baseFontData = baseFonts[baseFont]
const titleFont = props.has('titleFont') ? props.get('titleFont') : process.env.DEFAULT_TITLE_FONT
const titleFontData = titleFonts[titleFont]
const themeData = props.has('theme') ? themes[props.get('theme')] : themes[process.env.DEFAULT_THEME]
const parentWork = type === 'work' && data.chapterInfo ? await getWork({workId: data.id}) : null
const bfs = await Promise.all(baseFontData.defs.map(async (bf) => {
return {
name: baseFontData.displayName,
data: await readFile(
join(process.cwd(), bf.path)
),
style: bf.style,
weight: bf.weight
}
})).then(x => x)
const tfs = await Promise.all(titleFontData.defs.map(async (tf) => {
return {
name: titleFontData.displayName,
data: await readFile(
join(process.cwd(), tf.path)
),
style: tf.style,
weight: tf.weight
}
})).then(x => x)
const authorsFormatted = data.authors
? data.authors.map((a) => {
if (a.anonymous) return "Anonymous"
if (a.pseud !== a.username) return `${a.pseud} (${a.username})`
return a.username
})
: []
const authorString = authorsFormatted.length > 1
? authorsFormatted.slice(0, -1).join(", ") + " & " +
authorsFormatted.slice(-1)[0]
: authorsFormatted[0]
const summaryContent = type === 'work'
? (props.get('summaryType') === 'chapter' && data.chapterInfo && data.chapterInfo.summary ? data.chapterInfo.summary : (props.get('summaryType') === 'custom' && props.has('customSummary') ? props.get('customSummary') : (data.summary ? data.summary : (parentWork ? parentWork.summary : ''))))
: (props.get('summaryType') === 'custom' && props.has('customSummary') ? props.get('customSummary') : data.notes)
const summaryDOM = new DOM(summaryContent, {decodeEntities: true});
const summaryFormatted = summaryDOM.innerHTML.replace(/\
/g, "\n").replace(
/(<([^>]+)>)/ig,
"",
).split("\n")
const titleString = type === 'work' ? data.title : data.name
const chapterString = data.chapterInfo ? (data.chapterInfo.name
? data.chapterInfo.name
: "Chapter " + data.chapterInfo.index) : null
const chapterCountString = data.chapters
? ' | Chapters: '+data.chapters.published+' / '+(
data.chapters.total
? data.chapters.total
: '?'
)
: ''
const fandomString = type === 'work' ? (
data.fandoms.length > 1
? (
data.fandoms.length <= 5
? data.fandoms.slice(0, -1).join(", ")+" & "+data.fandoms.slice(-1)
: data.fandoms.join(", ")+" (+"+(data.fandoms.length - 4)+")"
)
: data.fandoms[0]
) : (
data.works.map(w => w.fandoms).reduce((a, b) => { return a.concat(b) }).filter((w, i) => { return i === data.works.indexOf(w) })
)
return {
topLine: fandomString,
titleLine: titleString,
authorLine: authorString,
chapterLine: chapterString,
summary: summaryFormatted,
url: 'https://archiveofourown.org/',
theme: themeData,
baseFont: baseFont,
titleFont: titleFont,
opts: {
fonts: bfs.concat(tfs)
}
}
}