diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 8027606e..dc7d51e8 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -1,23 +1,20 @@ import Head from 'next/head' +import Image from 'next/image' import { App } from 'src/components/App' import { tail } from 'lodash' import { Page } from 'src/components/Page' import { Markdown } from 'src/components/Markdown' import { Meta } from 'src/components/Meta' import { MetaBuilder } from 'src/lib/meta' -import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' +import { createApiUrl, requestGet, IResponseItems, apiGet } from 'src/next-lib' import { NextPage } from 'next' import { IBreadcumbsPart, IMeta, IPage, ITag, FileTokenData, Token } from 'src/types' import { joinTokens } from 'src/lib/tokens' import { size, ext } from 'src/lib/file' import { Html } from 'src/components/Html' import { Youtube } from '@/components/Youtube' - -function array(value: T | T[]) { - return Array.isArray(value) - ? value - : [value] -} +import { createPage } from '@/remote/factory' +import { asArray } from '@/remote/lib' const File: React.SFC = props => { const fileSize = size(props.file_size) @@ -27,7 +24,11 @@ const File: React.SFC = props => {
- {props['title']} +
@@ -107,9 +108,11 @@ const Index: NextPage = props => ( return (
- {x.data.alt}
{x.data.caption}
@@ -137,14 +140,19 @@ const Index: NextPage = props => ( export const getStaticProps = async (ctx: any) => { let slug = null if (ctx.query) { - slug = '/' + array(ctx.query.slug).join('/') + slug = '/' + asArray(ctx.query.slug).join('/') } else { - slug = '/' + array(ctx.params.slug).join('/') + slug = '/' + asArray(ctx.params.slug).join('/') } - const page = await requestGet(createApiUrl(`/api/page?page=${slug}`), null) + + let apiUrl = `https://hudozka.tmshv.com/pages?slug=${slug}` + const page = await apiGet(createPage)(apiUrl, null) if (!page) { - throw new Error(`Not found: ${slug}`) + return { + notFound: true, + } } + const description = page.description ?? undefined const breadcrumbSize = page?.breadcrumb?.length ?? 0 const breadcrumb = breadcrumbSize < 2 ? null : page.breadcrumb diff --git a/src/lib/image.ts b/src/lib/image.ts index 70bf0413..2918f139 100644 --- a/src/lib/image.ts +++ b/src/lib/image.ts @@ -3,14 +3,16 @@ import { ImageSize } from 'src/types' export type ResizeOptions = { width: number height: number + n?: number } export function getResizedUrl(src: string, options: ResizeOptions): string { + const n = options.n ?? 1 if (!src || src === '') { return null } - return `https://images.weserv.nl/?url=${src}&w=${options.width}&h=${options.height}&n=-1` + return `http://images.weserv.nl/?url=${src}&w=${options.width}&h=${options.height}&n=${n}` } export function imageSrcSet(src: string, sizes: ImageSize[]): string { diff --git a/src/next-lib.ts b/src/next-lib.ts index 84efdcf0..0902de19 100644 --- a/src/next-lib.ts +++ b/src/next-lib.ts @@ -36,3 +36,14 @@ export function wrapInitialProps(fn: (ctx: NextPageContext) => void) { export function createApiUrl(path: string) { return `https://api.tmshv.com/hudozka${path}` } + +export function apiGet(factory: (response: I) => O) { + return async (url: string, defaultResponse: O) => { + const res = await requestGet(url, null) + if (!res) { + return defaultResponse + } + + return factory(res) + } +} diff --git a/src/remote/factory.ts b/src/remote/factory.ts new file mode 100644 index 00000000..e6d36b6e --- /dev/null +++ b/src/remote/factory.ts @@ -0,0 +1,89 @@ +import { getResizedUrl, imageSrc } from "@/lib/image"; +import { IPage, Token } from "@/types"; +import { asItem } from "./lib"; +import { StrapiComponent, StrapiPage } from "./types"; + +export function createPageTokens(components: StrapiComponent[]): Token[] { + return components.map(component => { + switch (component.__component) { + case 'hudozka.text': { + return { + token: 'text', + data: component.text, + } + } + + case 'hudozka.image': { + return { + token: 'image', + data: { + alt: component.caption, + caption: component.caption, + src: component.media.url, + width: component.media.width, + height: component.media.height, + } + } + } + + case 'hudozka.document': { + return { + token: 'file', + data: { + url: component.media.url, + slug: 'jopa', + image_url: getResizedUrl(component.media.url, { + width: 200, + height: 200, + n: 1, + }), + file_url: component.media.url, + title: component.title, + file_size: component.media.size * 1000, + // file_size: component.media.size, + file_format: component.media.mime, + } + } + } + + default: + return { + token: 'text', + data: ` + ${JSON.stringify(component)} + `, + } + } + }) +} + +export function createPage(res: StrapiPage | StrapiPage[]): IPage { + const item = asItem(res) + + return { + title: item.title, + description: item.excerpt, + id: `${item.id}`, + url: item.slug, + + data: '', + date: '', + cover: { + src: item.cover.url, + width: item.cover.width, + height: item.cover.height, + }, + tokens: [ + { + token: 'text', + data: `# ${item.title}`, + }, + + ...createPageTokens(item.content) + .filter(Boolean), + ], + tags: [], + // breadcrumb?: [], + featured: false, + } +} \ No newline at end of file diff --git a/src/remote/lib.ts b/src/remote/lib.ts new file mode 100644 index 00000000..c3b6db0a --- /dev/null +++ b/src/remote/lib.ts @@ -0,0 +1,11 @@ +export function asArray(value: T | T[]) { + return Array.isArray(value) + ? value + : [value] +} + +export function asItem(value: T | T[]) { + return Array.isArray(value) + ? value[0] + : value +} diff --git a/src/remote/types.ts b/src/remote/types.ts new file mode 100644 index 00000000..70529d4e --- /dev/null +++ b/src/remote/types.ts @@ -0,0 +1,113 @@ +type StrapiMediaObject = { + url: string, + name: string, + hash: string, + ext: string, + mime: string, + width: number, + height: number, + size: number, + // "path": null, +} + +type StrapiMedia = StrapiMediaObject & { + id: number, + alternativeText: string | null, + caption: string | null, + // "previewUrl": null, + // "provider": "digitalocean", + // "provider_metadata": null, + // "created_at": "2021-01-31T20:55:04.027Z", + // "updated_at": "2021-01-31T20:55:04.027Z" + formats: null | { + thumbnail: StrapiMediaObject, + large: StrapiMediaObject, + medium: StrapiMediaObject, + small: StrapiMediaObject, + }, +} + +type StrapiComponentText = { + __component: "hudozka.text", + id: number, + text: string, +} + +type StrapiComponentImage = { + __component: "hudozka.image", + id: number, + wide: boolean, + caption: string, + media: StrapiMedia, +} + +type StrapiComponentDocument = { + __component: "hudozka.document", + id: number, + title: string, + media: StrapiMediaObject + // "media": { + // "id": 210, + // "name": "Положение.pdf", + // // "alternativeText": null, + // // "caption": null, + // // "width": null, + // // "height": null, + // // "formats": null, + // // "hash": "Polozhenie_efaf157737", + // // "ext": ".pdf", + // // "mime": "application/pdf", + // // "size": 103.79, + // "url": "https://hudozkacdn.tmshv.com/Polozhenie_efaf157737.pdf", + // // "previewUrl": null, + // // "provider": "digitalocean", + // // "provider_metadata": null, + // // "created_at": "2021-01-31T21:14:03.158Z", + // // "updated_at": "2021-01-31T21:14:03.158Z", + // } +} + +type StrapiComponentEmbed = { + __component: "hudozka.embed", + id: number, + src: string, +} + +export type StrapiComponent = + | StrapiComponentText + | StrapiComponentImage + | StrapiComponentDocument + | StrapiComponentEmbed + +export type StrapiPage = { + id: number, + title: string, + excerpt: string, + slug: string, + date?: string, + cover?: StrapiMedia, + published_at: string, + created_at: string, + updated_at: string, + content: StrapiComponent[], + // "tags": [ + // { + // "id": 5, + // "slug": "hudozka", + // "name": "Художка", + // "page": 114, + // "created_at": "2021-01-31T20:38:39.373Z", + // "updated_at": "2021-02-04T04:04:34.943Z" + // }, + // { + // "id": 6, + // "slug": "public-space", + // "name": "Среда", + // "page": 114, + // "created_at": "2021-01-31T20:38:39.722Z", + // "updated_at": "2021-02-04T04:04:34.943Z" + // } + // ], + // "breadcrumbs": [], + // "breadcrumbRelations": [] +} \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 47d4867c..c3b8bcdb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -115,6 +115,8 @@ export type ImageToken = { src: string alt: string caption: string + width: number + height: number } } @@ -124,8 +126,8 @@ export type FileTokenData = { image_url: string file_url: string title: string - file_size: number - file_format: 'application/pdf' + file_size?: number + file_format: string } export type FileToken = {