This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
prepareSitemap
Batfish helper to improve sitemap (#427)
* Create prepareSitemap Batfish helper * Update CHANGELOG.md * Clean up, add `docsPath`, and `outputDirectory` * Exclude `splitPage` from sitemap * Assert sitemap * Extract urls * Sort sitemap * Remove snapshot * Update sitemap.test.js * Update batfish-helpers.md * Update batfish-helpers.md * Copyedit * Update package-lock.json
- Loading branch information
Katy DeCorah
authored
Apr 14, 2021
1 parent
a6b95c0
commit de93e03
Showing
9 changed files
with
220 additions
and
4 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
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
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
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,94 @@ | ||
const prepareSitemap = require('../sitemap'); | ||
const { pages } = require('./fixtures/data.json'); | ||
const { existsSync, readFileSync } = require('fs'); | ||
|
||
const siteMap = './docs/_site/sitemap.xml'; | ||
const siteBasePath = '/dr-ui'; | ||
|
||
describe('prepareSitemap', () => { | ||
it('works', () => { | ||
const ignoreFile = prepareSitemap({ pages, siteBasePath }).map((f) => | ||
f.replace(process.cwd(), '') | ||
); | ||
expect(ignoreFile).toEqual([ | ||
'/_site/guides/split-pages/sections/how-to/', | ||
'/_site/guides/split-pages/sections/intro/', | ||
'/_site/guides/split-pages/sections/limitations/', | ||
'/_site/guides/split-pages/sections/tag-debugger/', | ||
'/_site/index.html' | ||
]); | ||
}); | ||
|
||
it('advanced, exclude demos generated by webpack loader', () => { | ||
const ignoreFile = [ | ||
...prepareSitemap({ pages, siteBasePath }), | ||
`${process.cwd()}/_site/assets/` | ||
].map((f) => f.replace(process.cwd(), '')); | ||
expect(ignoreFile).toEqual([ | ||
'/_site/guides/split-pages/sections/how-to/', | ||
'/_site/guides/split-pages/sections/intro/', | ||
'/_site/guides/split-pages/sections/limitations/', | ||
'/_site/guides/split-pages/sections/tag-debugger/', | ||
'/_site/index.html', | ||
'/_site/assets/' | ||
]); | ||
}); | ||
|
||
it('with docsPath', () => { | ||
const ignoreFile = prepareSitemap({ | ||
pages, | ||
siteBasePath, | ||
docsPath: 'docs' | ||
}).map((f) => f.replace(process.cwd(), '')); | ||
expect(ignoreFile).toEqual([ | ||
'/docs/_site/guides/split-pages/sections/how-to/', | ||
'/docs/_site/guides/split-pages/sections/intro/', | ||
'/docs/_site/guides/split-pages/sections/limitations/', | ||
'/docs/_site/guides/split-pages/sections/tag-debugger/', | ||
'/docs/_site/index.html' | ||
]); | ||
}); | ||
|
||
it('with outputDirectory', () => { | ||
const ignoreFile = prepareSitemap({ | ||
pages, | ||
siteBasePath, | ||
outputDirectory: '_batfish_site' | ||
}).map((f) => f.replace(process.cwd(), '')); | ||
expect(ignoreFile).toEqual([ | ||
'/_batfish_site/guides/split-pages/sections/how-to/', | ||
'/_batfish_site/guides/split-pages/sections/intro/', | ||
'/_batfish_site/guides/split-pages/sections/limitations/', | ||
'/_batfish_site/guides/split-pages/sections/tag-debugger/', | ||
'/_batfish_site/index.html' | ||
]); | ||
}); | ||
|
||
// travis is configured to run this test file again after build | ||
// this test asserts the URLs in the sitemap | ||
if (existsSync(siteMap)) { | ||
it('sitemap', () => { | ||
const contents = readFileSync(siteMap, 'utf-8') | ||
.match( | ||
/(?<=(<loc>))(.*)+?(?=(<\/loc>))/gm // eslint-disable-line | ||
) | ||
.sort(); | ||
expect(contents).toEqual([ | ||
'https://mapbox.github.io/dr-ui/changelog', | ||
'https://mapbox.github.io/dr-ui/components', | ||
'https://mapbox.github.io/dr-ui/examples', | ||
'https://mapbox.github.io/dr-ui/examples/example-1', | ||
'https://mapbox.github.io/dr-ui/examples/example-2', | ||
'https://mapbox.github.io/dr-ui/examples/example-3', | ||
'https://mapbox.github.io/dr-ui/examples/example-4', | ||
'https://mapbox.github.io/dr-ui/examples/example-5', | ||
'https://mapbox.github.io/dr-ui/guides', | ||
'https://mapbox.github.io/dr-ui/guides/a11y', | ||
'https://mapbox.github.io/dr-ui/guides/batfish-helpers', | ||
'https://mapbox.github.io/dr-ui/guides/multi-structured', | ||
'https://mapbox.github.io/dr-ui/guides/page-layout', | ||
'https://mapbox.github.io/dr-ui/guides/split-pages' | ||
]); | ||
}); | ||
} | ||
}); |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
const { buildNavigation } = require('./navigation'); | ||
const { buildFilters } = require('./filters'); | ||
const { buildSplitPages } = require('./split-pages'); | ||
const prepareSitemap = require('./sitemap'); | ||
|
||
module.exports = { | ||
buildNavigation, | ||
buildFilters, | ||
buildSplitPages | ||
buildSplitPages, | ||
prepareSitemap | ||
}; |
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,32 @@ | ||
const { join } = require('path'); | ||
|
||
function prepareSitemap({ | ||
pages, | ||
siteBasePath, | ||
docsPath = '', | ||
outputDirectory = '_site' | ||
}) { | ||
// find pages with `hideFromSearchEngines: true` in frontMatter or `splitPage: true` | ||
// we do not want these pages to appear in the sitemap | ||
return pages | ||
.filter( | ||
({ frontMatter }) => | ||
frontMatter.hideFromSearchEngines || frontMatter.splitPage | ||
) | ||
.map(({ path }) => { | ||
// fix root index path to return full path | ||
if (path === `${siteBasePath}/`) { | ||
return path.replace( | ||
join(siteBasePath, '/'), | ||
join(process.cwd(), docsPath, outputDirectory, 'index.html') | ||
); | ||
} else { | ||
return path.replace( | ||
siteBasePath, | ||
join(process.cwd(), docsPath, outputDirectory) | ||
); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = prepareSitemap; |