Skip to content

Commit 126f8af

Browse files
committed
feat: Add absolute url to API example
1 parent 760331e commit 126f8af

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docusaurus.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,29 @@ module.exports = {
303303
if (node.title) return `[${node.title}](${url})`;
304304
return url;
305305
},
306+
code: (node) => {
307+
const apiMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
308+
const splitValue = node.value.trim().split('\n');
309+
310+
splitValue.forEach((item, i, valuesArray) => {
311+
if (!apiMethods.some((method) => item.startsWith(method))) {
312+
// try to parse as URL, if successful, prefix with absolute URL
313+
try {
314+
const parsedUrl = parse(valuesArray[i + 1]);
315+
if (isInternal(parsedUrl) && parsedUrl.pathname) {
316+
valuesArray[i + 1] = `${config.absoluteUrl}${parsedUrl.pathname}`;
317+
}
318+
} catch {
319+
// do nothing, leave the line as is
320+
}
321+
}
322+
});
323+
324+
if (apiMethods.some((method) => node.value.trim().startsWith(method))) {
325+
node.lang = node.lang.toLowerCase();
326+
}
327+
return `\n\`\`\`${node.lang || ''}\n${node.value}\n\`\`\`\n`;
328+
},
306329
},
307330
},
308331
excludeRoutes: [

0 commit comments

Comments
 (0)