nfp_moe: Fixed few bugs, fixed fallback posters, fixed metadata, numerous other slight changes.

This commit is contained in:
Jonatan Nilsson 2022-08-22 17:17:39 +00:00
parent bf2080fd63
commit 1c0995f4da
10 changed files with 1149 additions and 1106 deletions

View file

@ -22,7 +22,7 @@ export default class ServeHandler {
} }
let indexFile = fsSync.readFileSync(path.join(this.root, 'index.html')) let indexFile = fsSync.readFileSync(path.join(this.root, 'index.html'))
this.template = dot.template(indexFile.toString(), { argName: ['headerDescription', 'headerImage', 'headerTitle', 'headerUrl', 'payloadData', 'payloadTree', 'version', 'nonce', 'type', 'banner'] }) this.template = dot.template(indexFile.toString(), { argName: ['headerDescription', 'headerImage', 'headerTitle', 'headerUrl', 'payloadData', 'payloadTree', 'version', 'nonce', 'type', 'banner', 'media', 'in_debug'] })
// console.log(indexFile.toString()) // console.log(indexFile.toString())
} }

View file

@ -1,7 +1,9 @@
import path from 'path' import path from 'path'
import striptags from 'striptags'
import Parent from '../base/serve.mjs' import Parent from '../base/serve.mjs'
import fs from 'fs/promises' import fs from 'fs/promises'
import dot from 'dot' import dot from 'dot'
import config from '../base/config.mjs'
export default class ServeHandler extends Parent { export default class ServeHandler extends Parent {
traverseTree(set, tree) { traverseTree(set, tree) {
@ -13,9 +15,42 @@ export default class ServeHandler extends Parent {
} }
} }
getDescriptionFromBlocks(blocks) {
return encodeURI(blocks
.filter(x => x.type === 'header'
|| x.type === 'paragraph'
|| x.type === 'quote'
|| x.type === 'list'
|| x.type === 'code'
|| x.type === 'htmlraw')
.map(x => {
if (x.type === 'htmlraw') {
return striptags(x.data.html)
}
if (x.type === 'quote') {
return `"${x.data.text}"` + (x.data.caption ? ` - ${x.data.caption}` : '')
}
if (x.type === 'list') {
return x.data.items.join('. ')
}
return x.data.code ? `"${x.data.code}"` : x.data.text
})
.map(x => striptags(x))
.map(x => {
console.log(x)
return x
})
.filter(x => x)
.join('. ')
.replace('/\.+/g', '.')
)
}
async serveIndex(ctx) { async serveIndex(ctx) {
let indexFile = await fs.readFile(path.join(this.root, 'index.html')) if (config.get('NODE_ENV') === 'development') {
this.template = dot.template(indexFile.toString(), { argName: ['headerDescription', 'headerImage', 'headerTitle', 'headerUrl', 'payloadData', 'payloadTree', 'version', 'nonce', 'type', 'banner', 'media'] }) let indexFile = await fs.readFile(path.join(this.root, 'index.html'))
this.template = dot.template(indexFile.toString(), { argName: ['headerDescription', 'headerImage', 'headerTitle', 'headerUrl', 'payloadData', 'payloadTree', 'version', 'nonce', 'type', 'banner', 'media', 'in_debug'] })
}
let payload = { let payload = {
headerDescription: 'Small fansubbing and scanlation group translating and encoding our favourite shows from Japan.', headerDescription: 'Small fansubbing and scanlation group translating and encoding our favourite shows from Japan.',
@ -29,6 +64,7 @@ export default class ServeHandler extends Parent {
type: 'page', type: 'page',
banner: false, banner: false,
media: false, media: false,
in_debug: config.get('NODE_ENV') === 'development' && false,
} }
try { try {
@ -45,16 +81,36 @@ export default class ServeHandler extends Parent {
let data = await this.pageRoutes.getPage(ctx, true) let data = await this.pageRoutes.getPage(ctx, true)
if (!data.page) { if (!data.page) {
payload.type = 'frontpage' payload.type = 'frontpage'
} } else if (setOfBranches.has(data.page.id)) {
else if (setOfBranches.has(data.page.id)) {
payload.type = 'page_with_children' payload.type = 'page_with_children'
} }
if (data.page) {
payload.headerTitle = data.page.name + ' - NFP Moe'
if (data.page.content.blocks.length) {
payload.headerDescription = this.getDescriptionFromBlocks(data.page.content.blocks) || payload.headerDescription
}
if (data.page.media_alt_prefix) {
payload.headerImage = data.page.media_alt_prefix + '_small.jpg'
}
}
payload.media = data.page?.media_avif_preview || false payload.media = data.page?.media_avif_preview || false
payload.banner = data.featured?.banner_avif_preview || data.page?.banner_avif_preview || false payload.banner = data.featured?.banner_avif_preview || data.page?.banner_avif_preview || false
payload.payloadData = JSON.stringify(data) payload.payloadData = JSON.stringify(data)
} else if (ctx.url.startsWith('/article/') && ctx.url.lastIndexOf('/') === 8) { } else if (ctx.url.startsWith('/article/') && ctx.url.lastIndexOf('/') === 8) {
ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1) ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1)
let data = await this.articleRoutes.getArticle(ctx, true) let data = await this.articleRoutes.getArticle(ctx, true)
if (data.article) {
payload.headerTitle = data.article.name + ' - NFP Moe'
if (data.article.content.blocks.length) {
payload.headerDescription = this.getDescriptionFromBlocks(data.article.content.blocks) || payload.headerDescription
}
if (data.article.media_alt_prefix) {
payload.headerImage = data.article.media_alt_prefix + '_small.jpg'
}
}
payload.media = data.article?.media_avif_preview || false payload.media = data.article?.media_avif_preview || false
payload.payloadData = JSON.stringify(data) payload.payloadData = JSON.stringify(data)
payload.type = 'article' payload.type = 'article'

View file

@ -16,52 +16,6 @@ export function generatePictureSource(item, cover) {
} }
} }
let loadingImage = null
let loader = null
function cancelLoader() {
if (loader) {
loader.src = ''
}
loader = null
}
export function smartBanner(item) {
if (!item) {
if (loader) {
cancelLoader()
}
loadingImage = null
return null
}
if (!item.preview) {
loadingImage = null
cancelLoader()
return item.banner
}
if (loadingImage !== item.banner && loader) {
cancelLoader()
}
if (loadingImage === item.banner && !loader) {
return item.banner
}
if (loadingImage === item.banner) {
return item.preview
}
loadingImage = item.banner
loader = new Image();
loader.src = item.banner;
loader.onload = loader.onerror = function() {
loader = null
m.redraw()
}
return item.preview
}
export function getBannerImage(item, prefix) { export function getBannerImage(item, prefix) {
if (!item || !item.banner_alt_prefix) return null if (!item || !item.banner_alt_prefix) return null
@ -97,12 +51,12 @@ export function getArticlePicture(pictureData, useRouteLink, path, altText, fall
if (!pictureData) return fallback || null if (!pictureData) return fallback || null
return m(useRouteLink ? m.route.Link : 'a', { return m(useRouteLink ? m.route.Link : 'a', {
class: 'cover ' + (pictureData.preview ? 'haspreview' : ''), class: 'cover ' + (pictureData.preview && window.supportsavif ? 'haspreview' : ''),
rel: useRouteLink ? null : 'noopener', rel: useRouteLink ? null : 'noopener',
target: useRouteLink ? null : '_blank', target: useRouteLink ? null : '_blank',
href: path, href: path,
}, [ }, [
pictureData.preview ? m('img', { src: pictureData.preview }) : null, pictureData.preview && window.supportsavif ? m('img', { src: pictureData.preview }) : null,
m('picture', [ m('picture', [
m('source', { m('source', {
srcset: pictureData.avif, srcset: pictureData.avif,

View file

@ -228,7 +228,7 @@ const SitePage = {
? m(m.route.Link, { ? m(m.route.Link, {
class: 'page-banner', class: 'page-banner',
href: featuredBanner.path, href: featuredBanner.path,
style: { 'background-image': 'url("' + featuredBanner.preview + '")' }, style: window.supportsavif ? { 'background-image': 'url("' + featuredBanner.preview + '")' } : null,
}, [ }, [
m('div.page-banner-real', { m('div.page-banner-real', {
style: { 'background-image': 'url("' + featuredBanner.banner + '")' }, style: { 'background-image': 'url("' + featuredBanner.banner + '")' },
@ -240,7 +240,7 @@ const SitePage = {
? m('a.page-banner', { ? m('a.page-banner', {
href: pageBanner.original, href: pageBanner.original,
target: '_blank', target: '_blank',
style: { 'background-image': 'url("' + pageBanner.preview + '")' }, style: window.supportsavif ? { 'background-image': 'url("' + pageBanner.preview + '")' } : null,
}, },
m('div.page-banner-real', { m('div.page-banner-real', {
style: { 'background-image': 'url("' + pageBanner.banner + '")' }, style: { 'background-image': 'url("' + pageBanner.banner + '")' },

View file

@ -56,7 +56,8 @@
"flaska": "^1.3.0", "flaska": "^1.3.0",
"formidable": "^1.2.6", "formidable": "^1.2.6",
"msnodesqlv8": "^2.4.7", "msnodesqlv8": "^2.4.7",
"nconf-lite": "^2.0.0" "nconf-lite": "^2.0.0",
"striptags": "^3.2.0"
}, },
"devDependencies": { "devDependencies": {
"asbundle": "^2.6.1", "asbundle": "^2.6.1",

View file

@ -19,5 +19,5 @@
} }
.jpegonly .spritesheet { .jpegonly .spritesheet {
background-image: url("/assets/img/combined.png") background-image: url("/assets/img/combined.webp")
} }

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 236 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 KiB

File diff suppressed because one or more lines are too long