1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-06-05 19:15:30 -04:00

strip js import/export statements from raw post content (for rss, etc.)

This commit is contained in:
2025-04-27 17:51:36 -04:00
parent 4cc25f7ab9
commit c4f67f170b
10 changed files with 122 additions and 103 deletions
+1
View File
@@ -0,0 +1 @@
export { default as recmaMdxEscapeMissingComponents } from "recma-mdx-escape-missing-components";
+4
View File
@@ -0,0 +1,4 @@
export { default as rehypeMdxImportMedia } from "rehype-mdx-import-media";
export { default as rehypePrettyCode } from "rehype-pretty-code";
export { default as rehypeSlug } from "rehype-slug";
export { default as rehypeUnwrapImages } from "rehype-unwrap-images";
@@ -1,7 +1,3 @@
export { default as rehypeMdxImportMedia } from "rehype-mdx-import-media";
export { default as rehypePrettyCode } from "rehype-pretty-code";
export { default as rehypeSlug } from "rehype-slug";
export { default as rehypeUnwrapImages } from "rehype-unwrap-images";
export { default as remarkFrontmatter } from "remark-frontmatter";
export { default as remarkGfm } from "remark-gfm";
export { default as remarkHtml } from "remark-html";
+2 -3
View File
@@ -4,7 +4,7 @@ import path from "path";
import fs from "fs/promises";
import glob from "fast-glob";
import { unified } from "unified";
import { remarkHtml, remarkParse, remarkSmartypants, remarkFrontmatter } from "./remark-rehype-plugins";
import { remarkHtml, remarkParse, remarkSmartypants, remarkFrontmatter } from "./mdx/remark";
import { decode } from "html-entities";
import { POSTS_DIR } from "../config/constants";
@@ -106,7 +106,6 @@ export const getFrontMatter: {
/** Returns the content of a post with very limited processing to include in RSS feeds */
export const getContent = cache(async (slug: string): Promise<string | undefined> => {
try {
// TODO: also remove MDX-related syntax (e.g. import/export statements)
const content = await unified()
.use(remarkParse)
.use(remarkFrontmatter)
@@ -137,7 +136,7 @@ export const getContent = cache(async (slug: string): Promise<string | undefined
.process(await fs.readFile(path.join(process.cwd(), `${POSTS_DIR}/${slug}/index.mdx`)));
// convert the parsed content to a string with "safe" HTML
return content.toString().replaceAll("<p></p>", "").trim();
return content.toString().replaceAll("<p></p>\n", "").replaceAll("<p>{/* prettier-ignore */}</p>\n", "").trim();
} catch (error) {
console.error(`Failed to load/parse content for post with slug "${slug}":`, error);
return undefined;