Skip to content

Commit 6fa782a

Browse files
committed
refactor: review
1 parent 914652c commit 6fa782a

File tree

7 files changed

+91
-23
lines changed

7 files changed

+91
-23
lines changed

npm-shrinkwrap.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/generators/llms-txt/utils/buildApiDocLink.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { BASE_URL } from '../../../constants.mjs';
21
import { transformNodeToString } from '../../../utils/unist.mjs';
2+
import { buildApiDocURL } from '../../../utils/url.mjs';
33

44
/**
55
* Retrieves the description of a given API doc entry. It first checks whether
@@ -38,8 +38,7 @@ export const getEntryDescription = entry => {
3838
export const buildApiDocLink = entry => {
3939
const title = entry.heading.data.name;
4040

41-
const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/');
42-
const url = new URL(path, BASE_URL);
41+
const url = buildApiDocURL(entry);
4342

4443
const link = `[${title}](${url})`;
4544

src/generators/sitemap/index.mjs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { join } from 'node:path';
44
import dedent from 'dedent';
55

66
import { BASE_URL } from '../../constants.mjs';
7+
import { buildApiDocUrl } from './utils/buildApiDocUrl.mjs';
78

89
/**
910
* This generator generates a sitemap.xml file for search engine optimization
@@ -33,17 +34,7 @@ export default {
3334

3435
const apiPages = entries
3536
.filter(entry => entry.heading.depth === 1)
36-
.map(entry => {
37-
const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/');
38-
const url = new URL(path, BASE_URL).href;
39-
40-
return {
41-
loc: url,
42-
lastmod,
43-
changefreq: 'weekly',
44-
priority: '0.8',
45-
};
46-
});
37+
.map(entry => buildApiDocUrl(entry, lastmod));
4738

4839
apiPages.push({
4940
loc: new URL('/docs/latest/api/', BASE_URL).href,

src/generators/sitemap/types.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface SitemapUrl {
2+
loc: string;
3+
lastmod?: string;
4+
changefreq?:
5+
| 'always'
6+
| 'hourly'
7+
| 'daily'
8+
| 'weekly'
9+
| 'monthly'
10+
| 'yearly'
11+
| 'never';
12+
priority?: string;
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { buildApiDocURL } from '../../../utils/url.mjs';
2+
3+
/**
4+
* Builds an API doc sitemap url.
5+
*
6+
* @param {ApiDocMetadataEntry} entry
7+
* @param {string} lastmod
8+
* @returns {import('../types').SitemapUrl}
9+
*/
10+
export const buildApiDocUrl = (entry, lastmod) => {
11+
const { href } = buildApiDocURL(entry, true);
12+
13+
return { loc: href, lastmod, changefreq: 'weekly', priority: '0.8' };
14+
};

src/utils/__tests__/url.test.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import assert from 'node:assert/strict';
2+
import { describe, it } from 'node:test';
3+
4+
import { buildApiDocURL } from '../url.mjs';
5+
6+
const BASE = 'https://nodejs.org/';
7+
8+
describe('buildApiDocURL', () => {
9+
it('builds markdown doc URLs from doc/ sources', () => {
10+
const entry = { api_doc_source: 'doc/api/fs.md' };
11+
12+
const result = buildApiDocURL(entry);
13+
14+
assert.equal(result.href, `${BASE}docs/latest/api/fs.md`);
15+
});
16+
17+
it('builds html doc URLs when requested', () => {
18+
const entry = { api_doc_source: 'doc/api/path.md' };
19+
20+
const result = buildApiDocURL(entry, true);
21+
22+
assert.equal(result.href, `${BASE}docs/latest/api/path.html`);
23+
});
24+
25+
it('leaves non doc/ sources untouched', () => {
26+
const entry = { api_doc_source: 'api/crypto.md' };
27+
28+
const result = buildApiDocURL(entry);
29+
30+
assert.equal(result.href, `${BASE}api/crypto.md`);
31+
});
32+
});

src/utils/url.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { BASE_URL } from '../constants.mjs';
2+
3+
/**
4+
* Builds the url of a api doc entry.
5+
*
6+
* @param {ApiDocMetadataEntry} entry
7+
* @param {boolean} [useHtml]
8+
* @returns {URL}
9+
*/
10+
export const buildApiDocURL = (entry, useHtml = false) => {
11+
const path = entry.api_doc_source.replace(/^doc\//, '/docs/latest/');
12+
13+
if (useHtml) {
14+
const htmlPath = path.replace(/\.md$/, '.html');
15+
return new URL(htmlPath, BASE_URL);
16+
}
17+
18+
return new URL(path, BASE_URL);
19+
};

0 commit comments

Comments
 (0)