forked from ProjectEvergreen/greenwood
-
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.
Dynamic Sitemap Copy Plugin (ProjectEvergreen#1232)
- Loading branch information
Showing
8 changed files
with
168 additions
and
0 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,24 @@ | ||
# @greenwood/plugin-dynamic-sitemap | ||
|
||
## Overview | ||
Spiders love to spider. To show our love to all the spiders out there, this plugin reads | ||
the graph and renders a sitemap.xml. Currently, it handles up to 10000 content entries, warning | ||
after 9000 content entries. | ||
|
||
## Usage | ||
Add this plugin to your _greenwood.config.js_ and spread the `export`. | ||
|
||
```javascript | ||
import { greenwoodPluginDynamicExport } from '@greenwood/plugin-dynamic-sitemap'; | ||
|
||
export default { | ||
... | ||
|
||
plugins: [ | ||
greenwoodPluginDynamicExport({ | ||
"base_url": "https://example.com" | ||
}) | ||
] | ||
} | ||
``` | ||
|
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,37 @@ | ||
import { checkResourceExists } from "@greenwood/cli/src/lib/resource-utils.js"; | ||
import fs from 'fs/promises'; | ||
|
||
|
||
|
||
|
||
|
||
const greenwoodPluginDynamicExport = (options = {}) => [{ | ||
type: 'copy', | ||
name: 'plugin-dynamic-sitemap', | ||
provider: async (compilation) => { | ||
|
||
const { base_url} = options; | ||
const { outputDir } = compilation.context; | ||
|
||
let sitemapXML = '<?xml version="1.0" encoding="UTF-8"?>\n'; | ||
sitemapXML += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' | ||
|
||
compilation.graph.forEach(page => { | ||
sitemapXML += `<url><loc>${base_url}${page.outputPath}</loc></url>\n` | ||
}); | ||
|
||
sitemapXML += '</urlset>' | ||
|
||
const sitemapUrl = new URL('./sitemap.xml', outputDir); | ||
await fs.writeFile(sitemapUrl, sitemapXML); | ||
|
||
return { | ||
from: sitemapUrl, | ||
to: new URL('./sitemap.xml', outputDir) | ||
}; | ||
} | ||
}]; | ||
|
||
export { greenwoodPluginDynamicExport }; | ||
|
||
|
79 changes: 79 additions & 0 deletions
79
packages/plugin-dynamic-sitemap/test/copy.default.dynamic-sitemap.spec.js
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,79 @@ | ||
/* | ||
* Use Case | ||
* Run Greenwood build command with no config and this plugin. | ||
* | ||
* User Result | ||
* Should generate a bare bones Greenwood build with correctly built sitemap.xml | ||
* | ||
* User Command | ||
* greenwood build | ||
* | ||
* User Config | ||
* import { greenwoodPluginDynamicExport } from '@greenwood/plugin-dynamic-sitemap'; | ||
* | ||
* { | ||
* plugins: [{ | ||
* greenwoodPluginDynamicExport({ | ||
* "base_url": "https://example.com" | ||
* }) | ||
* }] | ||
* | ||
* } | ||
* | ||
* User Workspace | ||
* src/ | ||
* templates/ | ||
* artist.html | ||
* pages/ | ||
* index.md | ||
* about.md | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import chai from 'chai'; | ||
import path from 'path'; | ||
import { runSmokeTest } from '../../../test/smoke-test.js'; | ||
import { getSetupFiles, getOutputTeardownFiles } from '../../../test/utils.js'; | ||
import { Runner } from 'gallinago'; | ||
import { fileURLToPath, URL } from 'url'; | ||
|
||
const expect = chai.expect; | ||
|
||
describe('Build Greenwood With Dynamic Sitemap Plugin: ', function() { | ||
const LABEL = 'Using Dynamic Sitemap feature'; | ||
const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); | ||
const outputPath = fileURLToPath(new URL('.', import.meta.url)); | ||
let runner; | ||
|
||
before(function() { | ||
this.context = { | ||
publicDir: path.join(outputPath, 'public') | ||
}; | ||
runner = new Runner(); | ||
}); | ||
|
||
describe(LABEL, function() { | ||
|
||
before(function() { | ||
runner.setup(outputPath, getSetupFiles(outputPath)); | ||
runner.runCommand(cliPath, 'build'); | ||
}); | ||
|
||
runSmokeTest(['public', 'index'], LABEL); | ||
|
||
describe('Sitemap.xml should exist and be well formed', function() { | ||
let robots; | ||
|
||
|
||
it('should have one sitemaps file in the output directory', function() { | ||
const sitemapXML = fs.readFileSync(path.join(this.context.publicDir, './sitemap.xml')); | ||
console.log(sitemapXML); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
after(function() { | ||
runner.teardown(getOutputTeardownFiles(outputPath)); | ||
}); | ||
}); |
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,10 @@ | ||
import { greenwoodPluginDynamicExport } from '../src/index.js'; | ||
|
||
console.log(greenwoodPluginDynamicExport); | ||
export default { | ||
plugins: [ | ||
...greenwoodPluginDynamicExport({ | ||
"base_url": "https://example.com" | ||
}) | ||
] | ||
}; |
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,4 @@ | ||
{ | ||
"version": "1.0.0", | ||
"type": "module" | ||
} |
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,3 @@ | ||
# About Us | ||
|
||
Lorem ipsum. |
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,3 @@ | ||
## Home Page | ||
|
||
Welcome! |
8 changes: 8 additions & 0 deletions
8
packages/plugin-dynamic-sitemap/test/src/templates/artist.html
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,8 @@ | ||
<html> | ||
|
||
<body> | ||
<h1>Welcome to the artist page.</h1> | ||
<content-outlet></content-outlet> | ||
</body> | ||
|
||
</html> |