Skip to content
This repository was archived by the owner on Aug 9, 2020. It is now read-only.
Merged
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
"it": true,
"beforeEach": true,
"afterEach": true
},
"rules": {
"react/no-did-mount-set-state": 0
}
},
"pkg": {
Expand Down
1 change: 1 addition & 0 deletions src/core/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async function dirTree (baseDir, opts = {}) {
// resolve the path to an absolute path
path: syspath.resolve(path),
// keep the relative path so we can easily construct a url
// NOTE: needs to be an empty string for the base dir, so it doesn't become part of the url
path_relative: syspath.basename(path === baseDir ? '' : path),
}

Expand Down
66 changes: 32 additions & 34 deletions src/core/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function hydrateTree (tree, config, onRegenerate) {
const {
path_relative,
childrenIndex,
children,
children = [],
} = item

// hoist the index file and use it instead of the current item,
Expand Down Expand Up @@ -97,24 +97,35 @@ async function hydrateTree (tree, config, onRegenerate) {
)
)

hydratedItem.input = metaData.input || hoistedItem.path
hydratedItem.outputDir = syspath.join(config.output, hydratedItem.url)

// ensure there are no duplicated urls
if (urls[hydratedItem.url]) {
const duplicated = [hydratedItem.url, hoistedItem.path, urls[hydratedItem.url]]
throw new Error(`Duplicated URL was found: ${duplicated.join('\n\t- ')}`)
}

// url is now taken, like most women
urls[hydratedItem.url] = hoistedItem.path

// add url to the sitemap
const fullUrl = `${config.domain}${hydratedItem.url}`
sitemap.addUrl(fullUrl, metaData.sitemap)

hydratedItem.input = metaData.input || hoistedItem.path
hydratedItem.outputDir = syspath.join(
config.output,
hydratedItem.url,
)
// pull in source items if one exists
if (metaData.source) {
const source = await walkSource(config.temp, hoistedItem.path, metaData)
const sourceHydrated = await _recursive(source, hydratedItem)

// don't inherit these items from the source
delete sourceHydrated.path
delete sourceHydrated.title

// replace current item data with the source data
Object.assign(hydratedItem, sourceHydrated)
// don't register the url when there is a source (since item gets replaced)
} else {
// url is now taken, like most women
urls[hydratedItem.url] = hoistedItem.path

// add url to the sitemap
const fullUrl = `${config.domain}${hydratedItem.url}`
sitemap.addUrl(fullUrl, metaData.sitemap)
}
}

// get sub items from the front matter
Expand All @@ -123,42 +134,29 @@ async function hydrateTree (tree, config, onRegenerate) {
strategy: mergeStrategy,
} = normalizeItems(metaData)

// @TODO: inherit front matter from root source item
// pull in source items if one exists
const sourced = metaData.source
// fetch the source and walk the resulting directory
? await walkSource(config.temp, hoistedItem.path, metaData)
// no source on this item
: {}

// don't allow a source if there are already children. @TODO: merge instead?
if (children && sourced.children) {
throw new Error(`Cannot use a source when there are sub items:\n\t- ${hoistedItem.path}`)
}

// recurse sub items from the dir tree
const childrenItems = sourced.children || children || []
const childrenItemsUnsorted = await Promise.all(
childrenItems
children
.filter(({ index }) => !index)
.map(childItem => _recursive(
childItem,
hydratedItem,
metaDataItems,
))
.map(childItem => _recursive(childItem, hydratedItem, metaDataItems))
)

// sort alphabetically by default
const childrenSorted = childrenItemsUnsorted
.sort((a, b) => a.title.localeCompare(b.title))

// @TODO: figure out how to remove items_pre/append from the result
hydratedItem.items = mergeLeftByKey(metaDataItems, childrenSorted, {
const mergedItems = mergeLeftByKey(metaDataItems, childrenSorted, {
key: 'path',
name: hoistedItem.path,
strategy: mergeStrategy,
})

hydratedItem.items = [
...mergedItems || [],
...hydratedItem.items || [],
]

return hydratedItem
}

Expand Down
10 changes: 9 additions & 1 deletion tests/manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe('integration: manifest', () => {
it('generates correct manifest object', async () => {
const { stdout } = await run('manifest', { cwd })
const res = JSON.parse(stdout)
console.log(res)
expect(res.path).to.equal('')
expect(res.draft).to.be.false()
expect(res.title).to.equal('Mock')
Expand All @@ -22,5 +21,14 @@ describe('integration: manifest', () => {
expect(res.items[0].input).to.equal(syspath.resolve(__dirname, 'mock/foo/index.md'))
expect(res.items[0].outputDir).to.equal('.gitdocs_build/foo/')
expect(res.items[0].items).to.have.length(3)
expect(res.items[4].path).to.equal('external.md')
expect(res.items[4].title).to.equal('GitDocs')
expect(res.items[4].url).to.equal('/gitdocs/')
expect(res.items[4].input).to.match(/\/@repos\/gitdocs/)
expect(res.items[4].outputDir).to.equal('.gitdocs_build/gitdocs/')
expect(res.items[4].items).to.have.length(1)
expect(res.items[4].items[0].path).to.equal('externals.md')
expect(res.items[4].items[0].title).to.equal('Externals')
expect(res.items[4].items[0].url).to.equal('/gitdocs/externals/')
})
})
7 changes: 7 additions & 0 deletions tests/mock/external.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: https://github.com/timberio/gitdocs
source_type: git
source_branch: master
title: GitDocs
url: gitdocs
---
1 change: 1 addition & 0 deletions tests/mock/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ items:
- garply
- xyzzy
- thud.md
- external.md
- path: qux
title: The Quux
items_prepend:
Expand Down