diff --git a/package.json b/package.json index 0b84f41..5f12c72 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,9 @@ "it": true, "beforeEach": true, "afterEach": true + }, + "rules": { + "react/no-did-mount-set-state": 0 } }, "pkg": { diff --git a/src/core/filesystem.js b/src/core/filesystem.js index a6f0a03..a974e20 100644 --- a/src/core/filesystem.js +++ b/src/core/filesystem.js @@ -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), } diff --git a/src/core/hydrate.js b/src/core/hydrate.js index 6104766..921bc2f 100644 --- a/src/core/hydrate.js +++ b/src/core/hydrate.js @@ -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, @@ -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 @@ -123,29 +134,11 @@ 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 @@ -153,12 +146,17 @@ async function hydrateTree (tree, config, onRegenerate) { .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 } diff --git a/tests/manifest.test.js b/tests/manifest.test.js index 9c9f390..1b11fcf 100644 --- a/tests/manifest.test.js +++ b/tests/manifest.test.js @@ -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') @@ -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/') }) }) diff --git a/tests/mock/external.md b/tests/mock/external.md new file mode 100644 index 0000000..59a4182 --- /dev/null +++ b/tests/mock/external.md @@ -0,0 +1,7 @@ +--- +source: https://github.com/timberio/gitdocs +source_type: git +source_branch: master +title: GitDocs +url: gitdocs +--- diff --git a/tests/mock/readme.md b/tests/mock/readme.md index 4e4c1dc..994163d 100644 --- a/tests/mock/readme.md +++ b/tests/mock/readme.md @@ -4,6 +4,7 @@ items: - garply - xyzzy - thud.md + - external.md - path: qux title: The Quux items_prepend: