From 4fb41de0f12e870c6023f8df689b0520bdec5cad Mon Sep 17 00:00:00 2001 From: Pacharapol Withayasakpunt Date: Wed, 16 Dec 2020 09:29:57 +0700 Subject: [PATCH] full running --- packages/nuxt/app/router.scrollBehavior.ts | 3 ++ packages/nuxt/assets/global.ts | 37 +++++++++++++++-- packages/nuxt/assets/remark42.ts | 48 ++++++++++++++++++++++ packages/nuxt/layouts/default.vue | 7 +++- packages/nuxt/nuxt.config.ts | 13 +++++- packages/nuxt/pages/post/_.vue | 39 ++++++++++++++++++ packages/nuxt/pages/{ => tag}/_tag.vue | 0 packages/nuxt/plugins/remark42.client.js | 0 8 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 packages/nuxt/app/router.scrollBehavior.ts create mode 100644 packages/nuxt/assets/remark42.ts rename packages/nuxt/pages/{ => tag}/_tag.vue (100%) create mode 100644 packages/nuxt/plugins/remark42.client.js diff --git a/packages/nuxt/app/router.scrollBehavior.ts b/packages/nuxt/app/router.scrollBehavior.ts new file mode 100644 index 0000000..20930e1 --- /dev/null +++ b/packages/nuxt/app/router.scrollBehavior.ts @@ -0,0 +1,3 @@ +export default function () { + return { x: 0, y: 0 } +} diff --git a/packages/nuxt/assets/global.ts b/packages/nuxt/assets/global.ts index bc68ad2..f9e654c 100644 --- a/packages/nuxt/assets/global.ts +++ b/packages/nuxt/assets/global.ts @@ -2,15 +2,24 @@ import type { ITheme } from '~/server/theme' export const THEME: ITheme = JSON.parse(process.env.THEME!) -export const api = async ( +export async function api( url: string, opts: RequestInit & { params?: Record } = {} -) => { +): Promise< + | T + | { + error: { + statusCode: number + message: unknown + } + } +> { if (!url.startsWith('/api')) { return { error: { + statusCode: 500, message: 'malformed API route', }, } @@ -24,6 +33,9 @@ export const api = async ( baseURL.pathname = url Object.entries(opts.params || {}).map(([k, v]) => { if (v) { + /** + * Arrays will be comma separated, and empty strings will be discarded. + */ baseURL.searchParams.set(k, v) } }) @@ -34,8 +46,25 @@ export const api = async ( /** * Does not rely on `r.ok`, but rather, errorhandler */ - return r.json() - } catch (_) {} + const data = await r.json() + + /** + * Transform to Nuxt-compatible style + */ + if (data.error) { + return { + error: { + statusCode: r.status, + message: data, + }, + } + } + + return data + } catch (e) { + // eslint-disable-next-line no-console + console.error(e) + } return { error: { diff --git a/packages/nuxt/assets/remark42.ts b/packages/nuxt/assets/remark42.ts new file mode 100644 index 0000000..f4ea860 --- /dev/null +++ b/packages/nuxt/assets/remark42.ts @@ -0,0 +1,48 @@ +/* eslint-disable camelcase */ + +import { THEME } from './global' + +export type Theme = 'light' | 'dark' + +export interface CommentsConfig { + host: string + site_id: string + url?: string + max_shown_comments?: number + theme?: Theme + page_title?: string + node?: string | HTMLElement + locale?: string + // eslint-disable-next-line @typescript-eslint/no-explicit-any + __colors__?: any +} + +declare global { + interface Window { + remark_config: CommentsConfig + REMARK42: { + changeTheme(theme: Theme): void + destroy?: () => void + createInstance: ( + remark_config: CommentsConfig + ) => + | { + changeTheme(theme: Theme): void + destroy(): void + } + | undefined + } + } +} + +if (process.browser && THEME?.comment?.remark42) { + ;(function (c) { + for (let i = 0; i < c.length; i++) { + const d = document + const s = d.createElement('script') + s.src = THEME.comment.remark42.host + '/web/' + c[i] + '.js' + s.defer = true + ;(d.head || d.body).appendChild(s) + } + })(['embed']) +} diff --git a/packages/nuxt/layouts/default.vue b/packages/nuxt/layouts/default.vue index c8bb7e4..0e8e98b 100644 --- a/packages/nuxt/layouts/default.vue +++ b/packages/nuxt/layouts/default.vue @@ -4,8 +4,11 @@

{{ theme.title }}