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

Add anchors to h1's in docs #3892

Merged
merged 2 commits into from
Feb 7, 2018
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
8 changes: 8 additions & 0 deletions www/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const fs = require(`fs-extra`)
const slash = require(`slash`)
const slugify = require(`limax`)

// convert a string like `/some/long/path/name-of-docs/` to `name-of-docs`
const slugToAnchor = slug =>
slug
.split(`/`) // split on dir separators
.filter(item => item !== ``) // remove empty values
.pop() // take last item

exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -170,6 +177,7 @@ exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
createNodeField({ node, name: `package`, value: true })
}
if (slug) {
createNodeField({ node, name: `anchor`, value: slugToAnchor(slug) })
createNodeField({ node, name: `slug`, value: slug })
}
} else if (node.internal.type === `AuthorYaml`) {
Expand Down
4 changes: 3 additions & 1 deletion www/src/pages/docs/bound-action-creators.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class ActionCreatorsDocs extends React.Component {
<Helmet>
<title>Bound Action Creators</title>
</Helmet>
<h1 css={{ marginTop: 0 }}>Bound Action Creators</h1>
<h1 id="bound-action-creators" css={{ marginTop: 0 }}>
Bound Action Creators
</h1>
<p>
Gatsby uses
{` `}
Expand Down
4 changes: 3 additions & 1 deletion www/src/pages/docs/browser-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class BrowserAPIDocs extends React.Component {
<Helmet>
<title>Browser APIs</title>
</Helmet>
<h1 css={{ marginTop: 0 }}>Gatsby Browser APIs</h1>
<h1 id="browser-apis" css={{ marginTop: 0 }}>
Gatsby Browser APIs
</h1>
<h2 css={{ marginBottom: rhythm(1 / 2) }}>Usage</h2>
<p css={{ marginBottom: rhythm(1) }}>
Implement any of these APIs by exporting them from a file named{` `}
Expand Down
10 changes: 6 additions & 4 deletions www/src/pages/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class IndexRoute extends React.Component {
<Helmet>
<title>Docs</title>
</Helmet>
<h1 css={{ marginTop: 0 }}>Get started</h1>
<h1 id="get-started" css={{ marginTop: 0 }}>
Get started
</h1>
<p>Gatsby is a blazing-fast static site generator for React.</p>
<h2>Install Gatsby{`'`}s command line tool</h2>
<p>
<code>npm install --global gatsby-cli</code>
</p>
<h2>Using the Gatsby CLI</h2>
<h2 id="using-the-gatsby-cli">Using the Gatsby CLI</h2>
<ol>
<li>
Create a new site.
Expand Down Expand Up @@ -48,7 +50,7 @@ class IndexRoute extends React.Component {
testing your built site.
</li>
</ol>
<h2>Using other starters</h2>
<h2 id="using-other-starters">Using other starters</h2>
<p>
Running <code>gatsby new</code> installs the default Gatsby starter.
There are{` `}
Expand All @@ -58,7 +60,7 @@ class IndexRoute extends React.Component {
{` `}
you can use to kickstart building your Gatsby site.
</p>
<h2>Work through the tutorial</h2>
<h2 id="work-through-the-tutorial">Work through the tutorial</h2>
<p>
It walks you through building a Gatsby site from scratch to a finished
polished site.
Expand Down
4 changes: 3 additions & 1 deletion www/src/pages/docs/node-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class NodeAPIDocs extends React.Component {
<Helmet>
<title>Node APIs</title>
</Helmet>
<h1 css={{ marginTop: 0 }}>Gatsby Node APIs</h1>
<h1 id="gatsby-node-apis" css={{ marginTop: 0 }}>
Gatsby Node APIs
</h1>
<p>
Gatsby gives plugins and site builders many APIs for controlling your
site.
Expand Down
4 changes: 3 additions & 1 deletion www/src/pages/docs/ssr-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class SSRAPIs extends React.Component {
<Helmet>
<title>SSR APIs</title>
</Helmet>
<h1 css={{ marginTop: 0 }}>Gatsby Server Rendering APIs</h1>
<h1 id="gatsby-server-rendering-apis" css={{ marginTop: 0 }}>
Gatsby Server Rendering APIs
</h1>
<h2 css={{ marginBottom: rhythm(1 / 2) }}>Usage</h2>
<p css={{ marginBottom: rhythm(1) }}>
Implement any of these APIs by exporting them from a file named{` `}
Expand Down
8 changes: 7 additions & 1 deletion www/src/templates/template-docs-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class DocsTemplate extends React.Component {
<meta name="twitter.label1" content="Reading time" />
<meta name="twitter:data1" content={`${page.timeToRead} min read`} />
</Helmet>
<h1 css={{ marginTop: 0 }}>{page.frontmatter.title}</h1>
<h1 id={page.fields.anchor} css={{ marginTop: 0 }}>
{page.frontmatter.title}
</h1>
<div
dangerouslySetInnerHTML={{
__html: page.html,
Expand All @@ -40,6 +42,10 @@ export const pageQuery = graphql`
html
excerpt
timeToRead
fields {
slug
anchor
}
frontmatter {
title
}
Expand Down