-
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.
Merge pull request #148 from thegalactiks/improve-sitemap
feat: improve sitemap generation
- Loading branch information
Showing
17 changed files
with
349 additions
and
86 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
/var/data/workspace/thegalactiks/explorer/examples/contentlayer/assets/.gitkeep |
4 changes: 2 additions & 2 deletions
4
...entlayer/content/websites/example.com.mdx → ...tlayer/content/websites/galactiks.com.mdx
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,7 +1,7 @@ | ||
--- | ||
name: Website name | ||
description: Website description. | ||
identifier: example.com | ||
url: https://example.com/ | ||
identifier: galactiks.com | ||
url: https://www.galactiks.com/ | ||
dateCreated: 1970-01-01 | ||
--- |
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,112 @@ | ||
import path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import type { AstroConfig, AstroIntegration } from 'astro'; | ||
import { getDefaultLanguage } from '@galactiks/config'; | ||
import { type Content, getPageByURL } from '@galactiks/explorer'; | ||
import { generateSitemaps } from '@galactiks/sitemap'; | ||
|
||
const sitemapIndexOutput = 'sitemap-index.xml'; | ||
|
||
const createPlugin = (): AstroIntegration => { | ||
let config: AstroConfig; | ||
|
||
return { | ||
name: '@galactiks/astro-integration/sitemap', | ||
|
||
hooks: { | ||
'astro:config:done': async ({ config: cfg }) => { | ||
config = cfg; | ||
}, | ||
|
||
'astro:build:done': async ({ dir, routes, pages, logger }) => { | ||
if (!config.site) { | ||
logger.warn( | ||
'The Sitemap integration requires the `site` astro.config option. Skipping.' | ||
); | ||
return; | ||
} | ||
|
||
let finalSiteUrl: URL; | ||
if (config.site) { | ||
finalSiteUrl = new URL(config.base, config.site); | ||
} else { | ||
console.warn( | ||
'The Sitemap integration requires the `site` astro.config option. Skipping.' | ||
); | ||
return; | ||
} | ||
|
||
let pageUrls = pages | ||
.map((p) => { | ||
if (p.pathname !== '' && !finalSiteUrl.pathname.endsWith('/')) | ||
finalSiteUrl.pathname += '/'; | ||
if (p.pathname.startsWith('/')) p.pathname = p.pathname.slice(1); | ||
const fullPath = finalSiteUrl.pathname + p.pathname; | ||
return new URL(fullPath, finalSiteUrl).href; | ||
}); | ||
|
||
const routeUrls = routes.reduce<string[]>((urls, r) => { | ||
if (r.type !== 'page') return urls; | ||
|
||
/** | ||
* Dynamic URLs have entries with `undefined` pathnames | ||
*/ | ||
if (r.pathname) { | ||
// `finalSiteUrl` may end with a trailing slash | ||
// or not because of base paths. | ||
let fullPath = finalSiteUrl.pathname; | ||
if (fullPath.endsWith('/')) fullPath += r.generate(r.pathname).substring(1); | ||
else fullPath += r.generate(r.pathname); | ||
|
||
const newUrl = new URL(fullPath, finalSiteUrl).href; | ||
|
||
if (config.trailingSlash === 'never') { | ||
urls.push(newUrl); | ||
} else if (config.build.format === 'directory' && !newUrl.endsWith('/')) { | ||
urls.push(newUrl + '/'); | ||
} else { | ||
urls.push(newUrl); | ||
} | ||
} | ||
|
||
return urls; | ||
}, []); | ||
|
||
pageUrls = Array.from(new Set([...pageUrls, ...routeUrls])); | ||
logger.info(`Generating sitemap for ${pageUrls.length} pages`); | ||
logger.info(pageUrls.join('\n')); | ||
|
||
const contentPages = ( | ||
await Promise.all( | ||
pageUrls.map(getPageByURL) | ||
) | ||
).filter((page) => page !== undefined) as Content[]; | ||
if (contentPages.length === 0) { | ||
logger.warn(`No pages found!\n\`${sitemapIndexOutput}\` not created.`); | ||
return; | ||
} | ||
|
||
const destDir = fileURLToPath(dir); | ||
await generateSitemaps({ | ||
destinationDir: destDir, | ||
hostname: finalSiteUrl.href, | ||
pages: contentPages, | ||
defaultLanguage: getDefaultLanguage(), | ||
publication: { | ||
name: 'Galactiks', | ||
}, | ||
}); | ||
// await simpleSitemapAndIndex({ | ||
// hostname: finalSiteUrl.href, | ||
// destinationDir: destDir, | ||
// sourceData: urlData, | ||
// limit: entryLimit, | ||
// gzip: false, | ||
// }); | ||
logger.info(`\`${sitemapIndexOutput}\` created at \`${path.relative(process.cwd(), destDir)}\``); | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default createPlugin; |
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,29 +1,15 @@ | ||
import type { AstroIntegration } from 'astro'; | ||
import partytown from '@astrojs/partytown'; | ||
import react from '@astrojs/react'; | ||
import sitemap from '@astrojs/sitemap'; | ||
import { getDefaultLanguage, getLanguages } from '@galactiks/config'; | ||
import robotsTxt from 'astro-robots-txt'; | ||
|
||
import { sitemapSerialize } from './sitemap.js'; | ||
import sitemap from './plugins/sitemap.js'; | ||
|
||
export const integrationsPreset = (): AstroIntegration[] => { | ||
const defaultLanguage = getDefaultLanguage(); | ||
|
||
return [ | ||
react(), | ||
partytown(), | ||
sitemap({ | ||
i18n: defaultLanguage | ||
? { | ||
defaultLocale: defaultLanguage, | ||
locales: Object.fromEntries( | ||
getLanguages().map((lang) => [lang, lang]) | ||
), | ||
} | ||
: undefined, | ||
serialize: sitemapSerialize(), | ||
}), | ||
sitemap(), | ||
robotsTxt(), | ||
]; | ||
}; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './content/index.js'; | ||
|
||
export { Content } from './types/index.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,28 @@ | ||
# @galactiks/config | ||
|
||
This package allows reading [Galactiks](https://www.galactiks.com) configurations files and get config during website generation. | ||
|
||
## Installation | ||
|
||
Install the `@galactiks/config` package using your preferred package manager: | ||
|
||
```sh | ||
# npm | ||
npm i @galactiks/config | ||
|
||
# yarn | ||
yarn add @galactiks/config | ||
|
||
# pnpm | ||
pnpm i @galactiks/config | ||
``` | ||
|
||
If you run into any issues, [feel free to report them to us on GitHub](https://github.com/thegalactiks/explorer/issues). | ||
|
||
## Getting started | ||
|
||
Coming soon | ||
|
||
## License | ||
|
||
MIT © [Galactiks](https://www.galactiks.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,43 @@ | ||
{ | ||
"name": "@galactiks/sitemap", | ||
"version": "0.0.1", | ||
"description": "A simple sitemap generator for Galactiks", | ||
"author": "thegalactiks", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"bugs": "https://github.com/thegalactiks/explorer/issues", | ||
"homepage": "https://www.galactiks.com", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/thegalactiks/explorer.git", | ||
"directory": "packages/sitemap" | ||
}, | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc -p ./tsconfig.json", | ||
"build:ci": "tsc -p ./tsconfig.json" | ||
}, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": "./dist/index.js" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"galactiks", | ||
"sitemap", | ||
"seo" | ||
], | ||
"dependencies": { | ||
"date-fns": "3.3.1", | ||
"sitemap": "7.1.1" | ||
}, | ||
"devDependencies": { | ||
"@galactiks/explorer": "workspace:^" | ||
} | ||
} |
Oops, something went wrong.