-
Notifications
You must be signed in to change notification settings - Fork 21
feat: create sitemap generator #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||||||||||||||||||||||||||
| import { writeFile } from 'node:fs/promises'; | ||||||||||||||||||||||||||||||||||
| import { join } from 'node:path'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import { BASE_URL } from '../../constants.mjs'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * This generator generates a sitemap.xml file for search engine optimization | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @typedef {Array<ApiDocMetadataEntry>} Input | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @type {GeneratorMetadata<Input, string>} | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| export default { | ||||||||||||||||||||||||||||||||||
| name: 'sitemap', | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| version: '1.0.0', | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| description: 'Generates a sitemap.xml file for search engine optimization', | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| dependsOn: 'metadata', | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Generates a sitemap.xml file | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @param {Input} entries | ||||||||||||||||||||||||||||||||||
| * @param {Partial<GeneratorOptions>} options | ||||||||||||||||||||||||||||||||||
| * @returns {Promise<string>} | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| async generate(entries, { output }) { | ||||||||||||||||||||||||||||||||||
| const apiPages = entries | ||||||||||||||||||||||||||||||||||
| .filter(entry => entry.heading.depth === 1) | ||||||||||||||||||||||||||||||||||
| .map(entry => { | ||||||||||||||||||||||||||||||||||
| const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/'); | ||||||||||||||||||||||||||||||||||
| const url = new URL(path, BASE_URL).href; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||
| loc: url, | ||||||||||||||||||||||||||||||||||
| lastmod: new Date().toISOString().split('T')[0], | ||||||||||||||||||||||||||||||||||
| changefreq: 'weekly', | ||||||||||||||||||||||||||||||||||
| priority: '0.8', | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const mainPages = [ | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| loc: new URL('/docs/latest/api/', BASE_URL).href, | ||||||||||||||||||||||||||||||||||
| lastmod: new Date().toISOString().split('T')[0], | ||||||||||||||||||||||||||||||||||
| changefreq: 'daily', | ||||||||||||||||||||||||||||||||||
| priority: '1.0', | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const allPages = [...mainPages, ...apiPages]; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can optimize this by doing something like
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const sitemap = `<?xml version="1.0" encoding="UTF-8"?> | ||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make an actual file to store this template? And use handlebars or whatever we have already on doc-kit for replacement?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(What we already have is |
||||||||||||||||||||||||||||||||||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||||||||||||||||||||||||||||||||||
| ${allPages | ||||||||||||||||||||||||||||||||||
| .map( | ||||||||||||||||||||||||||||||||||
| page => ` <url> | ||||||||||||||||||||||||||||||||||
| <loc>${page.loc}</loc> | ||||||||||||||||||||||||||||||||||
| <lastmod>${page.lastmod}</lastmod> | ||||||||||||||||||||||||||||||||||
| <changefreq>${page.changefreq}</changefreq> | ||||||||||||||||||||||||||||||||||
| <priority>${page.priority}</priority> | ||||||||||||||||||||||||||||||||||
| </url>` | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| .join('\n')} | ||||||||||||||||||||||||||||||||||
| </urlset>`; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+67
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is a draft, but I was wondering if:
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (output) { | ||||||||||||||||||||||||||||||||||
| await writeFile(join(output, 'sitemap.xml'), sitemap, 'utf-8'); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return sitemap; | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to create this Date for each entry?
Can't we initialize it once, and populate each
lastmodfrom it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I was thinking of somehow getting the last modification date from the git repository
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that could also work, neat!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ovflowd What do you think?