Skip to content
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

Move sidebar logic to theme #3133

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions plugins/gatsby-remark-dvc-linker/apiLinker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')
const { getItemByPath } = require('../../src/utils/shared/sidebar')
const {
getItemByPath
} = require('../gatsby-theme-iterative-docs/sidebar-helpers')
const sidebar = require('../../src/gatsby-theme-iterative-docs/sidebar')

const DVC_API_REGEXP = /dvc.api([a-z-._]*\(\)$)?/
const METHOD_REGEXP = /^[a-z-._]*\(\)$/
Expand All @@ -25,7 +28,7 @@ module.exports = astNode => {
url = `${API_ROOT}${method}`
}

const isMethodPageExists = getItemByPath(url)
const isMethodPageExists = getItemByPath(sidebar, url)
if (isMethodPageExists) {
createLinkNode(url, astNode)
}
Expand Down
12 changes: 9 additions & 3 deletions plugins/gatsby-remark-dvc-linker/commandLinker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')
const { getItemByPath } = require('../../src/utils/shared/sidebar')
const {
getItemByPath
} = require('../gatsby-theme-iterative-docs/sidebar-helpers')
const sidebar = require('../../src/gatsby-theme-iterative-docs/sidebar')

const DVC_REGEXP = /dvc\s+[a-z][a-z-.]*/
const COMMAND_REGEXP = /^[a-z][a-z-]*$/
Expand All @@ -16,11 +19,14 @@ module.exports = astNode => {
let url

const hasThirdSegment = parts[2] && COMMAND_REGEXP.test(parts[2])
const isCommandPageExists = getItemByPath(`${COMMAND_ROOT}${parts[1]}`)
const isCommandPageExists = getItemByPath(
sidebar,
`${COMMAND_ROOT}${parts[1]}`
)
const isSubcommandPageExists =
isCommandPageExists &&
hasThirdSegment &&
getItemByPath(`${COMMAND_ROOT}${parts[1]}/${parts[2]}`)
getItemByPath(sidebar, `${COMMAND_ROOT}${parts[1]}/${parts[2]}`)

if (isSubcommandPageExists) {
url = `${COMMAND_ROOT}${parts[1]}/${parts[2]}`
Expand Down
7 changes: 5 additions & 2 deletions plugins/gatsby-remark-dvc-linker/liveLinker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-env node */

const { createLinkNode } = require('./helpers')
const { getItemByPath } = require('../../src/utils/shared/sidebar')
const {
getItemByPath
} = require('../gatsby-theme-iterative-docs/sidebar-helpers')
const sidebar = require('../../src/gatsby-theme-iterative-docs/sidebar')

const LIVE_API_REGEXP = /Live.([a-z-._]*\(\)$)?/
const METHOD_REGEXP = /^[a-z-._]*\(\)$/
Expand All @@ -17,7 +20,7 @@ module.exports = astNode => {
const method = isMethod && parts[1].slice(0, -2)
const url = `${API_ROOT}${method}`

const isMethodPageExists = getItemByPath(url)
const isMethodPageExists = getItemByPath(sidebar, url)
if (isMethodPageExists) {
createLinkNode(url, astNode)
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/gatsby-theme-iterative-docs/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
PATH_ROOT: '/doc',
FILE_ROOT: '/docs/',
FILE_EXTENSION: '.md'
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
/* eslint-env node */
/*
These helpers normalize sidebar structure and create all the resources needed.
This prevents future recalculations.

Target structure example:

{
label: "Add Files or Directories",
path: "/doc/start/add-files",
source: "/doc/start/add-files.md",
prev: "/doc/start/configure",
next: "/doc/start/share-data",
icon: "house",
style: "customClass",
tutorials: {
katacoda: "https://www.katacoda.com/dvc/courses/get-started"
}
children: []
}
*/
const { PATH_ROOT, FILE_ROOT, FILE_EXTENSION } = require('./constants')

const { titleCase } = require('title-case')
const sidebar = require('../../../content/docs/sidebar.json')
const { findItemByField } = require('./sidebar-helpers')

const PATH_ROOT = '/doc'
const FILE_ROOT = '/docs/'
const FILE_EXTENSION = '.md'
const { titleCase } = require('title-case')

function dvcTitleCase(slug) {
return titleCase(slug.replace(/dvc/g, 'DVC').replace(/-/g, ' '))
}

function findPrevItemWithSource(data, item) {
if (item && item.source) {
return item
} else if (item && item.prev) {
const prevItem = findItemByField(data, 'path', item.prev)

return findPrevItemWithSource(data, prevItem)
}
}

function validateRawItem({ slug, source, children, type, url }) {
const isSourceDisabled = source === false

Expand All @@ -53,33 +40,6 @@ function validateRawItem({ slug, source, children, type, url }) {
}
}

function findItemByField(data, field, targetValue) {
if (data.length) {
for (let i = 0; i < data.length; i++) {
const { children } = data[i]

if (data[i][field] === targetValue) {
return data[i]
} else if (children) {
const result = findItemByField(children, field, targetValue)
if (result) {
return result
}
}
}
}
}

function findPrevItemWithSource(data, item) {
if (item && item.source) {
return item
} else if (item && item.prev) {
const prevItem = findItemByField(data, 'path', item.prev)

return findPrevItemWithSource(data, prevItem)
}
}

function normalizeItem({ rawItem, parentPath, resultRef, prevRef }) {
validateRawItem(rawItem)

Expand Down Expand Up @@ -122,12 +82,10 @@ function normalizeItem({ rawItem, parentPath, resultRef, prevRef }) {
}
}

function normalizeSidebar({
function normalizeSidebar(
data,
parentPath,
parentResultRef,
startingPrevRef
}) {
{ parentPath = '', parentResultRef, startingPrevRef } = {}
) {
Comment on lines -125 to +88
Copy link
Contributor Author

@rogermparent rogermparent Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small tweak to the signature to make this function more natural to use IMO, the rest of the args are only used in recursive calls so the function's practically unary.

const currentResult = []
const resultRef = parentResultRef || currentResult
let prevRef = startingPrevRef
Expand All @@ -147,8 +105,7 @@ function normalizeSidebar({
}

if (rawItem.children) {
normalizedItem.children = normalizeSidebar({
data: rawItem.children,
normalizedItem.children = normalizeSidebar(rawItem.children, {
parentPath: `${parentPath}${rawItem.slug}/`,
parentResultRef: resultRef,
startingPrevRef: normalizedItem
Expand All @@ -165,72 +122,4 @@ function normalizeSidebar({
return currentResult
}

/*
* Exports
*/

const normalizedSidebar = normalizeSidebar({
data: sidebar,
parentPath: ''
})

function findChildWithSource(item) {
// Return item unchanged if isn't root-relative
if (!item.path.startsWith('/')) return item
return item.source
? item
: findChildWithSource(item.children && item.children[0])
}

function getFirstPage() {
return findChildWithSource(normalizedSidebar[0]).path
}

function getItemByPath(path) {
const normalizedPath = path.replace(/\/$/, '')
const isRoot = normalizedPath === PATH_ROOT
const item = isRoot
? normalizedSidebar[0]
: findItemByField(normalizedSidebar, 'path', normalizedPath)

if (!item) return false

return findChildWithSource(item)
}

function getItemBySource(source) {
const item = findItemByField(normalizedSidebar, 'source', source)

return item || false
}

function getPathWithSource(path) {
return getItemByPath(path).path
}
function getParentsListFromPath(path) {
// If path is the homepage, indicate that it's the only one active.
// This will have to change if we add children under home, but we don't currently.
if (path === PATH_ROOT) return [PATH_ROOT]

let currentPath = PATH_ROOT

return path
.replace(PATH_ROOT + '/', '')
.split('/')
.map(part => {
const path = `${currentPath}/${part}`
currentPath = path

return path
})
}

module.exports = {
structure: normalizedSidebar,
findChildWithSource,
getItemByPath,
getItemBySource,
getPathWithSource,
getParentsListFromPath,
getFirstPage
}
module.exports = normalizeSidebar
Loading