From c9b6e634fb5b2b5e6f715d5d0064ce177c9af670 Mon Sep 17 00:00:00 2001 From: Jason Maurer Date: Wed, 30 May 2018 11:37:07 -0400 Subject: [PATCH 1/3] hydrate the index of a source, add integration test for it --- src/core/filesystem.js | 1 + src/core/hydrate.js | 58 ++++++++++++++++++++---------------------- tests/manifest.test.js | 10 +++++++- tests/mock/external.md | 7 +++++ tests/mock/readme.md | 1 + 5 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 tests/mock/external.md 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 3134c2e..f9f4cbc 100644 --- a/src/core/hydrate.js +++ b/src/core/hydrate.js @@ -62,7 +62,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, @@ -95,20 +95,31 @@ 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 - - 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 + } } // get sub items from the front matter @@ -117,29 +128,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 @@ -147,12 +140,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..5922c7d 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(/^\/var\/folders.*\/@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: From 1a6f47c750f9a404e9ce8630baa1d4ddc05015f1 Mon Sep 17 00:00:00 2001 From: Jason Maurer Date: Wed, 30 May 2018 11:42:36 -0400 Subject: [PATCH 2/3] lint rule override --- package.json | 3 +++ src/core/hydrate.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) 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/hydrate.js b/src/core/hydrate.js index 7b024b1..921bc2f 100644 --- a/src/core/hydrate.js +++ b/src/core/hydrate.js @@ -121,7 +121,7 @@ async function hydrateTree (tree, config, onRegenerate) { } 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) From c5334e981b7bb1f38f5d3ea3751a6a568263e926 Mon Sep 17 00:00:00 2001 From: Jason Maurer Date: Wed, 30 May 2018 11:46:09 -0400 Subject: [PATCH 3/3] tmp dir test fix --- tests/manifest.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/manifest.test.js b/tests/manifest.test.js index 5922c7d..1b11fcf 100644 --- a/tests/manifest.test.js +++ b/tests/manifest.test.js @@ -24,7 +24,7 @@ describe('integration: manifest', () => { 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(/^\/var\/folders.*\/@repos\/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')