From d6490bbe5c3c8fa8b3c620f6bb84ff8b60e35f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bry=C5=84ski?= Date: Tue, 14 Dec 2021 16:45:51 +0100 Subject: [PATCH] fix read time and remove demo pages --- pages/blog/[slug].tsx | 46 +++--- pages/demo/blog/[filename].js | 257 ---------------------------------- posts/test-article-3.mdx | 16 +-- 3 files changed, 24 insertions(+), 295 deletions(-) delete mode 100644 pages/demo/blog/[filename].js diff --git a/pages/blog/[slug].tsx b/pages/blog/[slug].tsx index f7b390c0c..ccbd5c512 100644 --- a/pages/blog/[slug].tsx +++ b/pages/blog/[slug].tsx @@ -1,6 +1,6 @@ import { GetStaticPropsContext, InferGetStaticPropsType } from 'next'; import Head from 'next/head'; -import React, { useEffect } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import styled from 'styled-components'; import { staticRequest } from 'tinacms'; import Container from 'components/Container'; @@ -17,9 +17,20 @@ import StructuredDataHead from 'views/SingleArticlePage/StructuredDataHead'; import { Posts, PostsDocument, Query } from '.tina/__generated__/types'; export default function SingleArticlePage(props: InferGetStaticPropsType) { + const contentRef = useRef(null); + const [readTime, setReadTime] = useState(''); + useEffect(() => { + calculateReadTime(); lazyLoadPrismTheme(); + function calculateReadTime() { + const currentContent = contentRef.current; + if (currentContent) { + setReadTime(getReadTime(currentContent.textContent || '')); + } + } + function lazyLoadPrismTheme() { const prismThemeLinkEl = document.querySelector('link[data-id="prism-theme"]'); @@ -38,7 +49,7 @@ export default function SingleArticlePage(props: InferGetStaticPropsType - +
@@ -126,35 +137,10 @@ export async function getStaticProps({ params }: GetStaticPropsContext<{ slug: s variables: variables, })) as { getPostsDocument: PostsDocument }; - const { title, description, date, tags, imageUrl, body } = data.getPostsDocument.data; - console.log(body); - - const meta = { title, description, date, tags, imageUrl }; - const serializedContent = await serializeContent(body || '', meta); + const { body } = data.getPostsDocument.data; return { - props: { slug, content: body || '', readTime: getReadTime(body || ''), variables, query, data }, + props: { slug, content: body || '', variables, query, data }, }; - - async function serializeContent(content: string, meta: Record) { - const { serialize } = await import('next-mdx-remote/serialize'); - return serialize(content, { - scope: meta, - mdxOptions: { - remarkPlugins: [ - // @ts-ignore - await import('@fec/remark-a11y-emoji'), - await import('remark-breaks'), - await import('remark-gfm'), - await import('remark-footnotes'), - await import('remark-external-links'), - await import('remark-slug'), - // @ts-ignore - await import('remark-sectionize'), - ], - rehypePlugins: [], - }, - }); - } } const CustomContainer = styled(Container)` diff --git a/pages/demo/blog/[filename].js b/pages/demo/blog/[filename].js deleted file mode 100644 index fe4fabf7d..000000000 --- a/pages/demo/blog/[filename].js +++ /dev/null @@ -1,257 +0,0 @@ -// THIS FILE HAS BEEN GENERATED WITH THE TINA CLI. - // This is a demo file once you have tina setup feel free to delete this file - - import { staticRequest, gql, getStaticPropsForTina } from "tinacms"; - import Head from "next/head"; - import { createGlobalStyle } from "styled-components"; - - // Styles for markdown - const GlobalStyle = createGlobalStyle` - h1,h2,h3,h4,h5 { - margin-bottom: 1.5rem; - margin-top: 1.5rem; - } - blockquote { - background-color: rgb(209,250,229); - } - h1 { - font-size: 45px; - } - h2 { - font-size: 35px; - } - h3 { - font-size: 25px; - } - h4 { - font-size: 22px; - } - ul { - padding-left: 0; - } - li { - list-style-type: none; - } - a { - font-weight: bold; - color: rgb(59,130,246); - text-decoration: underline; - } - `; - const defaultMarked = (markdown) => markdown; - // Use the props returned by get static props (this can be deleted when the edit provider and tina-wrapper are moved to _app.js) - const BlogPage = (props) => { - return ( - <> - - {/* Tailwind CDN */} - - {/* Marked CDN */} -