Skip to content

Commit 28aaa10

Browse files
committed
feat: add llm_description prop
1 parent ebc4828 commit 28aaa10

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed
Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
import { LATEST_DOC_API_BASE_URL } from '../../../constants.mjs';
22
import { transformNodeToString } from '../../../utils/unist.mjs';
33

4+
/**
5+
* Retrieves the description of a given API doc entry. It first checks whether
6+
* the entry has a llm_description property. If not, it extracts the first
7+
* paragraph from the entry's content.
8+
*
9+
* @param {ApiDocMetadataEntry} entry
10+
* @returns {string}
11+
*/
12+
const getEntryDescription = entry => {
13+
if (entry.llm_description) {
14+
return entry.llm_description;
15+
}
16+
17+
const descriptionNode = entry.content.children.find(
18+
child => child.type === 'paragraph'
19+
);
20+
21+
if (!descriptionNode) {
22+
return '';
23+
}
24+
25+
return (
26+
transformNodeToString(descriptionNode)
27+
// Remove newlines and extra spaces
28+
.replace(/[\r\n]+/g, '')
29+
);
30+
};
31+
432
/**
533
* Builds a markdown link for an API doc entry
634
*
@@ -16,18 +44,7 @@ export const buildApiDocLink = entry => {
1644

1745
const link = `[${title}](${url})`;
1846

19-
// Find the first paragraph in the content
20-
const descriptionNode = entry.content.children.find(
21-
child => child.type === 'paragraph'
22-
);
23-
24-
if (!descriptionNode) {
25-
return link;
26-
}
27-
28-
const description = transformNodeToString(descriptionNode)
29-
// Remove newlines and extra spaces
30-
.replace(/[\r\n]+/g, ' ');
47+
const description = getEntryDescription(entry);
3148

3249
return `${link}: ${description}`;
3350
};

src/metadata.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const createMetadata = slugger => {
131131
updates = [],
132132
changes = [],
133133
tags = [],
134+
llm_description,
134135
} = internalMetadata.properties;
135136

136137
// Also add the slug to the heading data as it is used to build the heading
@@ -157,6 +158,7 @@ const createMetadata = slugger => {
157158
content: section,
158159
tags,
159160
introduced_in,
161+
llm_description,
160162
yaml_position: internalMetadata.yaml_position,
161163
};
162164
},

src/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ declare global {
5656
introduced_in?: string;
5757
napiVersion?: number;
5858
tags?: Array<string>;
59+
llm_description?: string;
5960
}
6061

6162
export interface ApiDocMetadataEntry {
@@ -90,6 +91,8 @@ declare global {
9091
// Extra YAML section entries that are stringd and serve
9192
// to provide additional metadata about the API doc entry
9293
tags: Array<string>;
94+
// The llms.txt specific description
95+
llm_description: string | undefined;
9396
// The postion of the YAML of the API doc entry
9497
yaml_position: Position;
9598
}

src/utils/parser/index.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export const parseYAMLIntoMetadata = yamlString => {
9595
.replace('introduced_in=', 'introduced_in: ')
9696
.replace('source_link=', 'source_link: ')
9797
.replace('type=', 'type: ')
98-
.replace('name=', 'name: ');
98+
.replace('name=', 'name: ')
99+
.replace('llm_description=', 'llm_description: ');
99100

100101
// Ensures that the parsed YAML is an object, because even if it is not
101102
// i.e. a plain string or an array, it will simply not result into anything

0 commit comments

Comments
 (0)