Docusaurus configuration file in detail.
The docusaurus.config.js
file is crucial for setting up a Docusaurus site. It defines the overall configuration for your documentation website, including metadata, URL structure, plugins, and themes. This file runs in a Node.js environment, so you should avoid using client-side code (like browser APIs or JSX) within it.
Key Components
-
TypeScript Checking
/** @type {import('@docusaurus/types').Config} */
This line enables TypeScript checking for the configuration file, ensuring that the structure adheres to the expected configuration types. It helps with autocompletion and error checking in editors.
-
Basic Configuration
const config = {
title: 'HETFS',
tagline: 'Master the Art of Document Writing',
favicon: 'img/favicon.ico',
};- title: The title of your website, used in metadata and as the browser tab title.
- tagline: A brief description or slogan for your site.
- favicon: The path to your site's favicon.
-
Site URLs
url: 'https://your-docusaurus-site.example.com',
baseUrl: '/',- url: The production URL of your site.
- baseUrl: The base path under which your site is served. For GitHub Pages, this is often set to
/<projectName>/
.
-
Deployment Configuration
organizationName: 'facebook',
projectName: 'docusaurus',These fields are required if you are using GitHub Pages for hosting. They specify your GitHub organization and project repository name.
-
Link Handling
onBrokenLinks: 'throw',
onBrokenMarkdownLinks: 'warn',These options define how Docusaurus should handle broken links:
throw
: Will stop the build if a broken link is found.warn
: Will log a warning but allow the build to continue for broken Markdown links.
-
Internationalization (i18n)
i18n: {
defaultLocale: 'en',
locales: ['en'],
},This configuration allows you to set up localization for your site, enabling support for multiple languages.
-
Presets
presets: [
[
'classic',
{
docs: {
sidebarPath: './sidebars.js',
editUrl: 'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
},
blog: {
showReadingTime: true,
feedOptions: {
type: ['rss', 'atom'],
xslt: true,
},
editUrl: 'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
},
theme: {
customCss: './src/css/custom.css',
},
},
],
],- classic: This preset includes essential features like documentation and blogging.
- docs: Configuration for documentation, including the sidebar and edit URL.
- blog: Configuration for the blog, allowing features like reading time and feeds.
- theme: Custom CSS for the site's styling.
-
Theme Configuration
themeConfig: {
navbar: {
title: 'My Site',
logo: {
alt: 'My Site Logo',
src: 'img/logo.svg',
},
items: [
{
type: 'docSidebar',
sidebarId: 'tutorialSidebar',
position: 'left',
label: 'Tutorial',
},
{ to: '/blog', label: 'Blog', position: 'left' },
{
href: 'https://github.com/facebook/docusaurus',
label: 'GitHub',
position: 'right',
},
],
},
footer: {
style: 'dark',
links: [
{
title: 'Docs',
items: [
{
label: 'Tutorial',
to: '/docs/intro',
},
],
},
{
title: 'Community',
items: [
{
label: 'Stack Overflow',
href: 'https://stackoverflow.com/questions/tagged/docusaurus',
},
{
label: 'Discord',
href: 'https://discordapp.com/invite/docusaurus',
},
{
label: 'X',
href: 'https://x.com/docusaurus',
},
],
},
{
title: 'More',
items: [
{
label: 'Blog',
to: '/blog',
},
{
label: 'GitHub',
href: 'https://github.com/facebook/docusaurus',
},
],
},
],
copyright: `Copyright © ${new Date().getFullYear()} My Project, Inc. Built with Docusaurus.`,
},
prism: {
theme: prismThemes.github,
darkTheme: prismThemes.dracula,
},
},- navbar: Customizes the navigation bar with links to documentation, blog, and GitHub.
- footer: Configures the footer with links and copyright information.
- prism: Sets up syntax highlighting themes for code blocks.
Conclusion
This configuration file allows you to customize various aspects of your Docusaurus site, from basic metadata to advanced features like theming and plugins. By utilizing the structure provided, you can create a well-organized and user-friendly documentation site tailored to your project’s needs.