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
33 lines (26 loc) · 895 Bytes
/
buildApiDocLink.mjs
File metadata and controls
33 lines (26 loc) · 895 Bytes
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
import { LATEST_DOC_API_BASE_URL } from '../../../constants.mjs';
import { transformNodeToString } from '../../../utils/unist.mjs';
/**
* 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})`;
// Find the first paragraph in the content
const descriptionNode = entry.content.children.find(
child => child.type === 'paragraph'
);
if (!descriptionNode) {
return link;
}
const description = transformNodeToString(descriptionNode)
// Remove newlines and extra spaces
.replace(/[\r\n]+/g, ' ');
return `${link}: ${description}`;
};