redirecter for ao3 that adds opengraph metadata
1import { getWork } from "@fujocoded/ao3.js" 2import { setArchiveBaseUrl, resetArchiveBaseUrl } from "@fujocoded/ao3.js/urls" 3import DOM from "fauxdom" 4import { readFile } from 'node:fs/promises' 5import { join } from 'node:path' 6import themes from '@/lib/themes.js' 7import baseFonts from '@/lib/baseFonts.js' 8import titleFonts from '@/lib/titleFonts.js' 9 10const getHighestRating = async (works, archive = null) => { 11 const ratings = await Promise.all(works.map(async (w) => { 12 if (archive) setArchiveBaseUrl('https://'+archive) 13 const work = await getWork({workId: w.id}) 14 if (archive) resetArchiveBaseUrl() 15 return work.rating 16 })) 17 if (ratings.includes("Not Rated")) { 18 return "NR" 19 } else if (ratings.includes("Explicit")) { 20 return "E" 21 } else if (ratings.includes("Mature")) { 22 return "M" 23 } else if (ratings.includes("Teen")) { 24 return "T" 25 } 26 return "G" 27} 28 29const getHighestWarning = async (works, archive = null) => { 30 const warnings = await Promise.all(works.map(async (w) => { 31 if (archive) setArchiveBaseUrl('https://'+archive) 32 const work = await getWork({workId: w.id}) 33 if (archive) resetArchiveBaseUrl() 34 return work.tags.warnings 35 })) 36 const warningsUnique = warnings.reduce((a, b) => { return a.concat(b) }).filter((w, i) => { return i === warnings.indexOf(w) }) 37 if (warningsUnique.length === 1 && warningsUnique[0] === "Creator Chose Not To Use Archive Warnings") { 38 return "CNTW" 39 } else if (warningsUnique.length === 1 && warningsUnique[0] === "No Archive Warnings Apply") { 40 return "NW" 41 } 42 return "W" 43} 44 45const getCategory = async (works, archive = null) => { 46 const categories = await Promise.all(works.map(async (w) => { 47 if (archive) setArchiveBaseUrl('https://'+archive) 48 const work = await getWork({workId: w.id}) 49 if (archive) resetArchiveBaseUrl() 50 return work.category 51 })) 52 const categoriesJoined = categories.reduce((a, b) => { return a.concat(b) }) 53 const categoriesUnique = categoriesJoined.filter((w, i) => { return i === categoriesJoined.indexOf(w) }) 54 55 if (categoriesUnique.length === 1) { 56 if (categoriesUnique[0] === "F/F") return "F" 57 if (categoriesUnique[0] === "M/M") return "M" 58 if (categoriesUnique[0] === "F/M") return "FM" 59 if (categoriesUnique[0] === "Gen") return "G" 60 if (categoriesUnique[0] === "Multi") return "MX" 61 if (categoriesUnique[0] === "Other") return "O" 62 } 63 return "MX" 64} 65 66const sanitizeProps = (props) => { 67 let propsParsed = {} 68 Object.keys(props).forEach((pr) => { 69 if (props[pr] === 'true') { 70 propsParsed[pr] = true 71 return 72 } else if (props[pr] === 'false') { 73 propsParsed[pr] = false 74 return 75 } else if (typeof parseInt(props[pr]) === 'Number') { 76 propsParsed[pr] = parseInt(props[pr]) 77 return 78 } 79 propsParsed[pr] = props[pr] 80 }) 81 return propsParsed 82} 83 84export default async function sanitizeData ({ type, data, props}) { 85 const propsParsed = sanitizeProps(props) 86 const archive = propsParsed.archive 87 const baseFont = propsParsed.baseFont ? propsParsed.baseFont : process.env.DEFAULT_BASE_FONT 88 const baseFontData = baseFonts[baseFont] 89 const titleFont = propsParsed.titleFont ? propsParsed.titleFont : process.env.DEFAULT_TITLE_FONT 90 const titleFontData = titleFonts[titleFont] 91 const themeData = propsParsed.theme ? themes[propsParsed.theme] : themes[process.env.DEFAULT_THEME] 92 if (archive) setArchiveBaseUrl('https://'+archive) 93 const parentWork = type === 'work' && data.chapterInfo ? await getWork({workId: data.id}) : null 94 if (archive) resetArchiveBaseUrl() 95 const bfs = await Promise.all(baseFontData.defs.map(async (bf) => { 96 return { 97 name: baseFontData.displayName, 98 data: await readFile( 99 join(process.cwd(), bf.path) 100 ), 101 style: bf.style, 102 weight: bf.weight 103 } 104 })).then(x => x) 105 const tfs = await Promise.all(titleFontData.defs.map(async (tf) => { 106 return { 107 name: titleFontData.displayName, 108 data: await readFile( 109 join(process.cwd(), tf.path) 110 ), 111 style: tf.style, 112 weight: tf.weight 113 } 114 })).then(x => x) 115 const authorsFormatted = data.authors 116 ? data.authors.map((a) => { 117 if (a.anonymous) return "Anonymous" 118 if (a.pseud !== a.username) return `${a.pseud} (${a.username})` 119 return a.username 120 }) 121 : [] 122 const rating = type === 'work' ? await getHighestRating([data], archive) : await getHighestRating(data.works, archive) 123 const warning = type === 'work' ? await getHighestWarning([data], archive) : await getHighestWarning(data.works, archive) 124 const category = type === 'work' ? await getCategory([data], archive) : await getCategory(data.works, archive) 125 const authorString = authorsFormatted.length > 1 126 ? authorsFormatted.slice(0, -1).join(", ") + " & " + 127 authorsFormatted.slice(-1)[0] 128 : authorsFormatted[0] 129 const summaryContent = type === 'work' 130 ? (propsParsed.summaryType === 'chapter' && data.chapterInfo && data.chapterInfo.summary ? data.chapterInfo.summary : (propsParsed.summaryType === 'custom' && propsParsed.customSummary !== '' ? propsParsed.customSummary : (data.summary ? data.summary : (parentWork ? parentWork.summary : '')))) 131 : (propsParsed.summaryType === 'custom' && propsParsed.customSummary !== '' ? propsParsed.customSummary : data.notes) 132 const formatter = new Intl.NumberFormat('en-US') 133 const words = formatter.format(data.words) 134 const summaryDOM = new DOM(summaryContent, {decodeEntities: true}); 135 const summaryFormatted = summaryDOM.innerHTML.replace(/\<br(?: \/)?\>/g, "\n").replace( 136 /(<([^>]+)>)/ig, 137 "", 138 ).split("\n") 139 console.log(data) 140 const titleString = type === 'work' ? data.title : data.name 141 const chapterString = data.chapterInfo ? (data.chapterInfo.name 142 ? data.chapterInfo.name 143 : "Chapter " + data.chapterInfo.index) : null 144 const chapterCountString = type === 'work' ? (data.chapters 145 ? data.chapters.published+'/'+( 146 data.chapters.total 147 ? data.chapters.total 148 : '?' 149 ) 150 : '') : null 151 const fandomString = type === 'work' ? ( 152 data.fandoms.length > 1 153 ? ( 154 data.fandoms.length <= 2 155 ? data.fandoms.slice(0, -1).join(", ")+" & "+data.fandoms.slice(-1) 156 : data.fandoms.join(", ")+" (+"+(data.fandoms.length - 2)+")" 157 ) 158 : data.fandoms[0] 159 ) : ( 160 '' 161 ) 162 const charTags = type === 'work' ? data.tags.characters : data.works.map(w => w.tags.characters).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 163 const relTags = type === 'work' ? data.tags.relationships : data.works.map(w => w.tags.relationships).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 164 const freeTags = type === 'work' ? data.tags.additional : data.works.map(w => w.tags.additional).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 165 const warnings = type === 'work' ? data.tags.warnings : data.works.map(w => w.tags.warnings).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 166 167 return { 168 topLine: fandomString, 169 titleLine: titleString, 170 authorLine: authorString, 171 chapterLine: chapterString, 172 chapterCount: chapterCountString, 173 words: words, 174 rating: rating, 175 warning: warning, 176 category: category, 177 summary: summaryFormatted, 178 theme: themeData, 179 charTags: charTags, 180 relTags: relTags, 181 freeTags: freeTags, 182 postedAt: type === 'work' ? data.publishedAt : data.startedAt, 183 updatedAt: data.updatedAt, 184 baseFont: baseFont, 185 titleFont: titleFont, 186 props: propsParsed, 187 opts: { 188 fonts: bfs.concat(tfs) 189 } 190 } 191}