-
Notifications
You must be signed in to change notification settings - Fork 23
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
Open
araujogui
wants to merge
5
commits into
main
Choose a base branch
from
feat/sitemap-xml
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| import { readFile, writeFile } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| import dedent from 'dedent'; | ||
|
|
||
| import { BASE_URL } from '../../constants.mjs'; | ||
| import { createPageSitemapEntry } from './utils/createPageSitemapEntry.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 => createPageSitemapEntry(entry, lastmod)); | ||
|
|
||
| /** | ||
| * @typedef {import('./types').SitemapEntry} | ||
| */ | ||
| const mainPage = { | ||
| loc: new URL('/docs/latest/api/', BASE_URL).href, | ||
| lastmod, | ||
| changefreq: 'daily', | ||
| priority: '1.0', | ||
| }; | ||
|
|
||
| apiPages.push(mainPage); | ||
|
|
||
| const template = await readFile( | ||
| join(import.meta.dirname, 'template.xml'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| const urlset = apiPages | ||
| .map( | ||
| page => dedent` | ||
| <url> | ||
| <loc>${page.loc}</loc> | ||
| <lastmod>${page.lastmod}</lastmod> | ||
| <changefreq>${page.changefreq}</changefreq> | ||
| <priority>${page.priority}</priority> | ||
| </url> | ||
| ` | ||
| ) | ||
| .join('\n'); | ||
|
|
||
| const sitemap = template.replace('__URLSET__', urlset); | ||
|
|
||
| if (output) { | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await writeFile(join(output, 'sitemap.xml'), sitemap, 'utf-8'); | ||
| } | ||
|
|
||
| return sitemap; | ||
| }, | ||
| }; | ||
This file contains hidden or 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 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
| __URLSET__ | ||
| </urlset> |
This file contains hidden or 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,13 @@ | ||
| export interface SitemapEntry { | ||
| loc: string; | ||
| lastmod?: string; | ||
| changefreq?: | ||
| | 'always' | ||
| | 'hourly' | ||
| | 'daily' | ||
| | 'weekly' | ||
| | 'monthly' | ||
| | 'yearly' | ||
| | 'never'; | ||
| priority?: string; | ||
| } |
This file contains hidden or 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,14 @@ | ||
| import { buildApiDocURL } from '../../../utils/url.mjs'; | ||
|
|
||
| /** | ||
| * Builds an API doc sitemap url. | ||
| * | ||
| * @param {ApiDocMetadataEntry} entry | ||
| * @param {string} lastmod | ||
| * @returns {import('../types').SitemapEntry} | ||
| */ | ||
| export const createPageSitemapEntry = (entry, lastmod) => { | ||
| const { href } = buildApiDocURL(entry, true); | ||
|
|
||
| return { loc: href, lastmod, changefreq: 'weekly', priority: '0.8' }; | ||
| }; |
This file contains hidden or 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,32 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
|
|
||
| import { buildApiDocURL } from '../url.mjs'; | ||
|
|
||
| const BASE = 'https://nodejs.org/'; | ||
|
|
||
| describe('buildApiDocURL', () => { | ||
| it('builds markdown doc URLs from doc/ sources', () => { | ||
| const entry = { api_doc_source: 'doc/api/fs.md' }; | ||
|
|
||
| const result = buildApiDocURL(entry); | ||
|
|
||
| assert.equal(result.href, `${BASE}docs/latest/api/fs.md`); | ||
| }); | ||
|
|
||
| it('builds html doc URLs when requested', () => { | ||
| const entry = { api_doc_source: 'doc/api/path.md' }; | ||
|
|
||
| const result = buildApiDocURL(entry, true); | ||
|
|
||
| assert.equal(result.href, `${BASE}docs/latest/api/path.html`); | ||
| }); | ||
|
|
||
| it('leaves non doc/ sources untouched', () => { | ||
| const entry = { api_doc_source: 'api/crypto.md' }; | ||
|
|
||
| const result = buildApiDocURL(entry); | ||
|
|
||
| assert.equal(result.href, `${BASE}api/crypto.md`); | ||
| }); | ||
| }); |
This file contains hidden or 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,19 @@ | ||
| import { BASE_URL } from '../constants.mjs'; | ||
|
|
||
| /** | ||
| * Builds the url of a api doc entry. | ||
| * | ||
| * @param {ApiDocMetadataEntry} entry | ||
| * @param {boolean} [useHtml] | ||
| * @returns {URL} | ||
| */ | ||
| export const buildApiDocURL = (entry, useHtml = false) => { | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/'); | ||
|
|
||
| if (useHtml) { | ||
| const htmlPath = path.replace(/\.md$/, '.html'); | ||
| return new URL(htmlPath, BASE_URL); | ||
| } | ||
|
|
||
| return new URL(path, BASE_URL); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
You can probably use hast (but I'm also fine with it this way), seeing as the majority of it is a template
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.
If we use hast, we are probably going to need https://github.com/syntax-tree/hast-util-to-xast too
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.
I'm fine with whatever that uses the least amount of dependencies. We can also just use yet another template file, we can also simply use another dependency, like the xast one, or rss/feeds or whatever.