forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildApiDocLink.mjs
More file actions
50 lines (41 loc) · 1.24 KB
/
buildApiDocLink.mjs
File metadata and controls
50 lines (41 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { LATEST_DOC_API_BASE_URL } from '../../../constants.mjs';
import { transformNodeToString } from '../../../utils/unist.mjs';
/**
* Retrieves the description of a given API doc entry. It first checks whether
* the entry has a llm_description property. If not, it extracts the first
* paragraph from the entry's content.
*
* @param {ApiDocMetadataEntry} entry
* @returns {string}
*/
const getEntryDescription = entry => {
if (entry.llm_description) {
return entry.llm_description;
}
const descriptionNode = entry.content.children.find(
child => child.type === 'paragraph'
);
if (!descriptionNode) {
return '';
}
return (
transformNodeToString(descriptionNode)
// Remove newlines and extra spaces
.replace(/[\r\n]+/g, '')
);
};
/**
* Builds a markdown link for an API doc entry
*
* @param {ApiDocMetadataEntry} entry
* @returns {string}
*/
export const buildApiDocLink = entry => {
const title = entry.heading.data.name;
// Remove the leading doc/ from the path
const path = entry.api_doc_source.replace(/^doc\//, '');
const url = new URL(path, LATEST_DOC_API_BASE_URL);
const link = `[${title}](${url})`;
const description = getEntryDescription(entry);
return `${link}: ${description}`;
};