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
[nextra]: refactor loaders.ts
, provide type for PageOpts
in loader
#579
Conversation
🦋 Changeset detectedLatest commit: dfa509e The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
interface LayoutProps { | ||
filename: string | ||
pageMap: PageMapItem[] | ||
meta: Meta | ||
titleText: string | null | ||
headings?: Heading[] | ||
timestamp?: number | ||
} | ||
|
||
const Content: React.FC<LayoutProps> = ({ | ||
const Content = ({ | ||
filename, | ||
pageMap, | ||
meta, | ||
titleText, | ||
headings, | ||
timestamp, | ||
children | ||
}) => { | ||
}: PageOpts & { children: ReactNode }): ReactElement => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need to specify props since we'll receive all PageOpts anyway
const createLayout = (opts: PageOpts, config: DocsThemeConfig) => { |
<Content {...opts}>{page}</Content> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could use PropsWithChildren<PageOpts>
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PropsWithChildren has optional children, and like this his is required
const extendedConfig = Object.assign({}, defaultConfig, config, opts) | ||
|
||
const createLayout = (opts: PageOpts, config: DocsThemeConfig) => { | ||
const extendedConfig = { | ||
...defaultConfig, | ||
...config, | ||
unstable_flexsearch: opts.unstable_flexsearch | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was a mistake, we don't need to copy all PageOpts
in ThemeConfigContext
, but only unstable_flexsearch
packages/nextra/src/loader.ts
Outdated
const pageOpts: Omit<PageOpts, 'pageMap' | 'titleText'> = { | ||
filename: slash(filename), | ||
route: slash(route), | ||
meta, | ||
headings, | ||
timestamp, | ||
unstable_flexsearch, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now pageOpts
is type safe
packages/nextra/src/types.ts
Outdated
codeblocks: boolean | ||
} | ||
titleText?: string | ||
headings: (Heading & { value: string })[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before headings was marked optional, but this is false, headings always exist
nextra/packages/nextra/src/compile.ts
Line 14 in 153aee6
compiler.data('headings', []) |
packages/nextra/src/loader.ts
Outdated
filename: "${slash(filename)}", | ||
route: "${slash(route)}", | ||
meta: ${JSON.stringify(data)}, | ||
pageMap: __nextra_pageMap__, | ||
titleText: typeof titleText === 'string' ? titleText : undefined, | ||
headings: ${JSON.stringify(headings)}, | ||
${timestamp ? `timestamp: ${timestamp},\n` : ''} | ||
${ | ||
unstable_flexsearch | ||
? `unstable_flexsearch: ${JSON.stringify(unstable_flexsearch)}` | ||
: '' | ||
} | ||
...${JSON.stringify(pageOpts)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now it's looks much friendly
153aee6
to
ddb9810
Compare
@shuding sure, I'm planning to resolve conflicts on all PRs :) |
ddb9810
to
322f598
Compare
322f598
to
3627650
Compare
3627650
to
c4e8f09
Compare
meta: Meta | ||
meta: PageOpts['meta'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was a mistake, it's not meta.json#theme
but frontmatter data!
export interface Meta extends PageTheme { | ||
title: string | ||
} | ||
|
||
type Meta = Record<string, any> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said Meta is frontmatter data and not meta.json#theme
c4e8f09
to
b36c88c
Compare
loaders.ts
, provide type for PageOpts
in loader
if (repository.isShallow() && !wasShallowWarningPrinted) { | ||
if (process.env.VERCEL) { | ||
console.warn( | ||
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. Set the VERCEL_DEEP_CLONE=true environment variable to enable deep cloning.' | ||
) | ||
} else if (process.env.GITHUB_ACTION) { | ||
console.warn( | ||
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented. See https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches to fetch all the history.' | ||
) | ||
} else { | ||
console.warn( | ||
'[nextra] The repository is shallow cloned, so the latest modified time will not be presented.' | ||
) | ||
} | ||
wasShallowWarningPrinted = true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be moved outside of loader (inside of IIFE) and wasShallowWarningPrinted
variable can be removed
Thank you! |
shuding#579) * chore(nextra/blog/docs): provide types for PageOpts in loader * yet more readable * move `shallow cloned` console messages outside of loader
No description provided.