Skip to content

Commit

Permalink
AMP page layout
Browse files Browse the repository at this point in the history
  • Loading branch information
seth2810 committed Sep 15, 2022
1 parent 9ca04c8 commit 5b681b4
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 20 deletions.
7 changes: 5 additions & 2 deletions next-app/components/AmpLayout.jsx
Expand Up @@ -3,15 +3,18 @@ import Head from 'next/head';

import cfg from '../data/config.js';

// inject AMP directly because Next.js supports only css-in-js fro AMP
import ampCSS from '!!raw-loader!sass-loader!../styles/amp.scss';

const AmpLayout = ({ title, children }) => {
const fullTitle = title ? `${title} | ${cfg.title}` : cfg.title;

return (
<>
<Head>
<title>{fullTitle}</title>
<link rel="icon" href={cfg.favicon}></link>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" />
<style amp-custom="true" dangerouslySetInnerHTML={{ __html: ampCSS }}></style>
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
</Head>
<div className="markdown md:markdown-lg lg:markdown-xl">{children}</div>
</>
Expand Down
35 changes: 35 additions & 0 deletions next-app/components/AmpPostPageInfo.jsx
@@ -0,0 +1,35 @@
/* eslint-disable @next/next/no-img-element */
import React from 'react';
import Link from 'next/link';
import { MDXRemote } from 'next-mdx-remote';
import { useTranslation } from 'next-i18next';

import Banner from './Banner.jsx';

const components = {
Banner,
img: ({ src }) => <amp-img src={src} layout="responsive" height="0.6" width="1" />,
};

const AmpPostPageInfo = ({ post, disqus }) => {
const href = {
pathname: '/[name]',
query: {
name: post.name,
},
};

const { t } = useTranslation('post');

return (
<>
<h1>{post.header}</h1>
<Link className="" href={href}>
<a className="full_post_link">{t('amp.full_post_link')}</a>
</Link>
<MDXRemote compiledSource={post.content} components={components} />
</>
);
};

export default AmpPostPageInfo;
103 changes: 98 additions & 5 deletions next-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion next-app/package.json
Expand Up @@ -36,6 +36,8 @@
},
"devDependencies": {
"eslint": "^8.22.0",
"eslint-config-next": "^12.2.5"
"eslint-config-next": "^12.2.5",
"raw-loader": "^4.0.2",
"sass-loader": "^13.0.2"
}
}
14 changes: 2 additions & 12 deletions next-app/pages/amp/[name].js
@@ -1,28 +1,18 @@
import Link from 'next/link';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';

import { i18n } from '../../next-i18next.config.js';
import { findPost, getPublishedPosts } from '../../api/index.js';
import AmpLayout from '../../components/AmpLayout.jsx';
import AmpPostPageInfo from '../../components/AmpPostPageInfo.jsx';

const AmpPost = ({ post }) => {
if (!post) {
return null;
}

const href = {
pathname: '/[name]',
query: {
name: post.name,
},
}

return (
<AmpLayout title={post.header}>
<h1>{post.header}</h1>
<Link className="full_post_link" href={href}>
Читать в полной версии →
</Link>
<AmpPostPageInfo post={post} />
</AmpLayout>
);
};
Expand Down
3 changes: 3 additions & 0 deletions next-app/public/locales/en/post.json
Expand Up @@ -3,5 +3,8 @@
"source_code": "Source code (github)",
"next_guide": "Read next Guide",
"prev_guide": "Read previous Guide"
},
"amp": {
"full_post_link": "Read full version"
}
}
3 changes: 3 additions & 0 deletions next-app/public/locales/ru/post.json
Expand Up @@ -3,5 +3,8 @@
"source_code": "Исходный код (github)",
"next_guide": "Следующая статья",
"prev_guide": "Предыдущая статья"
},
"amp": {
"full_post_link": "Читать в полной версии"
}
}

0 comments on commit 5b681b4

Please sign in to comment.