import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
export default async function fetch(slug: string) {
const url = `https://go-api.twreporter.org/v2/posts/${slug}?full=true`;
const res = await got(url);
const post = res.data.data;
const time = post.published_date;
// For `writers`
let authors = '';
if (post.writers) {
authors = post.writers.map((writer) => (writer.job_title ? writer.job_title + ' / ' + writer.name : '文字 / ' + writer.name)).join(',');
}
// For `photography`, if it exists
let photographers = '';
if (post.photographers) {
photographers = post.photographers
.map((photographer) => {
let title = '攝影 / ';
if (photographer.job_title) {
title = photographer.job_title + ' / ';
}
return title + photographer.name;
})
.join(',');
authors += ';' + photographers;
}
const bannerImage = post.og_image.resized_targets.desktop.url;
const caption = post.leading_image_description;
const bannerDescription = post.og_image.description;
const ogDescription = post.og_description;
const banner = art(path.join(__dirname, 'templates/image.art'), { image: bannerImage, description: bannerDescription, caption });
function format(type, content) {
let block = '';
if (content !== '' && type !== 'embeddedcode') {
switch (type) {
case 'image':
case 'slideshow':
block = content.map((image) => art(path.join(__dirname, 'templates/image.art'), { image: image.desktop.url, description: image.description, caption: image.description })).join('
');
break;
case 'blockquote':
block = `
${content}`; break; case 'header-one': block = `