-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(docs): Fixing doc navigation link issues (#2760)
* fix(docs): Fixing doc navigation link issues * Fixed code review comments * refactor(docs): split up util functions * fix(ComponentControls): show menu when deep linking * fix(docs): handle ComponentExample hash names * fix(docs): export getNewHash in utils * consolidate hash functions and usage
- Loading branch information
1 parent
4f82da4
commit 14b7eca
Showing
11 changed files
with
186 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Get the Webpack Context for all doc site examples. | ||
*/ | ||
const exampleContext = require.context('docs/app/Examples/', true, /(\w+Example\w*|index)\.js$/) | ||
|
||
export default exampleContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import _ from 'lodash/fp' | ||
|
||
/** | ||
* Creates a short hash path from an example filename. | ||
* | ||
* Typical Hash structure ${pathname}-${section}-${exampleName} | ||
* shorten to new structure ${section} - -${exampleName without "component-example"} | ||
* @param {string} examplePath | ||
*/ | ||
const examplePathToHash = (examplePath) => { | ||
const hashParts = examplePath.split('/').filter(part => part !== '.') | ||
|
||
if (!hashParts.length) return examplePath | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const [type, componentName, section, exampleName] = hashParts | ||
|
||
// ButtonExample => Button | ||
// ButtonExampleButton => Button | ||
// ButtonExampleActive => Active | ||
const shortExampleName = exampleName.replace(`${componentName}Example`, '').replace('.js', '') | ||
|
||
return _.kebabCase(`${section}-${shortExampleName || componentName}`) | ||
} | ||
|
||
export default examplePathToHash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import _ from 'lodash/fp' | ||
|
||
import exampleContext from './exampleContext' | ||
import examplePathToHash from './examplePathToHash' | ||
|
||
/** | ||
* Check whether given hash is old or new, redirect to new hash in case of old one | ||
* @param {string} hash | ||
*/ | ||
const isOldHash = (hash) => { | ||
if (!hash) return false | ||
|
||
const [firstPart] = hash.split('-') || [] | ||
|
||
if (!firstPart) return false | ||
|
||
return !_.includes(firstPart, ['types', 'states', 'variations']) | ||
} | ||
|
||
/** | ||
* Retrieve hash string from location path | ||
* @param {string} hash | ||
*/ | ||
const getFormattedHash = (hash) => { | ||
const hashString = (hash || '').replace('#', '') | ||
|
||
if (isOldHash(hashString)) { | ||
const filename = `${_.startCase(hashString).replace(/\s/g, '')}` | ||
const completeFilename = `/${filename}.js` | ||
const exampleKeys = exampleContext.keys() | ||
const examplePath = _.find(key => _.endsWith(completeFilename, key), exampleKeys) | ||
|
||
// found old to new hashString match | ||
if (examplePath) { | ||
return examplePathToHash(examplePath) | ||
} | ||
} | ||
|
||
return hashString | ||
} | ||
|
||
export default getFormattedHash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import _ from 'lodash/fp' | ||
|
||
import exampleContext from './exampleContext' | ||
import examplePathToHash from './examplePathToHash' | ||
import isOldHash from './isOldHash' | ||
|
||
/** | ||
* get new hash using old | ||
* @param {string} hash | ||
*/ | ||
const getNewHash = (hash) => { | ||
if (isOldHash(hash)) { | ||
const fileName = _.startCase(hash).replace(/\s/g, '') | ||
const str = exampleContext.keys().find(item => item.indexOf(fileName) !== -1) | ||
// found old to new hash match | ||
if (str) { | ||
return examplePathToHash(str.replace(/((\.)(?:[\w]+))|(^\.\/)/g, '')) | ||
} | ||
} | ||
|
||
return hash | ||
} | ||
|
||
export default getNewHash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,8 @@ | ||
import _ from 'lodash/fp' | ||
|
||
import * as semanticUIReact from 'src' | ||
import { META } from 'src/lib' | ||
|
||
export * from './constants' | ||
export exampleContext from './exampleContext' | ||
export getComponentGroup from './getComponentGroup' | ||
export getFormattedHash from './getFormattedHash' | ||
export getSeeItems from './getSeeItems' | ||
export scrollToAnchor from './scrollToAnchor' | ||
|
||
/** | ||
* Get the Webpack Context for all doc site examples. | ||
*/ | ||
export const exampleContext = require.context('docs/app/Examples/', true, /(\w+Example\w*|index)\.js$/) | ||
|
||
export const parentComponents = _.flow( | ||
_.filter(META.isParent), | ||
_.sortBy('_meta.name'), | ||
)(semanticUIReact) | ||
export examplePathToHash from './examplePathToHash' | ||
export parentComponents from './parentComponents' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Check whether given hash is old or new, redirect to new hash in case of old one | ||
* @param {string} hash | ||
*/ | ||
const isOldHash = (hash) => { | ||
const expectedTypes = ['types', 'states', 'variations'] | ||
const hashParts = hash.split('-') || [] | ||
return !(expectedTypes.findIndex(item => item === hashParts[0]) !== -1) | ||
} | ||
|
||
export default isOldHash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import _ from 'lodash/fp' | ||
|
||
import * as semanticUIReact from 'src' | ||
import { META } from 'src/lib' | ||
|
||
const parentComponents = _.flow(_.filter(META.isParent), _.sortBy('_meta.name'))(semanticUIReact) | ||
|
||
export default parentComponents |