feat: Implement mdx

This commit is contained in:
dfireBird
2020-08-08 16:47:43 +05:30
parent 7e3565e78a
commit abf609d09f
14 changed files with 1735 additions and 1448 deletions
-6
View File
@@ -31,12 +31,6 @@ module.exports = {
}, },
}, },
`gatsby-plugin-layout`, `gatsby-plugin-layout`,
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [`gatsby-remark-auto-headers-improved`],
},
},
{ {
resolve: `gatsby-plugin-mdx`, resolve: `gatsby-plugin-mdx`,
options: { options: {
+1 -1
View File
@@ -161,7 +161,7 @@ exports.onCreateNode = async ({ node, getNode, actions }) => {
const { createPage, createNodeField } = actions const { createPage, createNodeField } = actions
// TODO: find a better way to avoid handling non-resources // TODO: find a better way to avoid handling non-resources
// or better, attach authors to what-is archives // or better, attach authors to what-is archives
if (node.internal.type === "MarkdownRemark" && node.frontmatter.authors) { if (node.internal.type === "Mdx" && node.frontmatter.authors) {
const { frontmatter } = node const { frontmatter } = node
const isDoc = Boolean(!frontmatter.path) const isDoc = Boolean(!frontmatter.path)
+1700 -1409
View File
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -14,6 +14,7 @@ import "prismjs/components/prism-python"
import "prismjs/components/prism-yaml" import "prismjs/components/prism-yaml"
import "prismjs/plugins/line-numbers/prism-line-numbers.css" import "prismjs/plugins/line-numbers/prism-line-numbers.css"
import React, { FC, useEffect } from "react" import React, { FC, useEffect } from "react"
import { MDXRenderer } from "gatsby-plugin-mdx"
import * as SC from "./styles" import * as SC from "./styles"
interface IMarkdownProps { interface IMarkdownProps {
@@ -30,8 +31,9 @@ export const Markdown: FC<IMarkdownProps> = ({ content, ...restProps }) => {
}, []) }, [])
return ( return (
<SC.MarkdownWrapper <SC.MarkdownWrapper
dangerouslySetInnerHTML={{ __html: content }}
{...restProps} {...restProps}
/> >
<MDXRenderer>{content}</MDXRenderer>
</SC.MarkdownWrapper>
) )
} }
@@ -6,7 +6,7 @@ import * as SC from "./styles"
interface IResourcesHomeContent extends HTMLAttributes<HTMLDivElement> { interface IResourcesHomeContent extends HTMLAttributes<HTMLDivElement> {
language: string language: string
html: any body: any
} }
export const ResourcesHomeContent: FC<IResourcesHomeContent> = (props) => { export const ResourcesHomeContent: FC<IResourcesHomeContent> = (props) => {
@@ -20,7 +20,7 @@ export const ResourcesHomeContent: FC<IResourcesHomeContent> = (props) => {
<ResourcesList relativeDirectory={props.language} /> <ResourcesList relativeDirectory={props.language} />
<SC.Box> <SC.Box>
<SC.Title>Extra resources</SC.Title> <SC.Title>Extra resources</SC.Title>
<Markdown content={props.html} /> <Markdown content={props.body} />
</SC.Box> </SC.Box>
<Footer /> <Footer />
</> </>
+1 -1
View File
@@ -15,7 +15,7 @@ const ALL_RESOURCES = graphql`
node { node {
relativePath relativePath
relativeDirectory relativeDirectory
childMarkdownRemark { childMdx {
frontmatter { frontmatter {
authors authors
title title
+2 -2
View File
@@ -18,7 +18,7 @@ const ALL_RESOURCES = graphql`
edges { edges {
node { node {
relativePath relativePath
childMarkdownRemark { childMdx {
frontmatter { frontmatter {
authors authors
title title
@@ -31,7 +31,7 @@ const ALL_RESOURCES = graphql`
edges { edges {
node { node {
relativePath relativePath
childMarkdownRemark { childMdx {
frontmatter { frontmatter {
authors authors
title title
+5 -5
View File
@@ -1,7 +1,7 @@
import { graphql } from "gatsby" import { graphql } from "gatsby"
import React, { Fragment } from "react" import React, { Fragment } from "react"
import cx from "classnames" import cx from "classnames"
import { MarkdownRemark } from "../../generated/graphql" import { Mdx } from "../../generated/graphql"
import { ComponentQuery } from "../../typings" import { ComponentQuery } from "../../typings"
import { Markdown } from "../components/Markdown" import { Markdown } from "../components/Markdown"
import { SEO } from "../components/SEO" import { SEO } from "../components/SEO"
@@ -9,7 +9,7 @@ import { PageContent } from "../components/PageContent"
import { HeaderBarebone } from "../components/HeaderBarebone" import { HeaderBarebone } from "../components/HeaderBarebone"
import { buildToc } from "../utils" import { buildToc } from "../utils"
function AboutPage({ data }: ComponentQuery<{ md: MarkdownRemark }>) { function AboutPage({ data }: ComponentQuery<{ md: Mdx }>) {
const { md } = data const { md } = data
const toc = buildToc(md.headings!) const toc = buildToc(md.headings!)
@@ -22,15 +22,15 @@ function AboutPage({ data }: ComponentQuery<{ md: MarkdownRemark }>) {
className={cx({ shifted: toc.length })} className={cx({ shifted: toc.length })}
/> />
<PageContent content={<Markdown content={md.html!} />} toc={toc} /> <PageContent content={<Markdown content={md.body!} />} toc={toc} />
</Fragment> </Fragment>
) )
} }
export const query = graphql` export const query = graphql`
query AboutPage { query AboutPage {
md: markdownRemark(frontmatter: { path: { eq: "/about" } }) { md: mdx(frontmatter: { path: { eq: "/about" } }) {
html body
headings { headings {
depth depth
value value
+5 -5
View File
@@ -1,7 +1,7 @@
import { graphql } from "gatsby" import { graphql } from "gatsby"
import React, { Fragment } from "react" import React, { Fragment } from "react"
import cx from "classnames" import cx from "classnames"
import { MarkdownRemark } from "../../generated/graphql" import { Mdx } from "../../generated/graphql"
import { ComponentQuery } from "../../typings" import { ComponentQuery } from "../../typings"
import { HeaderBarebone } from "../components/HeaderBarebone" import { HeaderBarebone } from "../components/HeaderBarebone"
import { Markdown } from "../components/Markdown" import { Markdown } from "../components/Markdown"
@@ -9,7 +9,7 @@ import { PageContent } from "../components/PageContent"
import { SEO } from "../components/SEO" import { SEO } from "../components/SEO"
import { buildToc } from "../utils" import { buildToc } from "../utils"
function RulesPage({ data }: ComponentQuery<{ md: MarkdownRemark }>) { function RulesPage({ data }: ComponentQuery<{ md: Mdx }>) {
const { md } = data const { md } = data
const toc = buildToc(md.headings!) const toc = buildToc(md.headings!)
@@ -19,15 +19,15 @@ function RulesPage({ data }: ComponentQuery<{ md: MarkdownRemark }>) {
<SEO title="Rules" /> <SEO title="Rules" />
<HeaderBarebone title="Rules" className={cx({ shifted: toc.length })} /> <HeaderBarebone title="Rules" className={cx({ shifted: toc.length })} />
<PageContent content={<Markdown content={md.html!} />} toc={toc} /> <PageContent content={<Markdown content={md.body!} />} toc={toc} />
</Fragment> </Fragment>
) )
} }
export const query = graphql` export const query = graphql`
query RulesPage { query RulesPage {
md: markdownRemark(frontmatter: { path: { eq: "/rules" } }) { md: mdx(frontmatter: { path: { eq: "/rules" } }) {
html body
headings { headings {
depth depth
value value
+4 -4
View File
@@ -9,7 +9,7 @@ import { PageContent } from "../components/PageContent"
// @todo maybe find alternative type for data // @todo maybe find alternative type for data
const Archive: FC<any> = ({ data }) => { const Archive: FC<any> = ({ data }) => {
const { relativePath, ctime } = data.file const { relativePath, ctime } = data.file
const { html, excerpt, timeToRead } = data.file.post const { body, excerpt, timeToRead } = data.file.post
const title = humanize(relativePath) const title = humanize(relativePath)
@@ -24,7 +24,7 @@ const Archive: FC<any> = ({ data }) => {
timeToRead={timeToRead} timeToRead={timeToRead}
shifted={false} shifted={false}
/> />
<PageContent content={<Markdown content={html} />} /> <PageContent content={<Markdown content={body} />} />
</Fragment> </Fragment>
) )
} }
@@ -36,8 +36,8 @@ export const query = graphql`
file(relativePath: { eq: $file }) { file(relativePath: { eq: $file }) {
relativePath relativePath
ctime ctime
post: childMarkdownRemark { post: childMdx {
html body
excerpt excerpt
timeToRead timeToRead
} }
+4 -4
View File
@@ -10,7 +10,7 @@ import useSidebar from "../hooks/useSidebar"
const ResourceHome: FC<any> = ({ data, pageContext }) => { const ResourceHome: FC<any> = ({ data, pageContext }) => {
const { current: language } = useSidebar() const { current: language } = useSidebar()
const { relativePath } = data.file const { relativePath } = data.file
const { html, excerpt, fields, frontmatter, timeToRead } = data.file.post const { body, excerpt, fields, frontmatter, timeToRead } = data.file.post
const shiftLayout = Boolean( const shiftLayout = Boolean(
frontmatter.recommended_reading || frontmatter.external_resources frontmatter.recommended_reading || frontmatter.external_resources
@@ -34,7 +34,7 @@ const ResourceHome: FC<any> = ({ data, pageContext }) => {
/> />
<PageContent <PageContent
content={ content={
<ResourcesHomeContent language={pageContext.language} html={html} /> <ResourcesHomeContent language={pageContext.language} body={body} />
} }
recommendedReading={frontmatter.recommended_reading} recommendedReading={frontmatter.recommended_reading}
externalResources={frontmatter.external_resources} externalResources={frontmatter.external_resources}
@@ -53,8 +53,8 @@ export const query = graphql`
base: { eq: "intro.md" } base: { eq: "intro.md" }
) { ) {
relativePath relativePath
post: childMarkdownRemark { post: childMdx {
html body
excerpt excerpt
fields { fields {
authors { authors {
+4 -4
View File
@@ -14,7 +14,7 @@ const ResourcePage: FC<any> = ({ data }) => {
const { current: language } = useSidebar() const { current: language } = useSidebar()
const { relativePath } = data.file const { relativePath } = data.file
const { const {
html, body,
headings, headings,
excerpt, excerpt,
fields, fields,
@@ -49,7 +49,7 @@ const ResourcePage: FC<any> = ({ data }) => {
<PageContent <PageContent
content={ content={
<> <>
<Markdown content={html} /> <Markdown content={body} />
<Footer /> <Footer />
</> </>
} }
@@ -67,8 +67,8 @@ export const query = graphql`
query ResourcePage($file: String!) { query ResourcePage($file: String!) {
file(relativePath: { eq: $file }) { file(relativePath: { eq: $file }) {
relativePath relativePath
post: childMarkdownRemark { post: childMdx {
html body
headings { headings {
depth depth
value value
+1 -1
View File
@@ -23,7 +23,7 @@ export interface IFileResourceQuery {
node: { node: {
relativePath: string relativePath: string
relativeDirectory: string relativeDirectory: string
childMarkdownRemark: { childMdx: {
frontmatter: { frontmatter: {
author: string author: string
date: string date: string
+2 -2
View File
@@ -5,7 +5,7 @@ import pipe from "ramda/es/pipe"
import sort from "ramda/es/sort" import sort from "ramda/es/sort"
import { IFile, IFileOrFolder, IFolder, ITocItem } from "../types" import { IFile, IFileOrFolder, IFolder, ITocItem } from "../types"
import { MarkdownRemark } from "../../generated/graphql" import { Mdx } from "../../generated/graphql"
const slugger = new GithubSlugger() const slugger = new GithubSlugger()
@@ -141,7 +141,7 @@ export function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] {
return [current, ...join(remaining)] return [current, ...join(remaining)]
} }
export function buildToc(headings: MarkdownRemark["headings"]): ITocItem[] { export function buildToc(headings: Mdx["headings"]): ITocItem[] {
if (!headings) { if (!headings) {
return [] return []
} }