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 "CNTW"
34 } else if (warningsUnique.length === 1 && warningsUnique[0] === "No Archive Warnings Apply") {
35 return "NW"
36 }
37 return "W"
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 categoriesJoined = categories.reduce((a, b) => { return a.concat(b) })
46 const categoriesUnique = categoriesJoined.filter((w, i) => { return i === categoriesJoined.indexOf(w) })
47
48 if (categoriesUnique.length === 1) {
49 if (categoriesUnique[0] === "F/F") return "F"
50 if (categoriesUnique[0] === "M/M") return "M"
51 if (categoriesUnique[0] === "F/M") return "FM"
52 if (categoriesUnique[0] === "Gen") return "G"
53 if (categoriesUnique[0] === "Multi") return "MX"
54 if (categoriesUnique[0] === "Other") return "O"
55 }
56 return "MX"
57}
58
59export default async function sanitizeData ({ type, data, props}) {
60 const baseFont = props.has('baseFont') ? props.get('baseFont') : process.env.DEFAULT_BASE_FONT
61 const baseFontData = baseFonts[baseFont]
62 const titleFont = props.has('titleFont') ? props.get('titleFont') : process.env.DEFAULT_TITLE_FONT
63 const titleFontData = titleFonts[titleFont]
64 const themeData = props.has('theme') ? themes[props.get('theme')] : themes[process.env.DEFAULT_THEME]
65 const parentWork = type === 'work' && data.chapterInfo ? await getWork({workId: data.id}) : null
66 const bfs = await Promise.all(baseFontData.defs.map(async (bf) => {
67 return {
68 name: baseFontData.displayName,
69 data: await readFile(
70 join(process.cwd(), bf.path)
71 ),
72 style: bf.style,
73 weight: bf.weight
74 }
75 })).then(x => x)
76 const tfs = await Promise.all(titleFontData.defs.map(async (tf) => {
77 return {
78 name: titleFontData.displayName,
79 data: await readFile(
80 join(process.cwd(), tf.path)
81 ),
82 style: tf.style,
83 weight: tf.weight
84 }
85 })).then(x => x)
86 const authorsFormatted = data.authors
87 ? data.authors.map((a) => {
88 if (a.anonymous) return "Anonymous"
89 if (a.pseud !== a.username) return `${a.pseud} (${a.username})`
90 return a.username
91 })
92 : []
93 const rating = type === 'work' ? await getHighestRating([data]) : await getHighestRating(data.works)
94 const warning = type === 'work' ? await getHighestWarning([data]) : await getHighestWarning(data.works)
95 const category = type === 'work' ? await getCategory([data]) : await getCategory(data.works)
96 const authorString = authorsFormatted.length > 1
97 ? authorsFormatted.slice(0, -1).join(", ") + " & " +
98 authorsFormatted.slice(-1)[0]
99 : authorsFormatted[0]
100 const summaryContent = type === 'work'
101 ? (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 : ''))))
102 : (props.get('summaryType') === 'custom' && props.has('customSummary') ? props.get('customSummary') : data.notes)
103 const formatter = new Intl.NumberFormat('en-US')
104 const words = formatter.format(data.words)
105 const summaryDOM = new DOM(summaryContent, {decodeEntities: true});
106 const summaryFormatted = summaryDOM.innerHTML.replace(/\<br(?: \/)?\>/g, "\n").replace(
107 /(<([^>]+)>)/ig,
108 "",
109 ).split("\n")
110 const titleString = type === 'work' ? data.title : data.name
111 const chapterString = data.chapterInfo ? (data.chapterInfo.name
112 ? data.chapterInfo.name
113 : "Chapter " + data.chapterInfo.index) : null
114 const chapterCountString = type === 'work' ? (data.chapters
115 ? data.chapters.published+'/'+(
116 data.chapters.total
117 ? data.chapters.total
118 : '?'
119 )
120 : '') : null
121 const fandomString = type === 'work' ? (
122 data.fandoms.length > 1
123 ? (
124 data.fandoms.length <= 2
125 ? data.fandoms.slice(0, -1).join(", ")+" & "+data.fandoms.slice(-1)
126 : data.fandoms.join(", ")+" (+"+(data.fandoms.length - 2)+")"
127 )
128 : data.fandoms[0]
129 ) : (
130 ''
131 )
132 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) })
133 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) })
134 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) })
135 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) })
136
137 return {
138 topLine: fandomString,
139 titleLine: titleString,
140 authorLine: authorString,
141 chapterLine: chapterString,
142 chapterCount: chapterCountString,
143 words: words,
144 rating: rating,
145 warning: warning,
146 category: category,
147 summary: summaryFormatted,
148 theme: themeData,
149 charTags: charTags,
150 relTags: relTags,
151 freeTags: freeTags,
152 postedAt: type === 'work' ? data.publishedAt : data.startedAt,
153 updatedAt: data.updatedAt,
154 baseFont: baseFont,
155 titleFont: titleFont,
156 props: props,
157 opts: {
158 fonts: bfs.concat(tfs)
159 }
160 }
161}