Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: script to validate all markdown validation errors #1454

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/ui/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type { DocsProps, FeatureFlags } from "./atoms";
export * from "./docs/DocsPage";
export * from "./docs/NextApp";
export { getApiRouteSupplier } from "./hooks/useApiRoute";
export { serializeMdx, setMdxBundler } from "./mdx/bundler";
export { serializeMdx, setMdxBundler, unsafeSerializeMdx } from "./mdx/bundler";
export { getFrontmatter } from "./mdx/frontmatter";
export { Stream } from "./playground/Stream";
export { ProxyRequestSchema } from "./playground/types";
Expand Down
28 changes: 25 additions & 3 deletions packages/ui/app/src/mdx/bundler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { captureSentryError } from "../analytics/sentry";
import { serializeMdx as defaultSerializeMdx } from "./bundlers/next-mdx-remote";
import type { BundledMDX, FernSerializeMdxOptions, SerializeMdxFunc } from "./types";

Expand All @@ -11,12 +12,12 @@ export function setMdxBundler(engine: SerializeMdxFunc): void {
currentEngine = engine;
}

export async function serializeMdx(content: string, options?: FernSerializeMdxOptions): Promise<BundledMDX>;
export async function serializeMdx(
export async function unsafeSerializeMdx(content: string, options?: FernSerializeMdxOptions): Promise<BundledMDX>;
export async function unsafeSerializeMdx(
content: string | undefined,
options?: FernSerializeMdxOptions,
): Promise<BundledMDX | undefined>;
export async function serializeMdx(
export async function unsafeSerializeMdx(
content: string | undefined,
options: FernSerializeMdxOptions = {},
): Promise<BundledMDX | undefined> {
Expand All @@ -27,3 +28,24 @@ export async function serializeMdx(
const bundler = await getMdxBundler();
return bundler(content, options);
}

export async function serializeMdx(content: string, options?: FernSerializeMdxOptions): Promise<BundledMDX>;
export async function serializeMdx(
content: string | undefined,
options?: FernSerializeMdxOptions,
): Promise<BundledMDX | undefined>;
export async function serializeMdx(
content: string | undefined,
options: FernSerializeMdxOptions = {},
): Promise<BundledMDX | undefined> {
try {
return unsafeSerializeMdx(content, options);
} catch (e) {
captureSentryError(e, {
context: "MDX",
errorSource: "maybeSerializeMdxContent",
errorDescription: "Failed to serialize MDX content",
});
}
return content;
}
170 changes: 82 additions & 88 deletions packages/ui/app/src/mdx/bundlers/mdx-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,97 +55,91 @@ export async function serializeMdx(
}
}

try {
const bundled = await bundleMDX<FernDocsFrontmatter>({
source: content,
files: mapKeys(files ?? {}, (_file, filename) => {
if (cwd != null) {
return path.relative(cwd, filename);
}
return filename;
}),

mdxOptions: (o: Options, matter) => {
o.remarkRehypeOptions = {
...o.remarkRehypeOptions,
...options,
handlers: {
...o.remarkRehypeOptions?.handlers,
heading: customHeadingHandler,
...options?.remarkRehypeOptions?.handlers,
},
const bundled = await bundleMDX<FernDocsFrontmatter>({
source: content,
files: mapKeys(files ?? {}, (_file, filename) => {
if (cwd != null) {
return path.relative(cwd, filename);
}
return filename;
}),

mdxOptions: (o: Options, matter) => {
o.remarkRehypeOptions = {
...o.remarkRehypeOptions,
...options,
handlers: {
...o.remarkRehypeOptions?.handlers,
heading: customHeadingHandler,
...options?.remarkRehypeOptions?.handlers,
},
};

const remarkPlugins: PluggableList = [
remarkSqueezeParagraphs,
remarkGfm,
remarkSmartypants,
remarkMath,
remarkGemoji,
];

if (options?.remarkPlugins != null) {
remarkPlugins.push(...options.remarkPlugins);
}

o.remarkPlugins = [...(o.remarkPlugins ?? []), ...remarkPlugins, ...(options?.remarkPlugins ?? [])];

const rehypePlugins: PluggableList = [
rehypeSqueezeParagraphs,
rehypeSlug,
rehypeKatex,
rehypeFernCode,
rehypeFernComponents,
];

if (options?.rehypePlugins != null) {
rehypePlugins.push(...options.rehypePlugins);
}

if (frontmatterDefaults != null) {
const opts = {
matter: mergeMatter(matter, frontmatterDefaults),
};
rehypePlugins.push([rehypeFernLayout, opts]);
frontmatter = opts.matter;
} else {
frontmatter = mergeMatter(matter, frontmatter);
}

const remarkPlugins: PluggableList = [
remarkSqueezeParagraphs,
remarkGfm,
remarkSmartypants,
remarkMath,
remarkGemoji,
];

if (options?.remarkPlugins != null) {
remarkPlugins.push(...options.remarkPlugins);
}

o.remarkPlugins = [...(o.remarkPlugins ?? []), ...remarkPlugins, ...(options?.remarkPlugins ?? [])];

const rehypePlugins: PluggableList = [
rehypeSqueezeParagraphs,
rehypeSlug,
rehypeKatex,
rehypeFernCode,
rehypeFernComponents,
];

if (options?.rehypePlugins != null) {
rehypePlugins.push(...options.rehypePlugins);
}

if (frontmatterDefaults != null) {
const opts = {
matter: mergeMatter(matter, frontmatterDefaults),
};
rehypePlugins.push([rehypeFernLayout, opts]);
frontmatter = opts.matter;
} else {
frontmatter = mergeMatter(matter, frontmatter);
}

// Always sanitize JSX at the end.
// rehypePlugins.push([rehypeSanitizeJSX, { showError }]);

o.rehypePlugins = [...(o.rehypePlugins ?? []), ...rehypePlugins, ...(options?.rehypePlugins ?? [])];

o.recmaPlugins = [...(o.recmaPlugins ?? []), ...(options?.recmaPlugins ?? [])];

o.development = options.development ?? o.development;

return o;
},

esbuildOptions: (o) => {
o.minify = disableMinify ? false : true;
return o;
},
});
// Always sanitize JSX at the end.
// rehypePlugins.push([rehypeSanitizeJSX, { showError }]);

if (bundled.errors.length > 0) {
bundled.errors.forEach((error) => {
// eslint-disable-next-line no-console
console.error(error);
});
}
o.rehypePlugins = [...(o.rehypePlugins ?? []), ...rehypePlugins, ...(options?.rehypePlugins ?? [])];

o.recmaPlugins = [...(o.recmaPlugins ?? []), ...(options?.recmaPlugins ?? [])];

o.development = options.development ?? o.development;

return o;
},

return {
engine: "mdx-bundler",
code: bundled.code,
frontmatter,
errors: bundled.errors,
};
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return content;
esbuildOptions: (o) => {
o.minify = disableMinify ? false : true;
return o;
},
});

if (bundled.errors.length > 0) {
bundled.errors.forEach((error) => {
// eslint-disable-next-line no-console
console.error(error);
});
}

return {
engine: "mdx-bundler",
code: bundled.code,
frontmatter,
errors: bundled.errors,
};
}
35 changes: 11 additions & 24 deletions packages/ui/app/src/mdx/bundlers/next-mdx-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkSmartypants from "remark-smartypants";
import type { PluggableList } from "unified";
import { captureSentryError } from "../../analytics/sentry";
import { stringHasMarkdown } from "../common/util";
import { FernDocsFrontmatter } from "../frontmatter";
import { rehypeFernCode } from "../plugins/rehypeFernCode";
Expand Down Expand Up @@ -103,28 +102,16 @@ export async function serializeMdx(

content = replaceBrokenBrTags(content);

try {
const result = await serialize<Record<string, unknown>, FernDocsFrontmatter>(content, {
scope: {},
mdxOptions: withDefaultMdxOptions(options),
parseFrontmatter: true,
});
return {
engine: "next-mdx-remote",
code: result.compiledSource,
frontmatter: result.frontmatter,
errors: [],
};
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
const result = await serialize<Record<string, unknown>, FernDocsFrontmatter>(content, {
scope: {},
mdxOptions: withDefaultMdxOptions(options),
parseFrontmatter: true,
});

captureSentryError(e, {
context: "MDX",
errorSource: "maybeSerializeMdxContent",
errorDescription: "Failed to serialize MDX content",
});

return content;
}
return {
engine: "next-mdx-remote",
code: result.compiledSource,
frontmatter: result.frontmatter,
errors: [],
};
}
Loading
Loading