forked from deepset-ai/haystack-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
139 lines (120 loc) · 3.83 KB
/
utils.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import fs from "fs";
import { join } from "path";
import remark from "remark";
import html from "remark-html";
import slug from "remark-slug";
import remarkPrism from "remark-prism";
import autolink from "remark-autolink-headings";
import GitHubSlugger from "github-slugger";
import imgLinks from "@pondorasti/remark-img-links";
import { getHaystackReleaseTagNames, getStargazersCount } from "./github";
import { MDXRemoteSerializeResult } from "next-mdx-remote";
// we have to explicitly require prismjs and loadLanguages so that they're available during revalidation on Vercel
const Prism = require("prismjs");
const loadLanguages = require("prismjs/components/index");
export const markdownToHtml = async ({
content,
downloadUrl,
}: {
content: string;
downloadUrl: string;
}) => {
loadLanguages();
const result = await remark()
.use(imgLinks, {
absolutePath: downloadUrl,
})
// @ts-ignore
.use(html)
// @ts-ignore
.use(slug)
// @ts-ignore
.use(autolink)
.use(remarkPrism)
.process(content);
return {
markup: result.toString(),
};
};
export type StaticPageProps = {
menu: any;
toc: { text: string; level: number; link: string }[];
editOnGitHubLink: string;
stars: number;
source: MDXRemoteSerializeResult | string;
};
const slugger = new GitHubSlugger();
export const getStaticLayoutProps = async ({
content,
version,
docTitleSlug,
type,
}: {
content: string;
version?: string;
docTitleSlug: string;
type: string;
}) => {
const getHeadings = () => {
const headingLines = content
.split("\n")
.filter((line) => line.match(/^###*\s/));
return headingLines.map((raw) => {
const text = raw.replace(/^###*\s/, "").replace(/\\/g, "");
const level = raw.slice(0, 3) === "###" ? 3 : 2;
return { text, level, link: slugger.slug(text) };
});
};
const menu = await getMenu(version);
const toc = getHeadings();
const latestVersion = await getLatestVersion();
const editOnGitHubLink = `https://github.com/deepset-ai/haystack-website/blob/source/docs/${
version || latestVersion
}/${type}/${docTitleSlug.split("-").join("_")}.mdx`;
const stars = await getStargazersCount();
return { menu, toc, editOnGitHubLink, stars };
};
export const getMenu = async (version?: string) => {
const latestVersion = await getLatestVersion();
const menu = await import(`../docs/${version || latestVersion}/menu.json`);
// JSON files don’t have a default export, so we have to explicitly return the default property
return menu.default;
};
export async function getDocsVersions() {
const tagNames = await getHaystackReleaseTagNames();
return tagNames.filter((tagName) => tagName.startsWith("v"));
}
export async function getVersionFromParams(params: string[]) {
const versions = await getDocsVersions();
const latestVersion = "latest";
return versions.find((version) => params.includes(version)) ?? latestVersion;
}
export async function getLatestVersion() {
return "latest";
}
export async function getDirectory(
category: "overview" | "usage" | "guides" | "components",
version?: string
) {
const latestVersion = await getLatestVersion();
return join(process.cwd(), `docs/${version || latestVersion}/${category}`);
}
export async function getDirectoryBenchmarks(
category: "map" | "performance" | "speed",
version?: string
) {
const latestVersion = await getLatestVersion();
return join(
process.cwd(),
`benchmarks/${version || latestVersion}/${category}`
);
}
export async function getSlugsFromLocalMarkdownFiles(
category: "overview" | "usage" | "guides" | "components",
version?: string
) {
const directory = await getDirectory(category, version);
if (!fs.existsSync(directory)) return [];
const filenames = fs.readdirSync(directory);
return filenames.map((file) => file.replace(/\.mdx$/, "").split("_").join("-"));
}