Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/generators/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import llmsTxt from './llms-txt/index.mjs';
import manPage from './man-page/index.mjs';
import metadata from './metadata/index.mjs';
import oramaDb from './orama-db/index.mjs';
import sitemap from './sitemap/index.mjs';
import web from './web/index.mjs';

export const publicGenerators = {
Expand All @@ -27,6 +28,7 @@ export const publicGenerators = {
'api-links': apiLinks,
'orama-db': oramaDb,
'llms-txt': llmsTxt,
sitemap,
web,
};

Expand Down
75 changes: 75 additions & 0 deletions src/generators/sitemap/index.mjs
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],
Copy link
Member

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 lastmod from it?

Copy link
Member Author

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

Copy link
Member

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!

Copy link
Member Author

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?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can optimize this by doing something like

Suggested change
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];
apiPages.push({
loc: new URL('/docs/latest/api/', BASE_URL).href,
lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily',
priority: '1.0',
})


const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And use handlebars or whatever we have already on doc-kit for replacement?

(What we already have is .replace("__MY_VARIABLE__", MY_VALUE)

<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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a draft, but I was wondering if:

  1. Does our HAST builder work?
  2. If not, can we use dedent for formatting?


if (output) {
await writeFile(join(output, 'sitemap.xml'), sitemap, 'utf-8');
}

return sitemap;
},
};
Loading