-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcustomFrontmatter.mjs
53 lines (45 loc) · 1.53 KB
/
customFrontmatter.mjs
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
51
52
53
// @ts-check
import { MarkdownPageEvent } from 'typedoc-plugin-markdown';
/**
* @param {import('typedoc-plugin-markdown').MarkdownApplication} app
*/
export function load(app) {
app.renderer.on(MarkdownPageEvent.BEGIN, (page) => {
// Update frontmatter with the page title
page.frontmatter = {
title: page.model?.name,
...page.frontmatter,
};
});
app.renderer.on(MarkdownPageEvent.END, (page) => {
// Transform specific link patterns in the page content
page.contents = replaceAndFormat(page.contents);
});
}
/**
* Transforms markdown link paths to a specific format.
* Examples:
* [`AxChatResponse`](TypeAlias.AxChatResponse.md) -> [`AxChatResponse`](#typealiasaxchatresponse)
*
* @param {string | undefined} input - The input markdown content
* @returns {string | undefined} Transformed markdown content
*/
function replaceAndFormat(input) {
if (!input) return input;
return input.replace(
/(\[`?[^`\]]+`?\]\()([^)]+)(\))/g,
(match, linkText, path, closing) => {
// Remove file extension
let transformedPath = path.replace(/\.md$/, '');
// Remove special characters like dots and convert to lowercase
transformedPath = transformedPath
.toLowerCase()
.replace(/[^a-z0-9-]/g, '');
// Add hashtag prefix if it doesn't exist
if (!transformedPath.startsWith('#')) {
transformedPath = '#apidocs/' + transformedPath;
}
return `${linkText}${transformedPath}${closing}`;
}
);
}