Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"lint:prettier:fix": "prettier --write \"./{src,app,scripts}/**/*.{md,mdx,ts,tsx,js,jsx,mjs}\"",
"lint:fix": "yarn run lint:prettier:fix && yarn run lint:eslint:fix",
"sidecar": "yarn spotlight-sidecar",
"test": "jest",
"build-trace": "npx next@canary internal turbo-trace-server .next/trace"
"test": "jest"
},
"prisma": {
"seed": "node prisma/seed/seed.mjs"
Expand All @@ -37,9 +36,9 @@
"@emotion/react": "^11.11.0",
"@emotion/styled": "^11.0.0",
"@google-cloud/storage": "^7.7.0",
"@mdx-js/loader": "^3.0.1",
"@mdx-js/react": "^3.0.1",
"@next/mdx": "^14.2.4",
"@mdx-js/loader": "^3.0.0",
"@mdx-js/react": "^3.0.0",
"@next/mdx": "^14.0.1",
"@popperjs/core": "^2.11.8",
"@prettier/plugin-xml": "^3.3.1",
"@prisma/client": "^5.8.1",
Expand All @@ -52,7 +51,7 @@
"@radix-ui/themes": "^2.0.3",
"@sentry-internal/global-search": "^1.0.0",
"@sentry/nextjs": "^8.8.0",
"@types/mdx": "^2.0.13",
"@types/mdx": "^2.0.9",
"algoliasearch": "^4.23.3",
"esbuild": "^0.19.8",
"framer-motion": "^10.12.16",
Expand All @@ -62,9 +61,9 @@
"js-cookie": "^3.0.5",
"js-yaml": "^4.1.0",
"match-sorter": "^6.3.4",
"mdx-bundler": "^10.0.2",
"mdx-bundler": "^10.0.1",
"micromark": "^4.0.0",
"next": "14.2.4",
"next": "14.0.2",
"next-auth": "^4.24.5",
"next-mdx-remote": "^4.4.1",
"nextjs-toploader": "^1.6.6",
Expand Down
46 changes: 22 additions & 24 deletions src/mdx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';

import matter from 'gray-matter';
import {s} from 'hastscript';
Expand Down Expand Up @@ -29,8 +28,7 @@ import remarkTocHeadings, {TocNode} from './remark-toc-headings';
import remarkVariables from './remark-variables';
import {FrontMatter, Platform, PlatformConfig} from './types';

// @ts-ignore
const directoryName = path.dirname(fileURLToPath(import.meta.url));
const root = process.cwd();

function formatSlug(slug: string) {
return slug.replace(/\.(mdx|md)/, '');
Expand Down Expand Up @@ -98,8 +96,8 @@ async function getDocsFrontMatterUncached(): Promise<FrontMatter[]> {
return frontMatter;
}

function getAllFilesFrontMatter() {
const docsPath = path.join(directoryName, '..', 'docs');
export function getAllFilesFrontMatter(folder: string = 'docs') {
const docsPath = path.join(root, folder);
const files = getAllFilesRecursively(docsPath);
const allFrontMatter: FrontMatter[] = [];
files.forEach(file => {
Expand All @@ -117,10 +115,15 @@ function getAllFilesFrontMatter() {
allFrontMatter.push({
...(frontmatter as FrontMatter),
slug: formatSlug(fileName),
sourcePath: path.join('docs', 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
Expand Down Expand Up @@ -215,17 +218,17 @@ function getAllFilesFrontMatter() {
}

export async function getFileBySlug(slug: string) {
const configPath = path.join(directoryName, '..', slug, 'config.yml');
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(directoryName, '..', `${slug}.mdx`);
let mdxIndexPath = path.join(directoryName, '..', slug, 'index.mdx');
let mdPath = path.join(directoryName, '..', `${slug}.md`);
let mdIndexPath = path.join(directoryName, '..', slug, 'index.md');
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 &&
Expand All @@ -246,20 +249,18 @@ export async function getFileBySlug(slug: string) {
commonFilePath = path.join(commonPath, slugParts.slice(3).join('/'));
}
if (commonFilePath && fs.existsSync(commonPath)) {
mdxPath = path.join(directoryName, '..', `${commonFilePath}.mdx`);
mdxIndexPath = path.join(directoryName, '..', commonFilePath, 'index.mdx');
mdPath = path.join(directoryName, '..', `${commonFilePath}.md`);
mdIndexPath = path.join(directoryName, '..', commonFilePath, 'index.md');
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(p => fs.existsSync(p)) ?? mdIndexPath;
const sourcePath = [mdxPath, mdxIndexPath, mdPath].find(fs.existsSync) ?? mdIndexPath;
const source = fs.readFileSync(sourcePath, 'utf8');

process.env.ESBUILD_BINARY_PATH = path.join(
directoryName,
'..',
root,
'node_modules',
'esbuild',
'bin',
Expand All @@ -284,10 +285,7 @@ export async function getFileBySlug(slug: string) {
[remarkTocHeadings, {exportRef: toc}],
remarkGfm,
remarkFormatCodeBlocks,
[
remarkImageSize,
{sourceFolder: cwd, publicFolder: path.join(directoryName, '..', 'public')},
],
[remarkImageSize, {sourceFolder: cwd, publicFolder: path.join(root, 'public')}],
remarkMdxImages,
remarkCodeTitles,
remarkCodeTabs,
Expand Down Expand Up @@ -355,7 +353,7 @@ export async function getFileBySlug(slug: string) {
};
// Set the `outdir` to a public location for this bundle.
// this where this images will be copied
options.outdir = path.join(directoryName, '..', 'public', 'mdx-images');
options.outdir = path.join(root, 'public', 'mdx-images');

// Set write to true so that esbuild will output the files.
options.write = true;
Expand Down
1 change: 0 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "https://openapi.vercel.sh/vercel.json",
"headers": [
{
"source": "/(.*)",
Expand Down
Loading