Skip to content

Commit

Permalink
Merge branch 'main' into open-source
Browse files Browse the repository at this point in the history
  • Loading branch information
catalinred committed Jul 16, 2024
2 parents 1e659ae + c804c82 commit ba7a455
Showing 1 changed file with 0 additions and 61 deletions.
61 changes: 0 additions & 61 deletions src/utils/contentApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<typeof groupKey>)
.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);
Expand Down Expand Up @@ -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</$1>");
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
*/
Expand Down

0 comments on commit ba7a455

Please sign in to comment.