doc.silverstripe.org/src/templates/docs-template.tsx

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-11-12 04:09:29 +01:00
import React, { StatelessComponent, ReactElement, useEffect } from 'react';
2019-11-08 03:40:20 +01:00
import { graphql } from 'gatsby';
import SEO from '../components/SEO';
import { SingleFileQuery } from '../types';
import parseHTML from '../utils/parseHTML';
2019-11-12 04:09:29 +01:00
import { setCurrentNode } from '../utils/nodes';
2019-11-08 03:40:20 +01:00
const Template: StatelessComponent<SingleFileQuery> = (result): ReactElement => {
2019-11-12 04:09:29 +01:00
const currentNode = result.data.silverstripeDocument;
2019-11-20 01:56:11 +01:00
let html;
if (currentNode.watchFile) {
html = currentNode.watchFile.html;
} else {
html = currentNode.parent.html;
}
2019-11-12 04:09:29 +01:00
const { title, slug } = currentNode;
2019-11-18 05:51:57 +01:00
const { relativePath, gitRemote: { ref, webLink } } = currentNode.parent.parent;
2019-11-08 03:40:20 +01:00
const editLink = `${webLink}/edit/${ref}/${relativePath}`;
2019-11-12 04:09:29 +01:00
useEffect(() => {
setCurrentNode(slug);
}, []);
2019-11-08 03:40:20 +01:00
return (
<>
<SEO title={title} />
{parseHTML(html)}
<div className="github-edit">
<a target="_blank" href={editLink} title="Edit on Github">
<i className="fas fa-pen fa-fw" />{` `}
Edit on Github
</a>
</div>
</>
);
};
export default Template;
export const pageQuery = graphql`
query DocsBySlug($slug: String!) {
silverstripeDocument(slug: { eq: $slug }) {
2019-11-19 01:55:33 +01:00
title
slug
id
watchFile {
html
}
2019-11-08 03:40:20 +01:00
parent {
... on MarkdownRemark {
2019-11-20 01:56:11 +01:00
html
2019-11-08 03:40:20 +01:00
parent {
... on File {
relativePath
gitRemote {
ref
webLink
2019-11-12 04:09:29 +01:00
sourceInstanceName
2019-11-08 03:40:20 +01:00
}
}
}
}
}
}
}
`
;