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

Remove Astro-flavored Markdown from @astrojs/markdown-remark #5785

Merged
merged 11 commits into from
Jan 9, 2023
1 change: 0 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 0 additions & 10 deletions packages/markdown/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@
"test": "mocha --exit --timeout 20000"
},
"dependencies": {
"@astrojs/micromark-extension-mdx-jsx": "^1.0.3",
"@astrojs/prism": "^1.0.0",
"acorn": "^8.7.1",
"acorn-jsx": "^5.3.2",
"github-slugger": "^1.4.0",
"hast-util-to-html": "^8.0.3",
"import-meta-resolve": "^2.1.0",
"mdast-util-from-markdown": "^1.2.0",
"mdast-util-mdx-expression": "^1.2.1",
"mdast-util-mdx-jsx": "^1.2.0",
"micromark-extension-mdx-expression": "^1.0.3",
"micromark-extension-mdx-md": "^1.0.0",
"micromark-util-combine-extensions": "^1.0.0",
"rehype-raw": "^6.1.1",
"rehype-stringify": "^9.0.3",
"remark-gfm": "^3.0.1",
Expand All @@ -46,7 +38,6 @@
"remark-smartypants": "^2.0.0",
"shiki": "^0.11.1",
"unified": "^10.1.2",
"unist-util-map": "^3.1.1",
"unist-util-visit": "^4.1.0",
"vfile": "^5.3.2"
},
Expand All @@ -59,7 +50,6 @@
"@types/unist": "^2.0.6",
"astro-scripts": "workspace:*",
"chai": "^4.3.6",
"micromark-util-types": "^1.0.2",
"mocha": "^9.2.2"
}
}
29 changes: 3 additions & 26 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,7 +53,6 @@ export async function renderMarkdown(
remarkRehype = markdownConfigDefaults.remarkRehype,
gfm = markdownConfigDefaults.gfm,
smartypants = markdownConfigDefaults.smartypants,
isAstroFlavoredMd = false,
isExperimentalContentCollections = false,
contentDir,
frontmatter: userFrontmatter = {},
Expand All @@ -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);
Expand Down Expand Up @@ -109,15 +100,7 @@ export async function renderMarkdown(
markdownToHtml as any,
{
allowDangerousHtml: true,
passThrough: isAstroFlavoredMd
? [
'raw',
'mdxFlowExpression',
'mdxJsxFlowElement',
'mdxJsxTextElement',
'mdxTextExpression',
]
: [],
passThrough: [],
...remarkRehype,
},
],
Expand All @@ -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 {
Expand Down
12 changes: 0 additions & 12 deletions packages/markdown/remark/src/mdast-util-mdxish.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/markdown/remark/src/mdxjs.ts

This file was deleted.

21 changes: 3 additions & 18 deletions packages/markdown/remark/src/rehype-collect-headings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
const depth = Number.parseInt(level);

let text = '';
let isJSX = false;
visit(node, (child, __, parent) => {
if (child.type === 'element' || parent == null) {
return;
Expand All @@ -36,31 +35,17 @@ export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
text += child.value;
} else {
text += child.value.replace(/\{/g, '${');
isJSX = isJSX || child.value.includes('{');
}
}
});

node.properties = node.properties || {};
if (typeof node.properties.id !== 'string') {
if (isJSX) {
// HACK: serialized JSX from internal plugins, ignore these for slug
const raw = toHtml(node.children, { allowDangerousHtml: true })
.replace(/\n(<)/g, '<')
.replace(/(>)\n/g, '>');
// HACK: for ids that have JSX content, use $$slug helper to generate slug at runtime
node.properties.id = `$$slug(\`${text}\`)`;
(node as any).type = 'raw';
(
node as any
).value = `<${node.tagName} id={${node.properties.id}}>${raw}</${node.tagName}>`;
} else {
let slug = slugger.slug(text);
let slug = slugger.slug(text);

if (slug.endsWith('-')) slug = slug.slice(0, -1);
if (slug.endsWith('-')) slug = slug.slice(0, -1);

node.properties.id = slug;
}
node.properties.id = slug;
}

headings.push({ depth, slug: node.properties.id, text });
Expand Down
22 changes: 0 additions & 22 deletions packages/markdown/remark/src/rehype-escape.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/markdown/remark/src/rehype-expressions.ts

This file was deleted.

43 changes: 0 additions & 43 deletions packages/markdown/remark/src/rehype-islands.ts

This file was deleted.

65 changes: 0 additions & 65 deletions packages/markdown/remark/src/rehype-jsx.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/markdown/remark/src/remark-escape.ts

This file was deleted.

Loading