From c804c82ebfc307fd4b46ae33cf6692ed44a2d079 Mon Sep 17 00:00:00 2001 From: Nick Frostbutter <75431177+nickfrosty@users.noreply.github.com> Date: Tue, 16 Jul 2024 09:44:28 -0400 Subject: [PATCH] fix: code block altering (#1619) --- src/utils/contentApi.ts | 61 ----------------------------------------- 1 file changed, 61 deletions(-) diff --git a/src/utils/contentApi.ts b/src/utils/contentApi.ts index a6c36d70c..c457bd316 100644 --- a/src/utils/contentApi.ts +++ b/src/utils/contentApi.ts @@ -90,10 +90,6 @@ export default class ContentApi { const route = `/api/overview/${locale}`; return await this.fetch(route) .then((res) => res.json() as unknown as ContentOverviewRecords) - // .then((record) => { - // record.body = this.preProcessContent(record.body); - // return record; - // }) .catch((err) => { // note: catching errors here will help prevent pages from loading on the front end console.error("[getOverviewRecords]", route); @@ -122,10 +118,6 @@ export default class ContentApi { route = `/api/content/${locale}/${route}`; return await this.fetch(route) .then((res) => res.json() as unknown as ContentRecord) - .then((record) => { - record.body = this.preProcessContent(record.body); - return record; - }) .catch((err) => { // note: catching errors here will help prevent pages from loading on the front end console.error("[getSingleRecord]", route, "groupKey:", groupKey); @@ -301,59 +293,6 @@ export default class ContentApi { return fileName.split(".")[0]; } - /** - * Pre-process a record's content (via the server), before sending it to the client - */ - static preProcessContent(content: string = ""): string { - // remove markdown comments (since they break mdx-remote's serialize) - content = content?.trim()?.replace(//gm, "") || ""; - - /** - * manually correct some of the markdown formatting that `mdx.serialize` does not lik: - * - convert `tsx` and `jsx` codeblocks to `typescript` - * - `<=` and `>=` (which mdx thinks are invalid html tags) - */ - content = content?.replace(/```(tsx|jsx)/gm, "```typescript"); - - // process all markdown links (e.g. remove ".md" and handle relative links) - content = this.processMarkdownLinks(content); - - // fix formatting issues for mdx component ending tags - // note: this seems to be common after content is processed by crowdin... - content = content?.replaceAll(/\<\/(.*)\>$/gm, "\n"); - content = content?.replaceAll(/^\<(.*)\>/gm, "\n<$1>\n"); - - return content; - } - - /** - * Helper function to process all markdown links, including: - * - converting all relative repo links to valid relative links for the site - */ - static processMarkdownLinks(content: string): string { - // locate and parse all links in the raw markdown - return content?.replace( - REGEX_MARKDOWN_LINKS, - (fullMatched: string, label: string, url: string) => { - // for errors in the regex, just return the original `fullMatched` string - if (!label || !url) return fullMatched; - - url = this.processSingleLink(url); - - // process the internal links (i.e. those that start with "/" and ".") - if (url.startsWith("/") || url.startsWith(".")) { - // auto remove links for internal ignored files - // (i.e. those with file names that start with `_`) - if (url.toString().split("/_").length > 1) { - return label; - } - } - - return `[${label}](${url})`; - }, - ); - } - /** * Link processors for standardizing links */