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