Skip to content

Commit df840da

Browse files
authored
Avoid parsing example description (#2841)
1 parent f8d4c76 commit df840da

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

packages/gitbook/src/lib/openapi.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,15 @@ const fetchFilesystem = cache({
5858

5959
const text = await response.text();
6060
const filesystem = await parseOpenAPI({ value: text, rootURL: url });
61-
const cache: Map<string, Promise<string>> = new Map();
62-
const transformedFs = await traverse(filesystem, async (node) => {
61+
const parseMarkdownWithCache = createMarkdownParser();
62+
const transformedFs = await traverse(filesystem, async (node, path) => {
6363
if ('description' in node && typeof node.description === 'string' && node.description) {
64-
if (cache.has(node.description)) {
65-
node['x-description-html'] = await cache.get(node.description);
66-
} else {
67-
const promise = parseMarkdown(node.description);
68-
cache.set(node.description, promise);
69-
node['x-description-html'] = await promise;
64+
const lastKey = path && path[path.length - 1];
65+
// Avoid parsing descriptions in examples.
66+
if (lastKey === 'example') {
67+
return node;
7068
}
69+
node['x-description-html'] = await parseMarkdownWithCache(node.description);
7170
}
7271
return node;
7372
});
@@ -80,3 +79,17 @@ const fetchFilesystem = cache({
8079
};
8180
},
8281
});
82+
83+
/**
84+
* Create a markdown parser that caches the results of parsing.
85+
*/
86+
const createMarkdownParser = () => async (input: string) => {
87+
const cache = new Map<string, Promise<string>>();
88+
if (cache.has(input)) {
89+
return cache.get(input) as Promise<string>;
90+
}
91+
92+
const promise = parseMarkdown(input);
93+
cache.set(input, promise);
94+
return promise;
95+
};

0 commit comments

Comments
 (0)