forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lib modules for refactored siteTree
- Loading branch information
Showing
5 changed files
with
166 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const Page = require('./page') | ||
const { sortBy } = require('lodash') | ||
|
||
let basePath | ||
|
||
module.exports = async function createTree (originalPath, langObj) { | ||
// Do not reset this value on recursive runs | ||
if (!basePath) basePath = originalPath | ||
|
||
// On recursive runs, this is processing page.children items in `/<link>` format. | ||
// If the path exists as is, assume this is a directory with a child index.md. | ||
// Otherwise, assume it's a child .md file and add `.md` to the path. | ||
let filepath | ||
try { | ||
await fs.promises.access(originalPath) | ||
filepath = `${originalPath}/index.md` | ||
} catch { | ||
filepath = `${originalPath}.md` | ||
} | ||
|
||
const relativePath = filepath.replace(`${basePath}/`, '') | ||
const localizedBasePath = path.join(__dirname, '..', langObj.dir, 'content') | ||
|
||
// Initialize the Page! This is where the magic happens (sorry). | ||
const page = await Page.init({ | ||
basePath: localizedBasePath, | ||
relativePath, | ||
languageCode: langObj.code | ||
}) | ||
|
||
if (!page) { | ||
// Do not throw an error if early access is not available | ||
if (relativePath.startsWith('early-access')) return | ||
|
||
throw Error(`Cannot initialize page for ${filepath}`) | ||
} | ||
|
||
// Create the root tree object on the first run, and create children recursively | ||
const item = { | ||
relativePath, | ||
title: page.shortTitle || page.title, | ||
// parentPath: parentPath || null, | ||
page | ||
} | ||
|
||
// Process frontmatter children recursively | ||
if (item.page.children) { | ||
item.childPages = sortBy( | ||
(await Promise.all(item.page.children | ||
.map(async (child) => await createTree(path.join(originalPath, child), langObj)))) | ||
.filter(Boolean), | ||
// Sort by the ordered array of children in the frontmatter | ||
item.page.children | ||
) | ||
} | ||
|
||
return item | ||
} |
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,17 @@ | ||
module.exports = function getDocumentType (relativePath) { | ||
if (!relativePath.endsWith('index.md')) { | ||
return 'article' | ||
} | ||
|
||
// Derive the document type from the path segment length | ||
switch (relativePath.split('/').length) { | ||
case 1: | ||
return 'homepage' | ||
case 2: | ||
return 'product' | ||
case 3: | ||
return 'category' | ||
case 4: | ||
return 'mapTopic' | ||
} | ||
} |
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