@primer/doctocat-nextjs 0.5.2 → 0.5.3-rc.1ab2f8f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/components/context/useConfig.tsx +1 -0
- package/components/layout/article/Article.module.css +15 -19
- package/components/layout/code-block/ReactCodeBlock.tsx +12 -1
- package/components/layout/code-block/code-transformer.ts +19 -0
- package/components/layout/index-cards/IndexCards.tsx +15 -7
- package/components/layout/root-layout/index.tsx +1 -0
- package/components/layout/table-of-contents/TableOfContents.module.css +0 -1
- package/css/global.css +2 -1
- package/doctocat.config.js +8 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @primer/doctocat-nextjs
|
|
2
2
|
|
|
3
|
+
## 0.5.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#43](https://github.com/primer/doctocat-nextjs/pull/43) [`1b15bdf`](https://github.com/primer/doctocat-nextjs/commit/1b15bdfcf4b54996f38d20f1da711def23c636bd) Thanks [@rezrah](https://github.com/rezrah)! - Fix lack of `basePath` support on index cards. `href` now prepends the `basePath`.
|
|
8
|
+
|
|
9
|
+
- [#46](https://github.com/primer/doctocat-nextjs/pull/46) [`2b99ba6`](https://github.com/primer/doctocat-nextjs/commit/2b99ba614d1bfe8f1c478b10a61c52df479901c9) Thanks [@rezrah](https://github.com/rezrah)! - Prepend `basePath` from `next.config.js` to paths in Doctocat UI components, where it would previously not resolve correctly.
|
|
10
|
+
|
|
3
11
|
## 0.5.2
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
|
@@ -1,42 +1,38 @@
|
|
|
1
1
|
.Article {
|
|
2
2
|
display: grid;
|
|
3
|
+
grid-template-areas: 'main';
|
|
4
|
+
grid-template-columns: 1fr;
|
|
3
5
|
}
|
|
4
6
|
|
|
5
7
|
.Article--withToc {
|
|
6
8
|
gap: var(--base-size-48);
|
|
9
|
+
grid-template-areas: 'main aside';
|
|
10
|
+
grid-template-columns: 1fr 200px;
|
|
7
11
|
}
|
|
8
12
|
|
|
9
13
|
.main {
|
|
10
|
-
|
|
14
|
+
grid-area: main;
|
|
15
|
+
max-width: 100%;
|
|
16
|
+
overflow-x: hidden;
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
.aside {
|
|
14
|
-
|
|
20
|
+
grid-area: aside;
|
|
21
|
+
position: relative;
|
|
15
22
|
}
|
|
16
23
|
|
|
17
|
-
@media screen and (max-width:
|
|
24
|
+
@media screen and (max-width: 1023px) {
|
|
18
25
|
.Article--withToc {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@media screen and (min-width: 1023px) {
|
|
25
|
-
.main {
|
|
26
|
-
order: 0;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
.aside {
|
|
30
|
-
order: 1;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
.Article--withToc {
|
|
34
|
-
grid-template-columns: 1fr 200px;
|
|
26
|
+
grid-template-areas:
|
|
27
|
+
'aside'
|
|
28
|
+
'main';
|
|
29
|
+
grid-template-columns: 1fr;
|
|
35
30
|
}
|
|
36
31
|
}
|
|
37
32
|
|
|
38
33
|
@media screen and (min-width: 2048px) {
|
|
39
34
|
.Article--withToc {
|
|
35
|
+
grid-template-areas: 'main';
|
|
40
36
|
grid-template-columns: 1fr;
|
|
41
37
|
}
|
|
42
38
|
}
|
|
@@ -3,12 +3,14 @@ import React, {PropsWithChildren, useCallback, useState} from 'react'
|
|
|
3
3
|
import clsx from 'clsx'
|
|
4
4
|
import {LiveProvider, LiveEditor, LiveError, LivePreview} from 'react-live'
|
|
5
5
|
import {useColorMode} from '../../context/color-modes/useColorMode'
|
|
6
|
+
import {useConfig} from '../../context/useConfig'
|
|
6
7
|
import styles from './ReactCodeBlock.module.css'
|
|
7
8
|
import {ActionBar, Button, ThemeProvider} from '@primer/react'
|
|
8
9
|
import {CopyIcon, MoonIcon, SunIcon, UndoIcon} from '@primer/octicons-react'
|
|
9
10
|
import {colorModes} from '../../context/color-modes/context'
|
|
10
11
|
|
|
11
12
|
import {lightTheme, darkTheme} from './syntax-highlighting-themes'
|
|
13
|
+
import {codeTransformer} from './code-transformer'
|
|
12
14
|
|
|
13
15
|
type ReactCodeBlockProps = {
|
|
14
16
|
'data-language': string
|
|
@@ -18,10 +20,19 @@ type ReactCodeBlockProps = {
|
|
|
18
20
|
|
|
19
21
|
export function ReactCodeBlock(props: ReactCodeBlockProps) {
|
|
20
22
|
const {colorMode, setColorMode} = useColorMode()
|
|
23
|
+
const {basePath} = useConfig()
|
|
21
24
|
const initialCode = getCodeFromChildren(props.children)
|
|
22
25
|
const [code, setCode] = useState(initialCode)
|
|
23
26
|
const shouldShowPreview = ['tsx', 'jsx'].includes(props['data-language'])
|
|
24
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Transforms code to prepend basePath to img src attributes
|
|
30
|
+
*/
|
|
31
|
+
const transformCodeWithBasePath = useCallback(
|
|
32
|
+
(sourceCode: string) => codeTransformer(sourceCode, basePath),
|
|
33
|
+
[basePath],
|
|
34
|
+
)
|
|
35
|
+
|
|
25
36
|
const handleReset = useCallback(() => {
|
|
26
37
|
setCode(initialCode)
|
|
27
38
|
}, [initialCode, setCode])
|
|
@@ -34,7 +45,7 @@ export function ReactCodeBlock(props: ReactCodeBlockProps) {
|
|
|
34
45
|
|
|
35
46
|
return (
|
|
36
47
|
<>
|
|
37
|
-
<LiveProvider code={code} scope={props.jsxScope} noInline={noInline}>
|
|
48
|
+
<LiveProvider transformCode={transformCodeWithBasePath} code={code} scope={props.jsxScope} noInline={noInline}>
|
|
38
49
|
<div className={clsx(styles.CodeBlock, 'custom-component')}>
|
|
39
50
|
{shouldShowPreview && (
|
|
40
51
|
<div>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const codeTransformer = (sourceCode: string, basePath: string): string => {
|
|
2
|
+
if (!basePath) return sourceCode
|
|
3
|
+
|
|
4
|
+
// to skip external URLs and other irrelevant paths
|
|
5
|
+
const shouldTransform = (src: string) => !src.startsWith('http') && !src.startsWith('//') && !src.startsWith(basePath)
|
|
6
|
+
|
|
7
|
+
// normalise for absolute (/path) and relative (path) values
|
|
8
|
+
const transformSrc = (src: string) => (src.startsWith('/') ? `${basePath}${src}` : `${basePath}/${src}`)
|
|
9
|
+
|
|
10
|
+
// Assumes all elements with a src attribute are trying to point at Next.js public folder
|
|
11
|
+
return sourceCode.replace(
|
|
12
|
+
/<(\w+)\s+([^>]*\s+)?src=["']([^"']+)["']([^>]*)/g,
|
|
13
|
+
(match, tagName, before = '', src, after) => {
|
|
14
|
+
if (!shouldTransform(src)) return match
|
|
15
|
+
|
|
16
|
+
return `<${tagName} ${before}src="${transformSrc(src)}"${after}`
|
|
17
|
+
},
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -3,6 +3,7 @@ import {Card, Grid} from '@primer/react-brand'
|
|
|
3
3
|
import {DocsItem} from '../../../types'
|
|
4
4
|
import {useColorMode} from '../../context/color-modes/useColorMode'
|
|
5
5
|
import type {StaticImageData} from 'next/image'
|
|
6
|
+
import Link from 'next/link'
|
|
6
7
|
import placeholderDarkOneThumb from './images/dark-1.png'
|
|
7
8
|
import placeholderDarkTwoThumb from './images/dark-2.png'
|
|
8
9
|
import placeholderDarkThreeThumb from './images/dark-3.png'
|
|
@@ -17,6 +18,7 @@ import placeholderLightFiveThumb from './images/light-5.png'
|
|
|
17
18
|
import placeholderLightSixThumb from './images/light-6.png'
|
|
18
19
|
|
|
19
20
|
import styles from './IndexCards.module.css'
|
|
21
|
+
import {useConfig} from '../../context/useConfig'
|
|
20
22
|
|
|
21
23
|
type IndexCardsProps = {
|
|
22
24
|
route: string
|
|
@@ -42,6 +44,7 @@ const lightModePlaceholderThumbs = [
|
|
|
42
44
|
] as unknown as StaticImageData[]
|
|
43
45
|
|
|
44
46
|
export function IndexCards({route, folderData}: IndexCardsProps) {
|
|
47
|
+
const {basePath} = useConfig()
|
|
45
48
|
const lastPlaceholderIndexRef = useRef<number>(-1)
|
|
46
49
|
// We don't want to show children of these pages. E.g. tabbed pages
|
|
47
50
|
const onlyDirectChildren = folderData.filter(item => {
|
|
@@ -85,16 +88,21 @@ export function IndexCards({route, folderData}: IndexCardsProps) {
|
|
|
85
88
|
|
|
86
89
|
const thumbnailUrl =
|
|
87
90
|
colorMode === 'dark'
|
|
88
|
-
? item.frontMatter.thumbnail_darkMode
|
|
89
|
-
|
|
91
|
+
? (item.frontMatter.thumbnail_darkMode
|
|
92
|
+
? `${basePath || ''}${item.frontMatter.thumbnail_darkMode}`
|
|
93
|
+
: null) || getNextPlaceholderIndex(darkModePlaceholderThumbs).src
|
|
94
|
+
: (item.frontMatter.thumbnail ? `${basePath || ''}${item.frontMatter.thumbnail}` : null) ||
|
|
95
|
+
getNextPlaceholderIndex(lightModePlaceholderThumbs).src
|
|
90
96
|
|
|
91
97
|
return (
|
|
92
98
|
<Grid.Column span={{xsmall: 12, small: 12, medium: 12, large: 6, xlarge: 4}} key={item.frontMatter.title}>
|
|
93
|
-
<
|
|
94
|
-
<Card
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
<Link legacyBehavior passHref href={item.route}>
|
|
100
|
+
<Card href="#" hasBorder fullWidth>
|
|
101
|
+
<Card.Image src={thumbnailUrl} alt="" aspectRatio="4:3" />
|
|
102
|
+
<Card.Heading>{item.frontMatter.title}</Card.Heading>
|
|
103
|
+
{item.frontMatter.description && <Card.Description>{item.frontMatter.description}</Card.Description>}
|
|
104
|
+
</Card>
|
|
105
|
+
</Link>
|
|
98
106
|
</Grid.Column>
|
|
99
107
|
)
|
|
100
108
|
})}
|
package/css/global.css
CHANGED
|
@@ -18,7 +18,8 @@ body {
|
|
|
18
18
|
margin-block-start: 0 !important;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
/* Pre styles are scoped to Nextra code blocks to prevent unintended styling of other code blocks (e.g., ReactCodeBlock previews). */
|
|
22
|
+
.nextra-code pre {
|
|
22
23
|
background-color: var(--brand-color-canvas-subtle);
|
|
23
24
|
border-radius: var(--brand-borderRadius-large);
|
|
24
25
|
padding-top: 1rem;
|
package/doctocat.config.js
CHANGED
|
@@ -10,7 +10,7 @@ const withNextra = nextra({
|
|
|
10
10
|
* Relies on `transpilePackages: ['@primer/doctocat-nextjs'],` being set in the next.config
|
|
11
11
|
*/
|
|
12
12
|
function withDoctocat(config = {}) {
|
|
13
|
-
|
|
13
|
+
const finalConfig = {
|
|
14
14
|
...withNextra(),
|
|
15
15
|
images: {
|
|
16
16
|
unoptimized: true,
|
|
@@ -18,6 +18,13 @@ function withDoctocat(config = {}) {
|
|
|
18
18
|
|
|
19
19
|
...config,
|
|
20
20
|
}
|
|
21
|
+
|
|
22
|
+
// Enables basePath in next.config.js to be used inside Doctocat
|
|
23
|
+
if (finalConfig.basePath) {
|
|
24
|
+
process.env.NEXT_PUBLIC_DOCTOCAT_BASE_PATH = finalConfig.basePath
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return finalConfig
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
export default withDoctocat
|