Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass MDX frontmatter title into page <title> #292

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/example/src/pages/components/markdown.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
title: Markdown
description: 'Carbon Gatsby theme markdown documentation'
keywords: 'ibm,carbon,gatsby,mdx,markdown'
---

<PageDescription>
Expand All @@ -21,6 +23,18 @@ For most pages, we recommend starting with a `PageDescription` followed by `Anch
<AnchorLink>Blockquotes</AnchorLink>
</AnchorLinks>

## MDX Frontmatter

You can declare frontmatter in your `.mdx` files to provide specific metadata for the theme to use. The most important of which is the `title`. You can also provide a descriptions and keywords which will be added to the `head` of your document.

```md
---
title: Markdown
description: 'Carbon Gatsby theme markdown documentation'
keywords: 'ibm,carbon,gatsby,mdx,markdown'
---
```

## Text decoration

Emphasis, aka italics, with _asterisks_ or _underscores_.
Expand Down
9 changes: 8 additions & 1 deletion packages/example/src/pages/guides/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gatsby themes allow you to override configuration from the theme by defining the
<AnchorLink>Image Compression</AnchorLink>
<AnchorLink>Global Search</AnchorLink>
<AnchorLink>Edit on Github link</AnchorLink>
<AnchorLink>Other options</AnchorLink>
</AnchorLinks>

## Site Title and Description
Expand Down Expand Up @@ -148,14 +149,20 @@ To add a link to the bottom of each page that points to the current page source

- `additionalFontWeights` – add support for additional Plex font weights. Don't forget to specify italics for the additional weights if needed.
- `mdxExtensions` – change the file extensions processed by `gatsby-mdx` (default ['.mdx', '.md'])
- `titleType` – pick between three formats for the `<title>` element for your site. Here are the three options using this page as an example:

- `page`: "Configuration" (default)
- `site`: "Gatsby Theme Carbon"
- `append`: "Gatsby Theme Carbon – Configuration"

```js
plugins: [
{
resolve: 'gatsby-theme-carbon',
options: {
additionalFontWeights: ['200', '200i'],
mdxExtensions: ['.mdx']
mdxExtensions: ['.mdx'],
titleType: 'append'
},
},
],
Expand Down
1 change: 0 additions & 1 deletion packages/example/src/pages/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import HomepageTemplate from 'gatsby-theme-carbon/src/templates/Homepage';

export default HomepageTemplate;

## MDX content starts here
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-theme-carbon/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ exports.onPreBootstrap = ({ store, reporter }) => {
// We need to provide the actual file that created a specific page to append links for EditLink.
// We can't do page queries from MDX templates, so we'll add the page's relative path to context after it's created.
// The context object **is** supplied to MDX templates through the pageContext prop.
exports.onCreatePage = ({ page, actions }) => {
exports.onCreatePage = ({ page, actions }, pluginOptions) => {
const { titleType = 'page' } = pluginOptions;
const { createPage, deletePage } = actions;
const [relativePagePath] = page.componentPath.split('src/pages').splice('-1');
deletePage(page);
Expand All @@ -30,6 +31,7 @@ exports.onCreatePage = ({ page, actions }) => {
context: {
...page.context,
relativePagePath,
titleType,
},
});
};
18 changes: 16 additions & 2 deletions packages/gatsby-theme-carbon/src/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ import Container from './Container';

import '../styles/index.scss';

const Layout = ({ children, homepage, shouldHideHeader, ...rest }) => {
const Layout = ({
children,
homepage,
shouldHideHeader,
titleType,
pageTitle,
pageDescription,
pageKeywords,
...rest
}) => {
const is404 = children.key === null;

useLayoutEffect(() => {
Expand All @@ -27,7 +36,12 @@ const Layout = ({ children, homepage, shouldHideHeader, ...rest }) => {

return (
<>
<Meta />
<Meta
titleType={titleType}
pageTitle={pageTitle}
pageDescription={pageDescription}
pageKeywords={pageKeywords}
/>
<Header shouldHideHeader={shouldHideHeader} />
<Switcher />
<LeftNav
Expand Down
23 changes: 19 additions & 4 deletions packages/gatsby-theme-carbon/src/components/Meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@ import React from 'react';
import { Helmet } from 'react-helmet';
import { useMetadata } from '../util/hooks';

const Meta = () => {
const Meta = ({ pageTitle, pageDescription, pageKeywords, titleType }) => {
const { title, description, keywords } = useMetadata();

const getPageTitle = () => {
switch (titleType) {
case 'page':
// use site title for fallback
return pageTitle || title;
case 'site':
return title;
case 'append':
return `${title}${pageTitle ? ` – ${pageTitle}` : ''}`;
default:
return null;
}
};

return (
<Helmet
title={title}
title={getPageTitle()}
meta={[
{
name: 'description',
content: description,
content: pageDescription || description,
},
{
name: 'keywords',
content: keywords,
content: pageKeywords || keywords,
},
]}
/>
Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby-theme-carbon/src/templates/Default.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import PageTabs from '../components/PageTabs';
import Main from '../components/Main';

const Default = ({ pageContext, children, location }) => {
const { frontmatter = {}, relativePagePath } = pageContext;
const { tabs, title } = frontmatter;
const { frontmatter = {}, relativePagePath, titleType } = pageContext;
const { tabs, title, description, keywords } = frontmatter;
const scrollDirection = useScrollDirection();
const shouldHideHeader = !!tabs && scrollDirection === 'down';

Expand All @@ -41,7 +41,14 @@ const Default = ({ pageContext, children, location }) => {

const currentTab = getCurrentTab();
return (
<Layout shouldHideHeader={shouldHideHeader} homepage={false}>
<Layout
shouldHideHeader={shouldHideHeader}
homepage={false}
pageTitle={title}
pageDescription={description}
pageKeywords={keywords}
titleType={titleType}
>
<PageHeader
shouldHideHeader={shouldHideHeader}
title={title}
Expand Down
31 changes: 20 additions & 11 deletions packages/gatsby-theme-carbon/src/templates/Homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ const Homepage = ({
SecondCallout,
location,
pageContext,
}) => (
<Layout homepage>
{Banner}
{FirstCallout}
<Main>{children}</Main>
{SecondCallout}
<NextPrevious location={location} pageContext={pageContext} />
<WebsiteBackToTopBtn />
</Layout>
);

}) => {
const { frontmatter = {}, titleType } = pageContext;
const { title, description, keywords } = frontmatter;
return (
<Layout
pageTitle={title}
pageDescription={description}
pageKeywords={keywords}
titleType={titleType}
homepage
>
{Banner}
{FirstCallout}
<Main>{children}</Main>
{SecondCallout}
<NextPrevious location={location} pageContext={pageContext} />
<WebsiteBackToTopBtn />
</Layout>
);
};
Homepage.defaultProps = {
Banner: (
<HomepageBanner
Expand Down
Loading