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

Move Documentation components to theme #3170

Merged
merged 35 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8b92ebe
Move sidebar logic to theme
rogermparent Dec 28, 2021
b73d69a
Split sidebar normalization and utils and import via shadowing
rogermparent Dec 29, 2021
0f1c0fe
Fix usage of findChildWithSource
rogermparent Dec 29, 2021
332644a
Fix lint-ts errors
rogermparent Dec 29, 2021
c127d19
Move comment normalized item definition to d.ts
rogermparent Dec 29, 2021
a24e1d6
Update and export INormalizedSidebarItem and use it in SidebarMenu
rogermparent Dec 29, 2021
e626bd0
Add suppressable default gatsby-transformer-remark definition
rogermparent Dec 29, 2021
b077fbb
Remove resolve-sidebar since local remark plugins are dvc.org only
rogermparent Dec 29, 2021
978b27b
Remove resolve-sidebar since local remark plugins are dvc.org only
rogermparent Dec 29, 2021
a6cfd96
Merge branch 'sidebar-to-plugin' into add-markdown-to-plugin
rogermparent Dec 29, 2021
b771dc0
Return src/components to master
rogermparent Jan 9, 2022
8df9091
Add alias for local theme
rogermparent Jan 4, 2022
57d4743
Roughly move doc components to theme
rogermparent Jan 10, 2022
c2b0462
Merge branch 'master' of github.com:iterative/dvc.org into add-compon…
rogermparent Jan 10, 2022
ba8bda3
Revert to master package.json
rogermparent Jan 10, 2022
912e5fe
Remove unused normalize sidebar module
rogermparent Jan 10, 2022
c311791
Re-fix package.json and change test imports
rogermparent Jan 10, 2022
56d1ae3
stylelint --fix
rogermparent Jan 10, 2022
e823d91
Move Page and DocumentationLayout from theme to site
rogermparent Jan 11, 2022
a9027a0
Fix svg imports
rogermparent Jan 11, 2022
6c11d10
Merge branch 'master' of github.com:iterative/dvc.org into add-compon…
rogermparent Jan 14, 2022
456c1c5
Remove unused layout components
rogermparent Jan 14, 2022
fad07b2
stylelint fix
rogermparent Jan 17, 2022
9407736
Merge branch 'master' of github.com:iterative/dvc.org into add-compon…
rogermparent Jan 17, 2022
d4b0985
Extract github link function and repo constant to more easily overrid…
rogermparent Jan 17, 2022
df3bc68
Remove unnecessary shadowed files since webpack override is in place
rogermparent Jan 17, 2022
b07ca1b
Remove unused constants file
rogermparent Jan 25, 2022
18ae55f
Revert "Remove unused constants file"
rogermparent Jan 25, 2022
857351b
Remove actual unused constants file
rogermparent Jan 25, 2022
0703ac9
Use theme-based Link component
rogermparent Jan 25, 2022
a8b4b1a
Move core part of Documentation layout into theme
rogermparent Jan 25, 2022
f2e61e6
Rename sidebar test block to something more descriptive of its contents
rogermparent Jan 25, 2022
d975f02
Break apart docs sidebar and move it into theme
rogermparent Jan 25, 2022
88ed746
Merge branch 'master' of github.com:iterative/dvc.org into add-compon…
rogermparent Jan 31, 2022
daad2be
Add missing import
rogermparent Jan 31, 2022
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
7 changes: 6 additions & 1 deletion gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ const plugins = [
'gatsby-plugin-react-helmet',
'gatsby-plugin-sitemap',
'gatsby-plugin-twitter',
'gatsby-theme-iterative-docs',
{
resolve: 'gatsby-theme-iterative-docs',
options: {
remark: false
}
},
{
resolve: 'gatsby-source-filesystem',
options: {
Expand Down
11 changes: 11 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('dotenv').config()
const path = require('path')

const { setPageContext } = require('./src/gatsby/common')

Expand Down Expand Up @@ -39,4 +40,14 @@ exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
}
actions.replaceWebpackConfig(config)
}
actions.setWebpackConfig({
resolve: {
alias: {
'gatsby-theme-iterative-docs': path.resolve(
'plugins',
'gatsby-theme-iterative-docs'
)
}
}
})
}
4 changes: 3 additions & 1 deletion plugins/gatsby-remark-dvc-linker/apiLinker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')
const { getItemByPath } = require('../../src/utils/shared/sidebar')
const {
getItemByPath
} = require('../../plugins/gatsby-theme-iterative-docs/src/utils/shared/sidebar')

const DVC_API_REGEXP = /dvc.api([a-z-._]*\(\)$)?/
const METHOD_REGEXP = /^[a-z-._]*\(\)$/
Expand Down
4 changes: 3 additions & 1 deletion plugins/gatsby-remark-dvc-linker/commandLinker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')
const { getItemByPath } = require('../../src/utils/shared/sidebar')
const {
getItemByPath
} = require('../../plugins/gatsby-theme-iterative-docs/src/utils/shared/sidebar')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these aren't webpacked, the imports will have to stay relative until the theme is a real package, which is the next step.


const DVC_REGEXP = /dvc\s+[a-z][a-z-.]*/
const COMMAND_REGEXP = /^[a-z][a-z-]*$/
Expand Down
4 changes: 3 additions & 1 deletion plugins/gatsby-remark-dvc-linker/liveLinker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')
const { getItemByPath } = require('../../src/utils/shared/sidebar')
const {
getItemByPath
} = require('../../plugins/gatsby-theme-iterative-docs/src/utils/shared/sidebar')

const LIVE_API_REGEXP = /Live.([a-z-._]*\(\)$)?/
const METHOD_REGEXP = /^[a-z-._]*\(\)$/
Expand Down
57 changes: 57 additions & 0 deletions plugins/gatsby-theme-iterative-docs/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module.exports = ({ remark }) => {
const plugins = []
if (remark) {
plugins.push({
resolve: 'gatsby-transformer-remark',
options: {
plugins: [
'gatsby-remark-embedder',
{
resolve: 'gatsby-remark-prismjs',
options: {
noInlineHighlight: true,
languageExtensions: [
{
language: 'text',
definition: {}
}
]
}
},
{
resolve: 'gatsby-remark-smartypants',
options: {
quotes: false
}
},
{
resolve: 'gatsby-remark-embed-gist',
options: {
includeDefaultCss: true
}
},
'gatsby-remark-relative-images',
'gatsby-remark-copy-linked-files',
'gatsby-remark-external-links',
{
resolve: 'gatsby-remark-autolink-headers',
options: {
enableCustomId: true,
isIconAfterHeader: true
}
},
{
resolve: 'gatsby-remark-images',
options: {
withWebp: true
}
},
'gatsby-remark-responsive-iframe'
]
}
})
}
return {
plugins
}
}
Comment on lines +1 to +57
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reimplemented #3143

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any website where we don't use remark plugins?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterative.ai doesn't, and likely won't, but it also wouldn't be using this theme. Also, cml.dev doesn't use the custom local remark plugins that dvc.org does, though it'll probably want to in the future.

24 changes: 22 additions & 2 deletions plugins/gatsby-theme-iterative-docs/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ exports.pluginOptionsSchema = ({ Joi }) => {
disable: Joi.boolean().default(Boolean(process.env.SKIP_DOCS)),
getTemplate: Joi.function().default(() => defaultGetTemplate),
defaultTemplate: Joi.string().default(
path.resolve('src', 'templates', 'doc.tsx')
)
require.resolve('./src/templates/doc.tsx')
),
remark: Joi.boolean().default(true)
})
}

Expand All @@ -33,6 +34,25 @@ exports.createSchemaCustomization = async api => {
])
}

exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
[path.resolve(__dirname, 'sidebar')]: path.resolve(
'src',
'gatsby-theme-iterative-docs',
'sidebar'
),
[path.resolve(__dirname, 'redirects')]: path.resolve(
'src',
'gatsby-theme-iterative-docs',
'redirects'
)
}
}
})
}
Comment on lines +37 to +54
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meat and potatoes to getting sidebar resolution working: this is similar to theme shadowing within Gatsby and uses the same directory structure on the consumer end, but the actual node files in the same location allow gatsby-node and other pure node modules to work with the same imports, and so allowing the modules that import these to be used in both environments, enabling the utils/shared modules to work even in an external module.


exports.createPages = require('./createPages.js')

exports.onCreateNode = require('./onCreateNode.js')
1 change: 1 addition & 0 deletions plugins/gatsby-theme-iterative-docs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
3 changes: 3 additions & 0 deletions plugins/gatsby-theme-iterative-docs/pseudo-shadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const path = require('path')
const pseudoShadow = modulePath => require(path.resolve(modulePath))
module.exports = pseudoShadow
2 changes: 2 additions & 0 deletions plugins/gatsby-theme-iterative-docs/redirects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const pseudoShadow = require('./pseudo-shadow')
module.exports = pseudoShadow('./src/gatsby-theme-iterative-docs/redirects')
2 changes: 2 additions & 0 deletions plugins/gatsby-theme-iterative-docs/sidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const pseudoShadow = require('./pseudo-shadow')
module.exports = pseudoShadow('./src/gatsby-theme-iterative-docs/sidebar')
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import React from 'react'
import MarkdownMain from '../Markdown/Main'
import RightPanel from '../RightPanel'

import { IHeading, getGithubLink } from '../'
import { IHeading } from '../'
import { getItemByPath } from '../../../utils/shared/sidebar'
import { getGithubLink } from '../../../getGithubLink'

interface IWithJSXProps {
path: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import Markdown from './Markdown'
import RightPanel from './RightPanel'

import { getItemByPath } from '../../utils/shared/sidebar'
import { getGithubLink } from '../../getGithubLink'

export interface IHeading {
slug: string
text: string
}

export const getGithubLink = (source: string): string =>
`https://github.com/iterative/dvc.org/blob/master/content${source}`

interface IDocumentationProps {
path: string
headings: Array<IHeading>
Expand Down
136 changes: 136 additions & 0 deletions plugins/gatsby-theme-iterative-docs/src/components/Link/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React, { useCallback } from 'react'
import { URL } from 'iso-url'
import { useLocation } from '@reach/router'
import GatsbyLink from 'gatsby-link'
import { getRedirect } from '../../utils/shared/redirects'
import { scrollIntoLayout, getScrollNode } from '../../utils/front/scroll'
import safeQuerySelector from '../../utils/front/safeQuerySelector'

export type ILinkProps = {
children: React.ReactNode
className?: string
href: string
target?: undefined | '_blank'
state?: unknown
scrollOptions?: Record<string, unknown>
optOutPreRedirect?: undefined | true
} & React.AnchorHTMLAttributes<HTMLAnchorElement>

const PROTOCOL_REGEXP = /^https?:\/\//
const isRelative = (url: string): boolean => !PROTOCOL_REGEXP.test(url)
const isMailto = (url: string): boolean => url.startsWith('mailto:')

const ResultLinkComponent: React.FC<ILinkProps> = ({
href,
children,
rel,
target,
download = false,
...restProps
}) => {
// Handle all situations where a basic `a` must be used over Gatsby Link
const hrefIsRelative = isRelative(href)
const hrefIsMailto = isMailto(href)
const hrefHasTarget = typeof target === 'string'
// Fragments within the page should be `a`, but links to other pages
// that have anchors should be okay.
const hrefIsRelativeFragment = href.startsWith('#')

if (
download ||
!hrefIsRelative ||
hrefIsMailto ||
hrefHasTarget ||
hrefIsRelativeFragment
) {
/*
Change external links without an explicit rel to have 'noopener
noreferrer', but leave explicitly defined rels alone.
Do the same with `target=_blank`
*/
if (!hrefIsRelative) {
if (typeof rel !== 'string') {
rel = 'noopener noreferrer'
}
if (!hrefHasTarget) {
target = '_blank'
}
}

return (
<a
download={download}
href={href}
rel={rel}
target={target}
{...restProps}
>
{children}
</a>
)
}

return (
<GatsbyLink to={href} {...restProps}>
{children}
</GatsbyLink>
)
}

const scrollToHash = (hash: string, scrollOptions = {}): void => {
if (hash) {
scrollIntoLayout(safeQuerySelector(hash), {
waitImages: true,
...scrollOptions
})
}
}

const Link: React.FC<ILinkProps> = ({
href,
scrollOptions,
optOutPreRedirect,
...restProps
}) => {
const currentLocation = useLocation()

const onClick = useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
if (restProps.onClick) {
restProps.onClick(e)
}

// Handle local fragments manually, allowing for more control than
// native HTML fragment navigation.
if (href === '#') {
getScrollNode().scrollTop = 0
} else if (href.startsWith('#')) {
e.preventDefault()

// We can't navigate by direct usage of @reach/router#navigate because
// gatsby-react-router-scroll will package intercept scroll in this
// case and we will see undesired jump
window.history.pushState(null, '', href)
scrollToHash(href, scrollOptions)
}
},
[restProps.onClick, currentLocation]
)

const location = new URL(href)

if (location.host === currentLocation.host && !optOutPreRedirect) {
// Replace link href with redirect if it exists
const [, redirectUrl] = getRedirect(location.host, location.pathname)

if (redirectUrl) {
href = isRelative(redirectUrl)
? redirectUrl + currentLocation.search
: redirectUrl
}
}

return <ResultLinkComponent href={href} {...restProps} onClick={onClick} />
}

export default Link
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import Helmet from 'react-helmet'
import { IGatsbyImageData } from 'gatsby-plugin-image'

import getSiteMeta from '../../queries/siteMeta'
import { IPaginatorPageInfo } from '../Paginator'
import { buildMetadata, MetaProps } from './helper'

export interface IPaginatorPageInfo {
currentPage: number
nextPage?: string
previousPage?: string
}

Comment on lines +8 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this interface but not the rest of Paginator

interface ISEOProps {
title?: string
defaultMetaTitle?: boolean
Expand Down
19 changes: 19 additions & 0 deletions plugins/gatsby-theme-iterative-docs/src/config/custom-media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const screens = {
giant: 1200,
desktop: 1005,
tablet: 768,
phablet: 572,
phone: 376
}

module.exports = {
screens,
customMedia: {
'--xxs-scr': `(max-width: ${screens.phone}px)`,
'--xs-scr': `(max-width: ${screens.phablet}px)`,
'--sm-scr': `(max-width: ${screens.tablet}px)`,
'--md-scr': `(max-width: ${screens.desktop - 1}px)`,
'--lg-scr': `(min-width: ${screens.desktop}px)`,
'--xl-scr': `(min-width: ${screens.giant}px)`
}
}
1 change: 1 addition & 0 deletions plugins/gatsby-theme-iterative-docs/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const githubRepo = 'iterative/dvc'
3 changes: 3 additions & 0 deletions plugins/gatsby-theme-iterative-docs/src/getGithubLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { githubRepo } from './constants'
export const getGithubLink = (sourcePath: string): string =>
`https://github.com/${githubRepo}/blob/master/content${sourcePath}`
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import includes from 'lodash/includes'
import { customMedia } from '../../../config/postcss/media'
import { customMedia } from '../../config/custom-media'

type AvailableCustomMedia = keyof typeof customMedia

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading