refactor(v2): move headerLinks -> themeConfig & rm dead code (#1442)

* refactor(v2): move headerLinks -> themeConfig & rm dead code

* rm -rf dead code
This commit is contained in:
Endi 2019-05-08 15:47:32 +07:00 committed by GitHub
parent 384fd5708f
commit 174ad279a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 71 deletions

View file

@ -15,12 +15,10 @@ import SearchBar from '@theme/SearchBar';
function Navbar() { function Navbar() {
const context = useDocusaurusContext(); const context = useDocusaurusContext();
const {siteConfig = {}} = context; const {siteConfig = {}} = context;
// TODO: navbar headerlinks should depends on theme, not siteConfig;
const { const {
baseUrl, baseUrl,
headerLinks,
headerIcon, headerIcon,
themeConfig: {algolia}, themeConfig: {algolia, headerLinks = []},
title, title,
disableHeaderTitle, disableHeaderTitle,
} = siteConfig; } = siteConfig;

View file

@ -20,8 +20,7 @@ class Search extends React.Component {
} }
componentDidMount() { componentDidMount() {
const {siteConfig = {}, metadata = {}} = this.context; const {siteConfig = {}} = this.context;
const {version: thisVersion, language: thisLanguage} = metadata;
const { const {
themeConfig: {algolia}, themeConfig: {algolia},
} = siteConfig; } = siteConfig;
@ -35,11 +34,7 @@ class Search extends React.Component {
apiKey: algolia.apiKey, apiKey: algolia.apiKey,
indexName: algolia.indexName, indexName: algolia.indexName,
inputSelector: '#search_input_react', inputSelector: '#search_input_react',
algoliaOptions: JSON.parse( algoliaOptions: algolia.algoliaOptions,
JSON.stringify(algolia.algoliaOptions)
.replace('VERSION', thisVersion)
.replace('LANGUAGE', thisLanguage),
),
}); });
}); });
} else { } else {
@ -55,8 +50,8 @@ class Search extends React.Component {
<input <input
id="search_input_react" id="search_input_react"
type="search" type="search"
placeholder="Search docs" placeholder="Search"
aria-label="Search docs" aria-label="Search"
/> />
) : null; ) : null;
} }

View file

@ -18,6 +18,8 @@ headerLinks: [
], ],
``` ```
- `headerLinks` is now moved to themeConfig
# Additions # Additions
### Presets ### Presets

View file

@ -12,10 +12,6 @@ module.exports = {
projectName: 'sakura', projectName: 'sakura',
baseUrl: '/sakura/', baseUrl: '/sakura/',
url: 'https://docusaurus.io', url: 'https://docusaurus.io',
headerLinks: [
{doc: 'foo/bar', label: 'Docs'},
{page: 'hello/world', label: 'Hello'},
],
headerIcon: 'img/docusaurus.svg', headerIcon: 'img/docusaurus.svg',
favicon: 'img/docusaurus.ico', favicon: 'img/docusaurus.ico',
plugins: [ plugins: [

View file

@ -12,10 +12,6 @@ module.exports = {
projectName: 'hello', projectName: 'hello',
baseUrl: '/', baseUrl: '/',
url: 'https://docusaurus.io', url: 'https://docusaurus.io',
headerLinks: [
{doc: 'foo/bar', label: 'Docs'},
{page: 'hello/world', label: 'Hello'},
],
headerIcon: 'img/docusaurus.svg', headerIcon: 'img/docusaurus.svg',
favicon: 'img/docusaurus.ico', favicon: 'img/docusaurus.ico',
plugins: [ plugins: [

View file

@ -22,19 +22,6 @@ Object {
"baseUrl": "/", "baseUrl": "/",
"favicon": "img/docusaurus.ico", "favicon": "img/docusaurus.ico",
"headerIcon": "img/docusaurus.svg", "headerIcon": "img/docusaurus.svg",
"headerLinks": Array [
Object {
"doc": "foo/bar",
"label": "Docs",
},
Object {
"label": "Hello",
"page": "hello/world",
},
Object {
"languages": true,
},
],
"organizationName": "endiliey", "organizationName": "endiliey",
"plugins": Any<Array>, "plugins": Any<Array>,
"projectName": "hello", "projectName": "hello",
@ -52,7 +39,7 @@ Object {
expect(() => { expect(() => {
loadConfig(siteDir); loadConfig(siteDir);
}).toThrowErrorMatchingInlineSnapshot( }).toThrowErrorMatchingInlineSnapshot(
`"The required field(s) 'favicon', 'headerLinks', 'headerIcon', 'organizationName', 'projectName', 'tagline', 'url' are missing from docusaurus.config.js"`, `"The required field(s) 'favicon', 'headerIcon', 'organizationName', 'projectName', 'tagline', 'url' are missing from docusaurus.config.js"`,
); );
}); });
@ -61,7 +48,7 @@ Object {
expect(() => { expect(() => {
loadConfig(siteDir); loadConfig(siteDir);
}).toThrowErrorMatchingInlineSnapshot( }).toThrowErrorMatchingInlineSnapshot(
`"The required field(s) 'favicon', 'headerLinks', 'headerIcon' are missing from docusaurus.config.js"`, `"The required field(s) 'favicon', 'headerIcon' are missing from docusaurus.config.js"`,
); );
}); });
@ -70,7 +57,7 @@ Object {
expect(() => { expect(() => {
loadConfig(siteDir); loadConfig(siteDir);
}).toThrowErrorMatchingInlineSnapshot( }).toThrowErrorMatchingInlineSnapshot(
`"The required field(s) 'baseUrl', 'favicon', 'headerLinks', 'headerIcon', 'organizationName', 'projectName', 'tagline', 'title', 'url' are missing from docusaurus.config.js"`, `"The required field(s) 'baseUrl', 'favicon', 'headerIcon', 'organizationName', 'projectName', 'tagline', 'title', 'url' are missing from docusaurus.config.js"`,
); );
}); });
}); });

View file

@ -13,7 +13,6 @@ const {CONFIG_FILE_NAME} = require('../../constants');
const REQUIRED_FIELDS = [ const REQUIRED_FIELDS = [
'baseUrl', 'baseUrl',
'favicon', 'favicon',
'headerLinks',
'headerIcon', 'headerIcon',
'organizationName', 'organizationName',
'projectName', 'projectName',
@ -24,11 +23,7 @@ const REQUIRED_FIELDS = [
const OPTIONAL_FIELDS = [ const OPTIONAL_FIELDS = [
'customFields', 'customFields',
'defaultLanguage',
'disableHeaderTitle',
'githubHost', 'githubHost',
'highlight',
'markdownPlugins',
'plugins', 'plugins',
'presets', 'presets',
'themeConfig', 'themeConfig',
@ -66,31 +61,6 @@ function loadConfig(siteDir, deleteCache = true) {
// Merge default config with loaded config. // Merge default config with loaded config.
const config = {...DEFAULT_CONFIG, ...loadedConfig}; const config = {...DEFAULT_CONFIG, ...loadedConfig};
// Build final headerLinks based on siteConfig.
const {headerLinks} = config;
// Add language dropdown to end if location not specified.
let languages = false;
headerLinks.forEach(link => {
if (link.languages) {
languages = true;
}
});
if (!languages) {
headerLinks.push({languages: true});
}
let search = false;
headerLinks.forEach(link => {
// Append search bar if location not specified.
if (link.search) {
search = true;
}
});
if (!search && config.algolia) {
headerLinks.push({search: true});
}
config.headerLinks = headerLinks;
// User's own array of custom fields/ // User's own array of custom fields/
// e.g: if they want to include some.field so they can access it later from `props.siteConfig`. // e.g: if they want to include some.field so they can access it later from `props.siteConfig`.
const {customFields = []} = config; const {customFields = []} = config;

View file

@ -12,11 +12,6 @@ module.exports = {
projectName: 'docusaurus', projectName: 'docusaurus',
baseUrl: '/', baseUrl: '/',
url: 'https://docusaurus-2.netlify.com', url: 'https://docusaurus-2.netlify.com',
headerLinks: [
{url: 'docs/introduction', label: 'Docs'},
{url: 'blog', label: 'Blog'},
{url: 'feedback/', label: 'Feedback'},
],
headerIcon: 'img/docusaurus.svg', headerIcon: 'img/docusaurus.svg',
favicon: 'img/docusaurus.ico', favicon: 'img/docusaurus.ico',
themeConfig: { themeConfig: {
@ -25,6 +20,11 @@ module.exports = {
indexName: 'docusaurus-2', indexName: 'docusaurus-2',
algoliaOptions: {}, algoliaOptions: {},
}, },
headerLinks: [
{url: 'docs/introduction', label: 'Docs'},
{url: 'blog', label: 'Blog'},
{url: 'feedback/', label: 'Feedback'},
],
}, },
presets: [ presets: [
[ [