Skip to content

Commit

Permalink
New docs site! (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer authored Jul 17, 2020
1 parent 8b148ce commit b09e195
Show file tree
Hide file tree
Showing 47 changed files with 10,116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions website/_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/ https://tsdx.io 301!
/* https://tsdx.io 301!
4 changes: 4 additions & 0 deletions website2/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": ["babel-plugin-macros", "./.nextra/babel-plugin-nextjs-mdx-patch"]
}
4 changes: 4 additions & 0 deletions website2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.next
.DS_Store
yarn-error.log
19 changes: 19 additions & 0 deletions website2/.nextra/arrow-right.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default ({ width = 24, height = 24, ...props }) => {
return (
<svg
width={width}
height={height}
viewBox="0 0 24 24"
fill="none"
{...props}
>
<path
d="M3 12h18m0 0l-6.146-6M21 12l-6.146 6"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)
}
34 changes: 34 additions & 0 deletions website2/.nextra/babel-plugin-nextjs-mdx-patch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Currently it's not possible to export data fetching functions from MDX pages
* because MDX includes them in `layoutProps`, and Next.js removes them at some
* point, causing a `ReferenceError`.
*
* https://github.com/mdx-js/mdx/issues/742#issuecomment-612652071
*
* This plugin can be removed once MDX removes `layoutProps`, at least that
* seems to be the current plan.
*/

// https://nextjs.org/docs/basic-features/data-fetching
const DATA_FETCH_FNS = ['getStaticPaths', 'getStaticProps', 'getServerProps']

module.exports = () => {
return {
visitor: {
ObjectProperty(path) {
if (
DATA_FETCH_FNS.includes(path.node.value.name) &&
path.findParent(
(path) =>
path.isVariableDeclarator() &&
path.node.id.name === 'layoutProps',
)
) {
path.remove()
}
},
},
}
}

// https://github.com/vercel/next.js/issues/12053#issuecomment-622939046
11 changes: 11 additions & 0 deletions website2/.nextra/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import userConfig from '../nextra.config';

const defaultConfig = {
nextLinks: true,
prevLinks: true,
search: true,
};

export default () => {
return { ...defaultConfig, ...userConfig };
};
96 changes: 96 additions & 0 deletions website2/.nextra/directories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import preval from 'preval.macro'
import title from 'title'

const excludes = ['/_app.js', '/_document.js', '/_error.js']

// watch all meta files
const meta = {}
function importAll(r) {
return r.keys().forEach(key => {
meta[key.slice(1)] = r(key)
})
}
importAll(require.context('../pages/', true, /meta\.json$/))

// use macro to load the file list
const items = preval`
const { readdirSync, readFileSync } = require('fs')
const { resolve, join } = require('path')
const extension = /\.(mdx?|jsx?)$/
function getFiles(dir, route) {
const files = readdirSync(dir, { withFileTypes: true })
// go through the directory
const items = files
.map(f => {
const filePath = resolve(dir, f.name)
const fileRoute = join(route, f.name.replace(extension, '').replace(/^index$/, ''))
if (f.isDirectory()) {
const children = getFiles(filePath, fileRoute)
if (!children.length) return null
return { name: f.name, children, route: fileRoute }
} else if (f.name === 'meta.json') {
return null
} else if (extension.test(f.name)) {
return { name: f.name.replace(extension, ''), route: fileRoute }
}
})
.map(item => {
if (!item) return
return { ...item, metaPath: join(route, 'meta.json') }
})
.filter(Boolean)
return items
}
module.exports = getFiles(join(process.cwd(), 'pages'), '/')
`

const attachPageConfig = function (items) {
let folderMeta = null
let fnames = null

return items
.filter(item => !excludes.includes(item.name))
.map(item => {
const { metaPath, ...rest } = item
folderMeta = meta[metaPath]
if (folderMeta) {
fnames = Object.keys(folderMeta)
}

const pageConfig = folderMeta?.[item.name]

if (rest.children) rest.children = attachPageConfig(rest.children)

if (pageConfig) {
if (typeof pageConfig === 'string') {
return { ...rest, title: pageConfig }
}
return { ...rest, ...pageConfig }
} else {
if (folderMeta) {
return null
}
return { ...rest, title: title(item.name) }
}
})
.filter(Boolean)
.sort((a, b) => {
if (folderMeta) {
return fnames.indexOf(a.name) - fnames.indexOf(b.name)
}
// by default, we put directories first
if (!!a.children !== !!b.children) {
return !!a.children ? -1 : 1
}
// sort by file name
return a.name < b.name ? -1 : 1
})
}

export default () => {
return attachPageConfig(items)
}
44 changes: 44 additions & 0 deletions website2/.nextra/docsearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useRef, useEffect } from 'react'

export default function () {
const input = useRef(null)

useEffect(() => {
const inputs = ['input', 'select', 'button', 'textarea']

const down = (e) => {
if (
document.activeElement &&
inputs.indexOf(document.activeElement.tagName.toLowerCase() !== -1)
) {
if (e.key === '/') {
e.preventDefault()
input.current?.focus()
}
}
}

window.addEventListener('keydown', down)
return () => window.removeEventListener('keydown', down)
}, [])

useEffect(() => {
if (window.docsearch) {
window.docsearch({
apiKey: '247dd86c8ddbbbe6d7a2d4adf4f3a68a',
indexName: 'vercel_swr',
inputSelector: 'input#algolia-doc-search'
})
}
}, [])

return <div className="relative w-full md:w-64 mr-2">
<input
id="algolia-doc-search"
className="appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline w-full"
type="search"
placeholder='Search ("/" to focus)'
ref={input}
/>
</div>
}
5 changes: 5 additions & 0 deletions website2/.nextra/github-icon.js

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

Loading

0 comments on commit b09e195

Please sign in to comment.