-
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,81 @@ | ||||||||||
| import { readFile, writeFile } from 'node:fs/promises'; | ||||||||||
| import { join } from 'node:path'; | ||||||||||
|
|
||||||||||
| import dedent from 'dedent'; | ||||||||||
|
|
||||||||||
| 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 lastmod = new Date().toISOString().split('T')[0]; | ||||||||||
|
|
||||||||||
| const apiPages = entries | ||||||||||
| .filter(entry => entry.heading.depth === 1) | ||||||||||
| .map(entry => { | ||||||||||
| const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/'); | ||||||||||
|
||||||||||
| const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/'); | |
| const path = entry.api_doc_source | |
| .replace(/^doc\//, '/docs/latest/') | |
| .replace(/\.md$/, '.html'); |
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.
| const url = new URL(path, BASE_URL).href; | |
| const { href: url } = new URL(path, BASE_URL); |
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.
nit: prefer object deconstruction.
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.
nit: all of this could be inlined (all in one line object creation instead of multi line)
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.
nit: use template files for this and then do simple key->value substitution. Or use proper rss/feed libraries OR xml libraries.
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.
Or use proper rss/feed libraries OR xml libraries.
You can probably use hast (but I'm also fine with it this way), seeing as the majority of it is a template
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| __URLSET__ | ||
| </urlset> |
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.
nit: extract this anonymous function to a dedicated function.