Skip to content

Commit

Permalink
feat: add option to generate other sitemaps links (#90)
Browse files Browse the repository at this point in the history
* feat: add option to generate other sitemaps links

* feat: allow urls
  • Loading branch information
jbaubree committed Nov 17, 2023
1 parent 56875a2 commit e98bc94
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ generateSitemap({
})
```

### externalSitemaps

- **Type:** `string[]`
- **Default:** `[]`

Array of strings with other sitemaps paths or urls.
```js
generateSitemap({
externalSitemaps: ['sitemap_1', 'sitemap_2', 'subpath/sitemap_3', 'https://site.com/sitemap.xml']
})
```

### base path

- **Type:** `string`
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function generateSitemap(options: UserOptions = {}) {
// robots.txt
if (resolvedOptions.generateRobotsTxt) {
const robotRules = getRules(resolvedOptions.robots)
const robotContent = getContent(robotRules, resolvedOptions.hostname)
const robotContent = getContent(robotRules, resolvedOptions.hostname, resolvedOptions.externalSitemaps)
writeFileSync(getResolvedPath('robots.txt', resolvedOptions), robotContent)
}

Expand Down
1 change: 1 addition & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const defaultOptions = {
hostname: 'http://localhost/',
dynamicRoutes: [],
exclude: [],
externalSitemaps: [],
basePath: '',
outDir: 'dist',
extensions: 'html',
Expand Down
12 changes: 8 additions & 4 deletions src/robots.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ensureSuffix } from '@antfu/utils'
import { ensurePrefix, ensureSuffix } from '@antfu/utils'

Check failure on line 1 in src/robots.ts

View workflow job for this annotation

GitHub Actions / Lint: node-16, ubuntu-latest

'ensureSuffix' is defined but never used

import type { RobotOption } from './types'
import { removeMaybeSuffix } from './utils'

enum RobotCorrespondences {
userAgent = 'User-agent',
Expand Down Expand Up @@ -39,11 +40,14 @@ export function getRules(options: RobotOption[]) {
return rules
}

export function getContent(rules: RobotRuleInterface[], hostname: string) {
export function getContent(rules: RobotRuleInterface[], hostname: string, externalSitemaps: string[]) {
return rules.map(rule => `${rule.key}: ${String(rule.value).trim()}`).join('\n')
.concat(`\n\nSitemap: ${getFinalSitemapPath(hostname)}`)
.concat(externalSitemaps.map(s => `\nSitemap: ${
s.startsWith('http') ? s : getFinalSitemapPath(hostname, s)
}`).join(''))
}

export function getFinalSitemapPath(hostname: string) {
return `${ensureSuffix('/', hostname)}sitemap.xml`
export function getFinalSitemapPath(hostname: string, file = '/sitemap.xml') {
return `${removeMaybeSuffix('/', hostname)}${ensurePrefix('/', file)}`
}
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ interface Options {
* @default []
*/
exclude: string[]
/**
* Other sitemaps paths
* Example: ['sitemap_1.xml', 'path/sitemap_2.xml', 'https://site.com/sitemap.xml']
* @default []
*/
externalSitemaps: string[]
/**
* String with base path.
* Example: '/mysubpath'
Expand Down
5 changes: 5 additions & 0 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Options', () => {
"dynamicRoutes": [],
"exclude": [],
"extensions": "html",
"externalSitemaps": [],
"generateRobotsTxt": true,
"hostname": "http://localhost/",
"lastmod": Any<Date>,
Expand Down Expand Up @@ -43,6 +44,7 @@ describe('Options', () => {
"/route2/sub-route",
],
"extensions": "html",
"externalSitemaps": [],
"generateRobotsTxt": true,
"hostname": "http://localhost/",
"lastmod": Any<Date>,
Expand Down Expand Up @@ -74,6 +76,7 @@ describe('Options', () => {
"html",
"md",
],
"externalSitemaps": [],
"generateRobotsTxt": true,
"hostname": "http://localhost/",
"lastmod": Any<Date>,
Expand Down Expand Up @@ -113,6 +116,7 @@ describe('Options', () => {
"dynamicRoutes": [],
"exclude": [],
"extensions": "html",
"externalSitemaps": [],
"generateRobotsTxt": true,
"hostname": "http://localhost/",
"lastmod": Any<Date>,
Expand Down Expand Up @@ -155,6 +159,7 @@ describe('Options', () => {
"dynamicRoutes": [],
"exclude": [],
"extensions": "html",
"externalSitemaps": [],
"generateRobotsTxt": false,
"hostname": "http://localhost/",
"lastmod": Any<Date>,
Expand Down
9 changes: 6 additions & 3 deletions test/robots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ describe('Robots', () => {
})

test('Get content', async () => {
expect(getContent([], 'http//localhost/')).toEqual('\n\nSitemap: http//localhost/sitemap.xml')
expect(getContent(TEST_RULES_1, 'http//localhost/')).toEqual('User-agent: *\nAllow: /\n\nSitemap: http//localhost/sitemap.xml')
expect(getContent(TEST_RULES_2, 'http//localhost/')).toEqual('User-agent: *\nAllow: /\nUser-agent: Googlebot\nAllow: /admin\nDisallow: /disallow\nDisallow: /test\nCrawl-delay: 10\n\nSitemap: http//localhost/sitemap.xml')
expect(getContent([], 'http//localhost/', [])).toEqual('\n\nSitemap: http//localhost/sitemap.xml')
expect(getContent(TEST_RULES_1, 'http//localhost/', [])).toEqual('User-agent: *\nAllow: /\n\nSitemap: http//localhost/sitemap.xml')
expect(getContent(TEST_RULES_2, 'http//localhost/', [])).toEqual('User-agent: *\nAllow: /\nUser-agent: Googlebot\nAllow: /admin\nDisallow: /disallow\nDisallow: /test\nCrawl-delay: 10\n\nSitemap: http//localhost/sitemap.xml')
expect(getContent([], 'http//localhost/', ['sitemap_1.xml', 'subpath/sitemap_2.xml'])).toEqual('\n\nSitemap: http//localhost/sitemap.xml\nSitemap: http//localhost/sitemap_1.xml\nSitemap: http//localhost/subpath/sitemap_2.xml')
expect(getContent([], 'http//localhost/', ['/sitemap_1.xml', '/subpath/sitemap_2.xml'])).toEqual('\n\nSitemap: http//localhost/sitemap.xml\nSitemap: http//localhost/sitemap_1.xml\nSitemap: http//localhost/subpath/sitemap_2.xml')
expect(getContent([], 'http//localhost/', ['/sitemap_1.xml', 'https://test.com/subpath/sitemap_2.xml'])).toEqual('\n\nSitemap: http//localhost/sitemap.xml\nSitemap: http//localhost/sitemap_1.xml\nSitemap: https://test.com/subpath/sitemap_2.xml')
})

test('Get final sitemap path', async () => {
Expand Down

0 comments on commit e98bc94

Please sign in to comment.