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

fix: add eslint-plugin-github and fix lint errors #714

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .eslintrc.local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
extends: [
'plugin:github/react',
],
overrides: [{
files: ['src/**'],
parserOptions: {
Expand Down
929 changes: 554 additions & 375 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@npmcli/eslint-config": "^4.0.1",
"@npmcli/template-oss": "4.19.0",
"eslint-plugin-github": "^4.10.1",
"tap": "^16.3.2"
},
"author": "GitHub Inc.",
Expand Down
13 changes: 12 additions & 1 deletion theme/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ exports.createPages = async ({graphql, actions}, themeOptions) => {
const contributors =
themeOptions.showContributors !== false ? await fetchContributors(repo, fileRelativePath, node.frontmatte) : []

// Fix some old CLI pages which have mismatched headings at the top level.
// All top level headings should be the same level.
const tableOfContents = node.tableOfContents?.items?.reduce((acc, item) => {
if (!item.url && Array.isArray(item.items)) {
acc.push(...item.items)
} else {
acc.push(item)
}
return acc
}, [])

actions.createPage({
path: pagePath,
component: node.fileAbsolutePath,
Expand All @@ -81,7 +92,7 @@ exports.createPages = async ({graphql, actions}, themeOptions) => {
themeOptions,
editUrl,
contributors,
tableOfContents: node.tableOfContents,
tableOfContents,
},
})

Expand Down
2 changes: 1 addition & 1 deletion theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@testing-library/jest-dom": "^5.16.5",
"babel-jest": "^29.3.1",
"eslint": "^8.33.0",
"eslint-plugin-github": "^4.6.0",
"eslint-plugin-github": "^4.10.1",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-primer-react": "1.0.1",
"eslint-plugin-react": "^7.31.11",
Expand Down
65 changes: 33 additions & 32 deletions theme/src/components/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,41 @@ import React, {useState, useEffect} from 'react'
import ClipboardCopy from './clipboard-copy'
import LiveCode from './live-code'

/**
* Resize the scroll handle to the size of the code contents, since the former has to be positioned absolutely.
*/
const useScrollSize = () => {
const ref = React.createRef()
const [size, setSize] = useState({})

useEffect(() => {
// Skip if already resized.
if (typeof size.width !== 'undefined') {
return
}

const node = ref.current
node.parentElement.style.position = 'relative'
const computedStyle = getComputedStyle(node.parentElement)
setSize({
height:
node.parentElement.clientHeight -
parseInt(computedStyle.paddingTop, 10) -
parseInt(computedStyle.paddingBottom, 10),
width:
node.parentElement.scrollWidth -
parseInt(computedStyle.paddingLeft, 10) -
parseInt(computedStyle.paddingRight, 10),
})
}, [size, ref])

return [ref, size]
}

function Code({className, children, live, noinline}) {
const [scrollHandleStyle, setScrollHandleStyle] = useState({position: 'absolute'})
const language = className ? className.replace(/language-/, '') : ''
const code = children.trim()
const scrollHandleRef = React.createRef()

useEffect(() => {
resizeScrollHandle()
})
const [scrollHandleRef, scrollSize] = useScrollSize()

if (live) {
return <LiveCode code={code} language={language} noinline={noinline} />
Expand All @@ -28,9 +54,7 @@ function Code({className, children, live, noinline}) {
{({className, style, tokens, getLineProps, getTokenProps}) => (
<BorderBox as="pre" className={className} mt={0} mb={3} p={3} border={0} style={{...style, overflow: 'auto'}}>
{/* This is the scroll handle, it is supposed to be focused with keyboard and scroll a wide codebox horizontally */}
<div aria-hidden="true" style={scrollHandleStyle} ref={scrollHandleRef} aria-label={code}>
&nbsp;
</div>
<div aria-hidden="true" style={{position: 'absolute', ...scrollSize}} ref={scrollHandleRef} />
{tokens.map((line, i) => (
<div key={i} {...getLineProps({line, key: i})}>
{line.map((token, key) => (
Expand All @@ -43,29 +67,6 @@ function Code({className, children, live, noinline}) {
</Highlight>
</Relative>
)

/**
* Resize the scroll handle to the size of the code contents, since the former has to be positioned absolutely.
*/
function resizeScrollHandle() {
// Skip if already resized.
if (typeof scrollHandleStyle.width !== 'undefined') {
return
}

const node = scrollHandleRef.current
node.parentElement.style.position = 'relative'
const computedStyle = getComputedStyle(node.parentElement)
const height =
node.parentElement.clientHeight -
parseInt(computedStyle.paddingTop, 10) -
parseInt(computedStyle.paddingBottom, 10)
const width =
node.parentElement.scrollWidth -
parseInt(computedStyle.paddingLeft, 10) -
parseInt(computedStyle.paddingRight, 10)
setScrollHandleStyle({...scrollHandleStyle, height, width})
}
}

export default Code
26 changes: 5 additions & 21 deletions theme/src/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import headerNavItems from '../header-nav.yml'
import useSiteMetadata from '../use-site-metadata'
import MobileSearch from './mobile-search'
import NavDrawer from './nav-drawer'
import NavDropdown, {NavDropdownItem} from './nav-dropdown'
import Search from './search'
import NpmLogo from './npm-logo'
import useSearch from '../use-search'
Expand Down Expand Up @@ -63,26 +62,11 @@ function Header({location, isSearchEnabled = true}) {
function HeaderNavItems({items}) {
return (
<Flex alignItems="center" color="gray.2">
{items.map((item, index) => {
if (item.children) {
return (
<Box ml={4} key={index}>
<NavDropdown title={item.title}>
{item.children.map(child => (
<NavDropdownItem key={child.title} href={child.url}>
{child.title}
</NavDropdownItem>
))}
</NavDropdown>
</Box>
)
}
return (
<Link key={index} href={item.url} display="block" color="inherit" ml={4}>
{item.title}
</Link>
)
})}
lukekarrys marked this conversation as resolved.
Show resolved Hide resolved
{items.map((item, index) => (
<Link key={index} href={item.url} display="block" color="inherit" ml={4}>
{item.title}
</Link>
))}
</Flex>
)
}
Expand Down
8 changes: 4 additions & 4 deletions theme/src/components/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function Layout({children, pageContext, location}) {
</Box>
) : null}
</Box>
{pageContext.tableOfContents.items ? (
{pageContext.tableOfContents ? (
<Position
display={['none', null, 'block']}
css={{gridArea: 'table-of-contents', overflow: 'auto'}}
Expand All @@ -91,7 +91,7 @@ function Layout({children, pageContext, location}) {
<Text display="inline-block" fontWeight="bold" mb={1} id="table-of-content-label">
Table of contents
</Text>
<TableOfContents items={pageContext.tableOfContents.items} labelId="table-of-content-label" />
<TableOfContents items={pageContext.tableOfContents} labelId="table-of-content-label" />
</Position>
) : null}
<Box css={{gridArea: 'content'}}>
Expand All @@ -102,7 +102,7 @@ function Layout({children, pageContext, location}) {
{source ? <SourceLink href={source} /> : null}
</Flex>
) : null}
{pageContext.tableOfContents.items ? (
{pageContext.tableOfContents ? (
<Box display={['block', null, 'none']} mb={3}>
<Details>
{({open}) => (
Expand All @@ -116,7 +116,7 @@ function Layout({children, pageContext, location}) {
Table of contents
</Text>
<Box pt={1}>
<TableOfContents items={pageContext.tableOfContents.items} />
<TableOfContents items={pageContext.tableOfContents} />
</Box>
</>
)}
Expand Down
57 changes: 16 additions & 41 deletions theme/src/components/nav-drawer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {BorderBox, Flex, Link, Text} from '@primer/components'
import {ChevronDownIcon, ChevronUpIcon, XIcon, ThreeBarsIcon} from '@primer/octicons-react'
import {BorderBox, Flex, Link} from '@primer/components'
import {XIcon, ThreeBarsIcon} from '@primer/octicons-react'
import {Link as GatsbyLink} from 'gatsby'
import React from 'react'
import navItems from '../nav.yml'
import headerNavItems from '../header-nav.yml'
import useSiteMetadata from '../use-site-metadata'
import DarkButton from './dark-button'
import Details from './details'
import Drawer from './drawer'
import NavItems from './nav-items'
import {useIsMobile} from '../use-breakpoint'
Expand Down Expand Up @@ -71,44 +70,20 @@ function NavDrawer({location}) {
}

function HeaderNavItems({items}) {
return items.map((item, index) => {
return (
<BorderBox
key={item.title}
borderWidth={0}
borderRadius={0}
borderTopWidth={index !== 0 ? 1 : 0}
borderColor="gray.7"
p={4}
>
{item.children ? (
<Details key={index}>
{({open, toggle}) => (
<>
<summary onClick={toggle} style={{cursor: 'pointer'}}>
<Flex alignItems="center" justifyContent="space-between">
<Text>{item.title}</Text>
{open ? <ChevronUpIcon /> : <ChevronDownIcon />}
</Flex>
</summary>
<Flex flexDirection="column" mt={2}>
{item.children.map(child => (
<Link key={child.title} href={child.url} py={1} mt={2} fontSize={1} color="inherit">
{child.title}
</Link>
))}
</Flex>
</>
)}
</Details>
) : (
<Link key={index} href={item.url} color="inherit" display="block">
{item.title}
</Link>
)}
</BorderBox>
)
})
return items.map((item, index) => (
lukekarrys marked this conversation as resolved.
Show resolved Hide resolved
<BorderBox
key={item.title}
borderWidth={0}
borderRadius={0}
borderTopWidth={index !== 0 ? 1 : 0}
borderColor="gray.7"
p={4}
>
<Link key={index} href={item.url} color="inherit" display="block">
{item.title}
</Link>
</BorderBox>
))
}

export default NavDrawer
40 changes: 0 additions & 40 deletions theme/src/components/nav-dropdown.js

This file was deleted.

Loading