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 all 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')
global.__basedir = __dirname

const { setPageContext } = require('./src/gatsby/common')
Expand Down Expand Up @@ -40,4 +41,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
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'
import Promise from 'promise-polyfill'
import { loadResource } from '../../../../utils/front/resources'
import { loadResource } from 'gatsby-theme-iterative-docs/src/utils/front/resources'

import * as styles from './styles.module.css'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react'
import includes from 'lodash/includes'
import * as styles from '../styles.module.css'
import { structure } from '../../../../../utils/shared/sidebar'
import SidebarMenuItem from '../Item'
import { SidebarItemClickHandler } from '..'

export interface IInnerSidebarProps {
activePaths: string[]
onClick: SidebarItemClickHandler
}

const SidebarSections: React.FC<IInnerSidebarProps> = ({
activePaths,
onClick
}) => {
return (
<div className={styles.sections}>
<div className={styles.sectionLinks}>
{structure.map(item => (
<SidebarMenuItem
key={item.path}
activePaths={
includes(activePaths, item.path) ? activePaths : undefined
}
onClick={onClick}
{...item}
/>
))}
</div>
</div>
)
}

export default SidebarSections
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import React, { useEffect, useRef, useState, SyntheticEvent } from 'react'
import { useLocation } from '@reach/router'
import cn from 'classnames'
import React, { SyntheticEvent, useEffect, useState } from 'react'
import { Collapse } from 'react-collapse'
import PerfectScrollbar from 'perfect-scrollbar'
import cn from 'classnames'
import includes from 'lodash/includes'

import ShowOnly from '../../../ShowOnly'
import DownloadButton from '../../../DownloadButton'
import Link from '../../../Link'
import Link from '../../../../Link'
import { ReactComponent as ExternalLinkIcon } from './external-link-icon.svg'
import { ReactComponent as HouseIcon } from './house.svg'
import { ReactComponent as CMLIcon } from './cml_bw_logo.svg'
import { ReactComponent as StudioIcon } from './studio_gray_icon.svg'

import {
structure,
getParentsListFromPath,
getPathWithSource
} from '../../../../utils/shared/sidebar'

import 'perfect-scrollbar/css/perfect-scrollbar.css'
import * as styles from './styles.module.css'

// A map for optional special icons that can be used in menu items
// Use the key string here as the "icon" field in sidebar.json
const ICONS: { [key: string]: React.FC<{ className?: string }> } = {
house: HouseIcon,
cml: CMLIcon,
studio: StudioIcon
}
import * as styles from '../styles.module.css'

import ICONS from '../icons'
import { getPathWithSource } from '../../../../../utils/shared/sidebar'

interface ISidebarMenuItemProps {
children?: Array<{ label: string; path: string; source: boolean | string }>
Expand Down Expand Up @@ -160,87 +140,4 @@ const SidebarMenuItem: React.FC<ISidebarMenuItemProps> = ({
)
}

interface ISidebarMenuProps {
currentPath: string
onClick: (isLeafItemClicked: boolean) => void
}

const SidebarMenu: React.FC<ISidebarMenuProps> = ({ currentPath, onClick }) => {
const location = useLocation()
const rootRef = useRef<HTMLDivElement>(null)
const psRef = useRef<PerfectScrollbar | undefined>(undefined)
const [isScrollHidden, setIsScrollHidden] = useState(false)
const activePaths = currentPath && getParentsListFromPath(currentPath)

const scrollToActiveItem = (): void => {
const node = document.getElementById(currentPath)
const parent = rootRef.current

setIsScrollHidden(true)
setTimeout(() => {
if (node && parent) {
psRef.current?.update()

const parentHeight = parent.clientHeight
const parentScroll = parent.scrollTop
const nodeOffset = node.offsetTop
const nodeHeight = node.clientHeight
const scrollOffset = nodeOffset - parentHeight + nodeHeight

if (
parentScroll > nodeOffset + nodeHeight ||
parentScroll + parentHeight < nodeOffset
) {
parent.scrollTop = scrollOffset
}
}

setIsScrollHidden(false)
}, 400)
}

useEffect(() => {
if (!psRef.current && rootRef.current) {
psRef.current = new PerfectScrollbar(rootRef.current, {
wheelPropagation: true
})
}

scrollToActiveItem()

return (): void => {
psRef.current?.destroy()
psRef.current = undefined
}
}, [])
useEffect(scrollToActiveItem, [location.pathname])

return (
<div
className={cn(styles.menu, isScrollHidden && styles.isScrollHidden)}
ref={rootRef}
>
<div className={styles.sections}>
<div className={styles.sectionLinks}>
{structure.map(item => (
<SidebarMenuItem
key={item.path}
activePaths={
includes(activePaths, item.path) ? activePaths : undefined
}
onClick={onClick}
{...item}
/>
))}
</div>
</div>
<ShowOnly on="desktop">
<div className={styles.footer}>
<DownloadButton openTop />
</div>
</ShowOnly>
</div>
)
}

export default SidebarMenu
export default SidebarMenuItem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ReactComponent as HouseIcon } from './house.svg'

export type SidebarIcons = { [key: string]: React.FC<{ className?: string }> }

const ICONS: SidebarIcons = {
house: HouseIcon
}

export default ICONS
Loading