-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmdx.ts
377 lines (338 loc) · 12.4 KB
/
mdx.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import {s} from 'hastscript';
import yaml from 'js-yaml';
import {bundleMDX} from 'mdx-bundler';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypePresetMinify from 'rehype-preset-minify';
import rehypePrismDiff from 'rehype-prism-diff';
import rehypePrismPlus from 'rehype-prism-plus';
import rehypeSlug from 'rehype-slug';
import remarkGfm from 'remark-gfm';
import remarkMdxImages from 'remark-mdx-images';
import getAppRegistry from './build/appRegistry';
import getPackageRegistry from './build/packageRegistry';
import {apiCategories} from './build/resolveOpenAPI';
import getAllFilesRecursively from './files';
import remarkCodeTabs from './remark-code-tabs';
import remarkCodeTitles from './remark-code-title';
import remarkComponentSpacing from './remark-component-spacing';
import remarkExtractFrontmatter from './remark-extract-frontmatter';
import remarkImageSize from './remark-image-size';
import remarkTocHeadings, {TocNode} from './remark-toc-headings';
import remarkVariables from './remark-variables';
import {FrontMatter, Platform, PlatformConfig} from './types';
const root = process.cwd();
function formatSlug(slug: string) {
return slug.replace(/\.(mdx|md)/, '');
}
const isSupported = (
frontmatter: FrontMatter,
platformName: string,
guideName?: string
): boolean => {
const canonical = guideName ? `${platformName}.${guideName}` : platformName;
if (frontmatter.supported && frontmatter.supported.length) {
if (frontmatter.supported.indexOf(canonical) !== -1) {
return true;
}
if (frontmatter.supported.indexOf(platformName) === -1) {
return false;
}
}
if (
frontmatter.notSupported &&
(frontmatter.notSupported.indexOf(canonical) !== -1 ||
frontmatter.notSupported.indexOf(platformName) !== -1)
) {
return false;
}
return true;
};
let getDocsFrontMatterCache: Promise<FrontMatter[]> | undefined;
export function getDocsFrontMatter(): Promise<FrontMatter[]> {
if (getDocsFrontMatterCache) {
return getDocsFrontMatterCache;
}
getDocsFrontMatterCache = getDocsFrontMatterUncached();
return getDocsFrontMatterCache;
}
async function getDocsFrontMatterUncached(): Promise<FrontMatter[]> {
const frontMatter = getAllFilesFrontMatter();
const categories = await apiCategories();
categories.forEach(category => {
frontMatter.push({
title: category.name,
slug: `api/${category.slug}`,
});
category.apis.forEach(api => {
frontMatter.push({
title: api.name,
slug: `api/${category.slug}/${api.slug}`,
});
});
});
// Remove a trailing /index, since that is also removed from the path by Next.
frontMatter.forEach(fm => {
const trailingIndex = '/index';
if (fm.slug.endsWith(trailingIndex)) {
fm.slug = fm.slug.slice(0, fm.slug.length - trailingIndex.length);
}
});
return frontMatter;
}
export function getAllFilesFrontMatter(folder: string = 'docs') {
const docsPath = path.join(root, folder);
const files = getAllFilesRecursively(docsPath);
const allFrontMatter: FrontMatter[] = [];
files.forEach(file => {
const fileName = file.slice(docsPath.length + 1);
if (path.extname(fileName) !== '.md' && path.extname(fileName) !== '.mdx') {
return;
}
if (fileName.indexOf('/common/') !== -1) {
return;
}
const source = fs.readFileSync(file, 'utf8');
const {data: frontmatter} = matter(source);
allFrontMatter.push({
...(frontmatter as FrontMatter),
slug: formatSlug(fileName),
sourcePath: path.join(folder, fileName),
});
});
if (folder !== 'docs') {
// We exit early if we're not in the docs folder. We use this for the changelog.
return allFrontMatter;
}
// Add all `common` files in the right place.
const platformsPath = path.join(docsPath, 'platforms');
const platformNames = fs
.readdirSync(platformsPath)
.filter(p => !fs.statSync(path.join(platformsPath, p)).isFile());
platformNames.forEach(platformName => {
let platformFrontmatter: PlatformConfig = {};
const configPath = path.join(platformsPath, platformName, 'config.yml');
if (fs.existsSync(configPath)) {
platformFrontmatter = yaml.load(
fs.readFileSync(configPath, 'utf8')
) as PlatformConfig;
}
const commonPath = path.join(platformsPath, platformName, 'common');
if (!fs.existsSync(commonPath)) {
return;
}
const commonFileNames: string[] = getAllFilesRecursively(commonPath).filter(
p => path.extname(p) === '.mdx'
);
const commonFiles = commonFileNames.map(commonFileName => {
const source = fs.readFileSync(commonFileName, 'utf8');
const {data: frontmatter} = matter(source);
return {commonFileName, frontmatter: frontmatter as FrontMatter};
});
commonFiles.forEach(f => {
if (!isSupported(f.frontmatter, platformName)) {
return;
}
const subpath = f.commonFileName.slice(commonPath.length + 1);
const slug = f.commonFileName.slice(docsPath.length + 1).replace(/\/common\//, '/');
if (
!fs.existsSync(path.join(docsPath, slug)) &&
!fs.existsSync(path.join(docsPath, slug.replace('/index.mdx', '.mdx')))
) {
let frontmatter = f.frontmatter;
if (subpath === 'index.mdx') {
frontmatter = {...frontmatter, ...platformFrontmatter};
}
allFrontMatter.push({
...frontmatter,
slug: formatSlug(slug),
sourcePath: 'docs/' + f.commonFileName.slice(docsPath.length + 1),
});
}
});
const guidesPath = path.join(docsPath, 'platforms', platformName, 'guides');
let guideNames: string[] = [];
if (!fs.existsSync(guidesPath)) {
return;
}
guideNames = fs
.readdirSync(guidesPath)
.filter(g => !fs.statSync(path.join(guidesPath, g)).isFile());
guideNames.forEach(guideName => {
let guideFrontmatter: FrontMatter | null = null;
const guideConfigPath = path.join(guidesPath, guideName, 'config.yml');
if (fs.existsSync(guideConfigPath)) {
guideFrontmatter = yaml.load(
fs.readFileSync(guideConfigPath, 'utf8')
) as FrontMatter;
}
commonFiles.forEach(f => {
if (!isSupported(f.frontmatter, platformName, guideName)) {
return;
}
const subpath = f.commonFileName.slice(commonPath.length + 1);
const slug = path.join('platforms', platformName, 'guides', guideName, subpath);
if (!fs.existsSync(path.join(docsPath, slug))) {
let frontmatter = f.frontmatter;
if (subpath === 'index.mdx') {
frontmatter = {...frontmatter, ...guideFrontmatter};
}
allFrontMatter.push({
...frontmatter,
slug: formatSlug(slug),
sourcePath: 'docs/' + f.commonFileName.slice(docsPath.length + 1),
});
}
});
});
});
return allFrontMatter;
}
export async function getFileBySlug(slug: string) {
const configPath = path.join(root, slug, 'config.yml');
let configFrontmatter: PlatformConfig | undefined;
if (fs.existsSync(configPath)) {
configFrontmatter = yaml.load(fs.readFileSync(configPath, 'utf8')) as PlatformConfig;
}
let mdxPath = path.join(root, `${slug}.mdx`);
let mdxIndexPath = path.join(root, slug, 'index.mdx');
let mdPath = path.join(root, `${slug}.md`);
let mdIndexPath = path.join(root, slug, 'index.md');
if (
slug.indexOf('docs/platforms/') === 0 &&
[mdxPath, mdxIndexPath, mdPath, mdIndexPath].filter(p => fs.existsSync(p)).length ===
0
) {
// Try the common folder.
const slugParts = slug.split('/');
const commonPath = path.join(slugParts.slice(0, 3).join('/'), 'common');
let commonFilePath: string | undefined;
if (
slugParts.length >= 5 &&
slugParts[1] === 'platforms' &&
slugParts[3] === 'guides'
) {
commonFilePath = path.join(commonPath, slugParts.slice(5).join('/'));
} else if (slugParts.length >= 3 && slugParts[1] === 'platforms') {
commonFilePath = path.join(commonPath, slugParts.slice(3).join('/'));
}
if (commonFilePath && fs.existsSync(commonPath)) {
mdxPath = path.join(root, `${commonFilePath}.mdx`);
mdxIndexPath = path.join(root, commonFilePath, 'index.mdx');
mdPath = path.join(root, `${commonFilePath}.md`);
mdIndexPath = path.join(root, commonFilePath, 'index.md');
}
}
const sourcePath = [mdxPath, mdxIndexPath, mdPath].find(fs.existsSync) ?? mdIndexPath;
const source = fs.readFileSync(sourcePath, 'utf8');
process.env.ESBUILD_BINARY_PATH = path.join(
root,
'node_modules',
'esbuild',
'bin',
'esbuild'
);
const toc: TocNode[] = [];
// cwd is how mdx-bundler knows how to resolve relative paths
const cwd = path.dirname(sourcePath);
const result = await bundleMDX<Platform>({
source,
cwd,
mdxOptions(options) {
// this is the recommended way to add custom remark/rehype plugins:
// The syntax might look weird, but it protects you in case we add/remove
// plugins in the future.
options.remarkPlugins = [
...(options.remarkPlugins ?? []),
remarkExtractFrontmatter,
[remarkTocHeadings, {exportRef: toc}],
remarkGfm,
[remarkImageSize, {sourceFolder: cwd, publicFolder: path.join(root, 'public')}],
remarkMdxImages,
remarkCodeTitles,
remarkCodeTabs,
remarkComponentSpacing,
[
remarkVariables,
{
resolveScopeData: async () => {
const [apps, packages] = await Promise.all([
getAppRegistry(),
getPackageRegistry(),
]);
return {apps, packages};
},
},
],
];
options.rehypePlugins = [
...(options.rehypePlugins ?? []),
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'wrap',
properties: {
ariaHidden: true,
tabIndex: -1,
className: 'autolink-heading',
},
content: [
s(
'svg.anchor.before',
{
xmlns: 'http://www.w3.org/2000/svg',
width: 16,
height: 16,
fill: 'currentColor',
viewBox: '0 0 24 24',
},
s('path', {
d: 'M9.199 13.599a5.99 5.99 0 0 0 3.949 2.345 5.987 5.987 0 0 0 5.105-1.702l2.995-2.994a5.992 5.992 0 0 0 1.695-4.285 5.976 5.976 0 0 0-1.831-4.211 5.99 5.99 0 0 0-6.431-1.242 6.003 6.003 0 0 0-1.905 1.24l-1.731 1.721a.999.999 0 1 0 1.41 1.418l1.709-1.699a3.985 3.985 0 0 1 2.761-1.123 3.975 3.975 0 0 1 2.799 1.122 3.997 3.997 0 0 1 .111 5.644l-3.005 3.006a3.982 3.982 0 0 1-3.395 1.126 3.987 3.987 0 0 1-2.632-1.563A1 1 0 0 0 9.201 13.6zm5.602-3.198a5.99 5.99 0 0 0-3.949-2.345 5.987 5.987 0 0 0-5.105 1.702l-2.995 2.994a5.992 5.992 0 0 0-1.695 4.285 5.976 5.976 0 0 0 1.831 4.211 5.99 5.99 0 0 0 6.431 1.242 6.003 6.003 0 0 0 1.905-1.24l1.723-1.723a.999.999 0 1 0-1.414-1.414L9.836 19.81a3.985 3.985 0 0 1-2.761 1.123 3.975 3.975 0 0 1-2.799-1.122 3.997 3.997 0 0 1-.111-5.644l3.005-3.006a3.982 3.982 0 0 1 3.395-1.126 3.987 3.987 0 0 1 2.632 1.563 1 1 0 0 0 1.602-1.198z',
})
),
],
},
],
[rehypePrismPlus, {ignoreMissing: true}],
[rehypePrismDiff, {remove: true}],
rehypePresetMinify,
];
return options;
},
esbuildOptions: options => {
options.loader = {
...options.loader,
'.js': 'jsx',
'.png': 'file',
'.gif': 'file',
'.jpg': 'file',
'.jpeg': 'file',
// inline svgs
'.svg': 'dataurl',
};
// Set the `outdir` to a public location for this bundle.
// this where this images will be copied
options.outdir = path.join(root, 'public', 'mdx-images');
// Set write to true so that esbuild will output the files.
options.write = true;
return options;
},
});
const {code, frontmatter} = result;
let mergedFrontmatter = frontmatter;
if (configFrontmatter) {
mergedFrontmatter = {...frontmatter, ...configFrontmatter};
}
return {
matter: result.matter,
mdxSource: code,
toc,
frontMatter: {
...mergedFrontmatter,
slug,
},
};
}