From 2b57a18582236fe2425ae48beeb521128d095ed6 Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Fri, 6 Jan 2023 19:31:33 +0100 Subject: [PATCH 01/10] Remove `astroFlavoredMd` option from `renderMarkdown()` --- .../astro/src/vite-plugin-markdown/index.ts | 1 - packages/markdown/remark/src/index.ts | 29 ++----------------- packages/markdown/remark/src/types.ts | 1 - 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 10e4cb8c0743..34ed3647b1ff 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -71,7 +71,6 @@ export default function markdown({ settings, logging }: AstroPluginOptions): Plu const renderResult = await renderMarkdown(raw.content, { ...settings.config.markdown, fileURL: new URL(`file://${fileId}`), - isAstroFlavoredMd: false, isExperimentalContentCollections: settings.config.experimental.contentCollections, contentDir: getContentPaths(settings.config).contentDir, frontmatter: raw.data, diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index 24cffa287b9d..91680f3fd848 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -8,18 +8,10 @@ import type { import { toRemarkInitializeAstroData } from './frontmatter-injection.js'; import { loadPlugins } from './load-plugins.js'; import { rehypeHeadingIds } from './rehype-collect-headings.js'; -import rehypeEscape from './rehype-escape.js'; -import rehypeExpressions from './rehype-expressions.js'; -import rehypeIslands from './rehype-islands.js'; -import rehypeJsx from './rehype-jsx.js'; import toRemarkContentRelImageError from './remark-content-rel-image-error.js'; -import remarkEscape from './remark-escape.js'; -import remarkMarkAndUnravel from './remark-mark-and-unravel.js'; -import remarkMdxish from './remark-mdxish.js'; import remarkPrism from './remark-prism.js'; import scopedStyles from './remark-scoped-styles.js'; import remarkShiki from './remark-shiki.js'; -import remarkUnwrap from './remark-unwrap.js'; import rehypeRaw from 'rehype-raw'; import rehypeStringify from 'rehype-stringify'; @@ -61,7 +53,6 @@ export async function renderMarkdown( remarkRehype = markdownConfigDefaults.remarkRehype, gfm = markdownConfigDefaults.gfm, smartypants = markdownConfigDefaults.smartypants, - isAstroFlavoredMd = false, isExperimentalContentCollections = false, contentDir, frontmatter: userFrontmatter = {}, @@ -72,7 +63,7 @@ export async function renderMarkdown( let parser = unified() .use(markdown) .use(toRemarkInitializeAstroData({ userFrontmatter })) - .use(isAstroFlavoredMd ? [remarkMdxish, remarkMarkAndUnravel, remarkUnwrap, remarkEscape] : []); + .use([]); if (gfm) { parser.use(remarkGfm); @@ -109,15 +100,7 @@ export async function renderMarkdown( markdownToHtml as any, { allowDangerousHtml: true, - passThrough: isAstroFlavoredMd - ? [ - 'raw', - 'mdxFlowExpression', - 'mdxJsxFlowElement', - 'mdxJsxTextElement', - 'mdxTextExpression', - ] - : [], + passThrough: [], ...remarkRehype, }, ], @@ -127,13 +110,7 @@ export async function renderMarkdown( parser.use([[plugin, pluginOpts]]); }); - parser - .use( - isAstroFlavoredMd - ? [rehypeJsx, rehypeExpressions, rehypeEscape, rehypeIslands, rehypeHeadingIds] - : [rehypeHeadingIds, rehypeRaw] - ) - .use(rehypeStringify, { allowDangerousHtml: true }); + parser.use([rehypeHeadingIds, rehypeRaw]).use(rehypeStringify, { allowDangerousHtml: true }); let vfile: MarkdownVFile; try { diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts index d5133aaf0e89..40b2ac841947 100644 --- a/packages/markdown/remark/src/types.ts +++ b/packages/markdown/remark/src/types.ts @@ -58,7 +58,6 @@ export interface MarkdownRenderingOptions extends AstroMarkdownOptions { $?: { scopedClassName: string | null; }; - isAstroFlavoredMd?: boolean; /** Used to prevent relative image imports from `src/content/` */ isExperimentalContentCollections?: boolean; /** Used to prevent relative image imports from `src/content/` */ From f39c47722c83931f793b6c15d163d40f7055b8e6 Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Fri, 6 Jan 2023 19:32:22 +0100 Subject: [PATCH 02/10] Removed AFMD remark & rehype plugins --- .../markdown/remark/src/mdast-util-mdxish.ts | 12 ---- packages/markdown/remark/src/mdxjs.ts | 27 ------- packages/markdown/remark/src/rehype-escape.ts | 22 ------ .../markdown/remark/src/rehype-expressions.ts | 18 ----- .../markdown/remark/src/rehype-islands.ts | 43 ----------- packages/markdown/remark/src/rehype-jsx.ts | 65 ----------------- packages/markdown/remark/src/remark-escape.ts | 15 ---- .../remark/src/remark-mark-and-unravel.ts | 72 ------------------- packages/markdown/remark/src/remark-mdxish.ts | 61 ---------------- packages/markdown/remark/src/remark-unwrap.ts | 44 ------------ 10 files changed, 379 deletions(-) delete mode 100644 packages/markdown/remark/src/mdast-util-mdxish.ts delete mode 100644 packages/markdown/remark/src/mdxjs.ts delete mode 100644 packages/markdown/remark/src/rehype-escape.ts delete mode 100644 packages/markdown/remark/src/rehype-expressions.ts delete mode 100644 packages/markdown/remark/src/rehype-islands.ts delete mode 100644 packages/markdown/remark/src/rehype-jsx.ts delete mode 100644 packages/markdown/remark/src/remark-escape.ts delete mode 100644 packages/markdown/remark/src/remark-mark-and-unravel.ts delete mode 100644 packages/markdown/remark/src/remark-mdxish.ts delete mode 100644 packages/markdown/remark/src/remark-unwrap.ts diff --git a/packages/markdown/remark/src/mdast-util-mdxish.ts b/packages/markdown/remark/src/mdast-util-mdxish.ts deleted file mode 100644 index 10b19f5be0c9..000000000000 --- a/packages/markdown/remark/src/mdast-util-mdxish.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { mdxExpressionFromMarkdown, mdxExpressionToMarkdown } from 'mdast-util-mdx-expression'; -import { mdxJsxFromMarkdown, mdxJsxToMarkdown } from 'mdast-util-mdx-jsx'; - -export function mdxFromMarkdown(): any { - return [mdxExpressionFromMarkdown, mdxJsxFromMarkdown]; -} - -export function mdxToMarkdown(): any { - return { - extensions: [mdxExpressionToMarkdown, mdxJsxToMarkdown], - }; -} diff --git a/packages/markdown/remark/src/mdxjs.ts b/packages/markdown/remark/src/mdxjs.ts deleted file mode 100644 index c3a9849ea97a..000000000000 --- a/packages/markdown/remark/src/mdxjs.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Note: The code in this file is based on `micromark-extension-mdxjs` -// and was adapted to use our fork `@astrojs/micromark-extension-mdx-jsx` -// instead of `micromark-extension-mdx-jsx` to allow some extended syntax. -// See `@astrojs/micromark-extension-mdx-jsx` on NPM for more details. -// Also, support for ESM imports & exports in Markdown content was removed. - -import { mdxJsx } from '@astrojs/micromark-extension-mdx-jsx'; -import { Parser } from 'acorn'; -import acornJsx from 'acorn-jsx'; -import type { Options } from 'micromark-extension-mdx-expression'; -import { mdxExpression } from 'micromark-extension-mdx-expression'; -import { mdxMd } from 'micromark-extension-mdx-md'; -import { combineExtensions } from 'micromark-util-combine-extensions'; -import type { Extension } from 'micromark-util-types'; - -export function mdxjs(options: Options): Extension { - const settings: any = Object.assign( - { - acorn: Parser.extend(acornJsx()), - acornOptions: { ecmaVersion: 2020, sourceType: 'module' }, - addResult: true, - }, - options - ); - - return combineExtensions([mdxExpression(settings), mdxJsx(settings), mdxMd]); -} diff --git a/packages/markdown/remark/src/rehype-escape.ts b/packages/markdown/remark/src/rehype-escape.ts deleted file mode 100644 index a4cc32cf724a..000000000000 --- a/packages/markdown/remark/src/rehype-escape.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { SKIP, visit } from 'unist-util-visit'; - -export function escapeEntities(value: string): string { - return value.replace(/&/g, '&').replace(//g, '>'); -} - -export default function rehypeEscape(): any { - return function (node: any): any { - return visit(node, 'element', (el) => { - if (el.tagName === 'code' || el.tagName === 'pre') { - el.properties['is:raw'] = true; - // Visit all raw children and escape HTML tags to prevent Markdown code - // like "This is a `