From 274ad9c0c084b39e35575731a71c8b7c9e5b674f Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Mon, 1 Aug 2022 16:24:54 -0400 Subject: [PATCH 1/7] Upgrade @astrojs/prism to a real package, fix component import not working --- packages/astro-prism/Prism.astro | 43 ++-------- packages/astro-prism/README.md | 31 ++++++++ packages/astro-prism/index.js | 1 - packages/astro-prism/internal.d.ts | 1 - packages/astro-prism/package.json | 26 ++++-- packages/astro-prism/src/index.ts | 2 + .../{internal.mjs => src/internal.ts} | 79 ++++++++++++++----- packages/astro-prism/tsconfig.json | 10 +++ packages/astro/package.json | 1 - packages/integrations/mdx/package.json | 1 - packages/integrations/mdx/src/remark-prism.ts | 46 +---------- packages/markdown/remark/package.json | 2 - packages/markdown/remark/src/remark-prism.ts | 45 +---------- packages/webapi/mod.d.ts | 4 +- pnpm-lock.yaml | 16 ++-- 15 files changed, 140 insertions(+), 168 deletions(-) create mode 100644 packages/astro-prism/README.md delete mode 100644 packages/astro-prism/index.js delete mode 100644 packages/astro-prism/internal.d.ts create mode 100644 packages/astro-prism/src/index.ts rename packages/astro-prism/{internal.mjs => src/internal.ts} (70%) create mode 100644 packages/astro-prism/tsconfig.json diff --git a/packages/astro-prism/Prism.astro b/packages/astro-prism/Prism.astro index 2af5f64ab0b6..a92d50a83b94 100644 --- a/packages/astro-prism/Prism.astro +++ b/packages/astro-prism/Prism.astro @@ -1,7 +1,5 @@ --- -import Prism from 'prismjs'; -import { addAstro } from './internal.mjs'; -import loadLanguages from 'prismjs/components/index.js'; +import { runHighlighterWithAstro } from './dist/internal'; export interface Props { class?: string; @@ -10,40 +8,9 @@ export interface Props { } const { class: className, lang, code } = Astro.props as Props; - -let classLanguage = `language-${lang}`; - -const languageMap = new Map([['ts', 'typescript']]); - -if (lang == null) { - console.warn('Prism.astro: No language provided.'); -} - -const ensureLoaded = (lang) => { - if (lang && !Prism.languages[lang]) { - loadLanguages([lang]); - } -}; - -if (languageMap.has(lang)) { - ensureLoaded(languageMap.get(lang)); -} else if (lang === 'astro') { - ensureLoaded('typescript'); - addAstro(Prism); -} else { - ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs - ensureLoaded(lang); -} - -if (lang && !Prism.languages[lang]) { - console.warn(`Unable to load the language: ${lang}`); -} - -const grammar = Prism.languages[lang]; -let html = code; -if (grammar) { - html = Prism.highlight(code, grammar, lang); -} +const { classLanguage, html } = runHighlighterWithAstro(lang, code) --- -
+
+	
+
diff --git a/packages/astro-prism/README.md b/packages/astro-prism/README.md new file mode 100644 index 000000000000..e9bcbdbcde72 --- /dev/null +++ b/packages/astro-prism/README.md @@ -0,0 +1,31 @@ +# @astrojs/prism + +Supports Prism highlighting in Astro projects + +## Component + +This package exports a component to support highlighting inside an Astro file. Example: + +```astro +--- +import { Prism } from "@astrojs/prism" +--- + + +``` + +## Internal + +This package exports a `runHighlighterWithAstro` function inside `internal.ts` to make sure the Astro language is loaded when highlighting code + +```typescript +import { runHighlighterWithAstro } from '@astrojs/prism/dist/internal'; + +runHighlighterWithAstro(` + --- + const helloAstro = 'Hello, Astro!'; + --- + +
{helloAstro}
+`, 'astro'); +``` diff --git a/packages/astro-prism/index.js b/packages/astro-prism/index.js deleted file mode 100644 index 99293ed80564..000000000000 --- a/packages/astro-prism/index.js +++ /dev/null @@ -1 +0,0 @@ -export { default as Prism } from './Prism.astro'; diff --git a/packages/astro-prism/internal.d.ts b/packages/astro-prism/internal.d.ts deleted file mode 100644 index 058e91da7ea8..000000000000 --- a/packages/astro-prism/internal.d.ts +++ /dev/null @@ -1 +0,0 @@ -export function addAstro(Prism: any): void; diff --git a/packages/astro-prism/package.json b/packages/astro-prism/package.json index 853417165e5c..121ee3cbfff0 100644 --- a/packages/astro-prism/package.json +++ b/packages/astro-prism/package.json @@ -3,6 +3,7 @@ "version": "0.6.1", "description": "Supports Prism highlighting in Astro projects", "author": "withastro", + "type": "module", "license": "MIT", "bugs": "https://github.com/withastro/astro/issues", "repository": { @@ -11,17 +12,28 @@ "directory": "packages/astro-prism" }, "homepage": "https://astro.build", - "main": "index.js", - "scripts": {}, + "main": "dist/index.js", + "scripts": { + "build": "astro-scripts build \"src/**/*.ts\" && tsc", + "build:ci": "astro-scripts build \"src/**/*.ts\"", + "dev": "astro-scripts dev \"src/**/*.ts\"" + }, "exports": { - ".": "./index.js", - "./internal": "./internal.mjs" + ".": "./dist/index.js", + "./dist/internal": "./dist/internal.js", + "./Prism.astro": "./Prism.astro" }, - "types": "./internal.d.ts", - "keywords": [], - "devDependencies": { + "keywords": [ + "astro", + "astro-component" + ], + "dependencies": { "prismjs": "^1.28.0" }, + "devDependencies": { + "astro-scripts": "workspace:*", + "@types/prismjs": "1.26.0" + }, "engines": { "node": "^14.18.0 || >=16.12.0" } diff --git a/packages/astro-prism/src/index.ts b/packages/astro-prism/src/index.ts new file mode 100644 index 000000000000..f1dc0d801e2b --- /dev/null +++ b/packages/astro-prism/src/index.ts @@ -0,0 +1,2 @@ +// @ts-expect-error +export { default as Prism } from '../Prism.astro'; diff --git a/packages/astro-prism/internal.mjs b/packages/astro-prism/src/internal.ts similarity index 70% rename from packages/astro-prism/internal.mjs rename to packages/astro-prism/src/internal.ts index 22a5f9d4808a..93cdf66401c5 100644 --- a/packages/astro-prism/internal.mjs +++ b/packages/astro-prism/src/internal.ts @@ -1,13 +1,17 @@ -export function addAstro(Prism) { +import Prism from 'prismjs'; +import loadLanguages from 'prismjs/components/index.js'; + +function addAstro() { if (Prism.languages.astro) { return; } - let scriptLang; + let scriptLang: string; if (Prism.languages.typescript) { scriptLang = 'typescript'; } else { scriptLang = 'javascript'; + // eslint-disable-next-line no-console console.warn( 'Prism TypeScript language not loaded, Astro scripts will be treated as JavaScript.' ); @@ -19,11 +23,7 @@ export function addAstro(Prism) { let braces = /(?:\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])*\})/.source; let spread = /(?:\{*\.{3}(?:[^{}]|)*\})/.source; - /** - * @param {string} source - * @param {string} [flags] - */ - function re(source, flags) { + function re(source: string, flags?: string) { source = source .replace(//g, function () { return space; @@ -40,16 +40,18 @@ export function addAstro(Prism) { spread = re(spread).source; Prism.languages.astro = Prism.languages.extend('markup', script); - Prism.languages.astro.tag.pattern = re( + + (Prism.languages.astro as any).tag.pattern = re( /<\/?(?:[\w.:-]+(?:+(?:[\w.:$-]+(?:=(?:"(?:\\[^]|[^\\"])*"|'(?:\\[^]|[^\\'])*'|[^\s{'"/>=]+|))?|))**\/?)?>/ .source ); - Prism.languages.astro.tag.inside['tag'].pattern = /^<\/?[^\s>\/]*/i; - Prism.languages.astro.tag.inside['attr-value'].pattern = + (Prism.languages.astro as any).tag.inside['tag'].pattern = /^<\/?[^\s>\/]*/i; + (Prism.languages.astro as any).tag.inside['attr-value'].pattern = /=(?!\{)(?:"(?:\\[^]|[^\\"])*"|'(?:\\[^]|[^\\'])*'|[^\s'">]+)/i; - Prism.languages.astro.tag.inside['tag'].inside['class-name'] = /^[A-Z]\w*(?:\.[A-Z]\w*)*$/; - Prism.languages.astro.tag.inside['comment'] = script['comment']; + (Prism.languages.astro as any).tag.inside['tag'].inside['class-name'] = + /^[A-Z]\w*(?:\.[A-Z]\w*)*$/; + (Prism.languages.astro as any).tag.inside['comment'] = script['comment']; Prism.languages.insertBefore( 'inside', @@ -60,7 +62,7 @@ export function addAstro(Prism) { inside: Prism.languages.astro, }, }, - Prism.languages.astro.tag + (Prism.languages.astro as any).tag ); Prism.languages.insertBefore( @@ -80,11 +82,11 @@ export function addAstro(Prism) { alias: `language-${scriptLang}`, }, }, - Prism.languages.astro.tag + (Prism.languages.astro as any).tag ); // The following will handle plain text inside tags - let stringifyToken = function (token) { + let stringifyToken = function (token: any) { if (!token) { return ''; } @@ -97,8 +99,8 @@ export function addAstro(Prism) { return token.content.map(stringifyToken).join(''); }; - let walkTokens = function (tokens) { - let openedTags = []; + let walkTokens = function (tokens: any) { + let openedTags: any[] = []; for (let i = 0; i < tokens.length; i++) { let token = tokens[i]; @@ -169,7 +171,7 @@ export function addAstro(Prism) { i--; } - tokens[i] = new Prism.Token('plain-text', plainText, null, plainText); + tokens[i] = new Prism.Token('plain-text', plainText, undefined, plainText); } } @@ -179,10 +181,49 @@ export function addAstro(Prism) { } }; - Prism.hooks.add('after-tokenize', function (env) { + Prism.hooks.add('after-tokenize', function (env: any) { if (env.language !== 'astro') { return; } walkTokens(env.tokens); }); } + +const languageMap = new Map([['ts', 'typescript']]); + +export function runHighlighterWithAstro(lang: string | undefined, code: string) { + let classLanguage = `language-${lang}`; + + if (!lang) { + lang = 'plaintext'; + } + + const ensureLoaded = (language: string) => { + if (language && !Prism.languages[language]) { + loadLanguages([language]); + } + }; + + if (languageMap.has(lang)) { + ensureLoaded(languageMap.get(lang)!); + } else if (lang === 'astro') { + ensureLoaded('typescript'); + addAstro(); + } else { + ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs + ensureLoaded(lang); + } + + if (lang && !Prism.languages[lang]) { + // eslint-disable-next-line no-console + console.warn(`Unable to load the language: ${lang}`); + } + + const grammar = Prism.languages[lang]; + let html = code; + if (grammar) { + html = Prism.highlight(code, grammar, lang); + } + + return { classLanguage, html }; +} diff --git a/packages/astro-prism/tsconfig.json b/packages/astro-prism/tsconfig.json new file mode 100644 index 000000000000..c56abb57e775 --- /dev/null +++ b/packages/astro-prism/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.base.json", + "include": ["src"], + "compilerOptions": { + "allowJs": true, + "target": "ES2020", + "module": "ES2020", + "outDir": "./dist" + } +} diff --git a/packages/astro/package.json b/packages/astro/package.json index 157dc28a352a..a13095705f49 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -124,7 +124,6 @@ "postcss": "^8.4.14", "postcss-load-config": "^3.1.4", "preferred-pm": "^3.0.3", - "prismjs": "^1.28.0", "prompts": "^2.4.2", "recast": "^0.20.5", "rehype": "^12.0.1", diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json index d87529af2b49..35ca33ae281e 100644 --- a/packages/integrations/mdx/package.json +++ b/packages/integrations/mdx/package.json @@ -35,7 +35,6 @@ "@mdx-js/rollup": "^2.1.1", "es-module-lexer": "^0.10.5", "gray-matter": "^4.0.3", - "prismjs": "^1.28.0", "rehype-raw": "^6.1.1", "remark-frontmatter": "^4.0.1", "remark-gfm": "^3.0.1", diff --git a/packages/integrations/mdx/src/remark-prism.ts b/packages/integrations/mdx/src/remark-prism.ts index 4a324dd1d4c8..b3f695a276bb 100644 --- a/packages/integrations/mdx/src/remark-prism.ts +++ b/packages/integrations/mdx/src/remark-prism.ts @@ -1,48 +1,6 @@ -// TODO: discuss extracting this file to @astrojs/prism -import { addAstro } from '@astrojs/prism/internal'; -import Prism from 'prismjs'; -import loadLanguages from 'prismjs/components/index.js'; +import { runHighlighterWithAstro } from '@astrojs/prism/dist/internal'; import { visit } from 'unist-util-visit'; -const languageMap = new Map([['ts', 'typescript']]); - -function runHighlighter(lang: string, code: string) { - let classLanguage = `language-${lang}`; - - if (lang == null) { - lang = 'plaintext'; - } - - const ensureLoaded = (language: string) => { - if (language && !Prism.languages[language]) { - loadLanguages([language]); - } - }; - - if (languageMap.has(lang)) { - ensureLoaded(languageMap.get(lang)!); - } else if (lang === 'astro') { - ensureLoaded('typescript'); - addAstro(Prism); - } else { - ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs - ensureLoaded(lang); - } - - if (lang && !Prism.languages[lang]) { - // eslint-disable-next-line no-console - console.warn(`Unable to load the language: ${lang}`); - } - - const grammar = Prism.languages[lang]; - let html = code; - if (grammar) { - html = Prism.highlight(code, grammar, lang); - } - - return { classLanguage, html }; -} - /** */ export default function remarkPrism() { return (tree: any) => @@ -50,7 +8,7 @@ export default function remarkPrism() { let { lang, value } = node; node.type = 'html'; - let { html, classLanguage } = runHighlighter(lang, value); + let { html, classLanguage } = runHighlighterWithAstro(lang, value); let classes = [classLanguage]; node.value = `
 {
-		if (language && !Prism.languages[language]) {
-			loadLanguages([language]);
-		}
-	};
-
-	if (languageMap.has(lang)) {
-		ensureLoaded(languageMap.get(lang)!);
-	} else if (lang === 'astro') {
-		ensureLoaded('typescript');
-		addAstro(Prism);
-	} else {
-		ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
-		ensureLoaded(lang);
-	}
-
-	if (lang && !Prism.languages[lang]) {
-		// eslint-disable-next-line no-console
-		console.warn(`Unable to load the language: ${lang}`);
-	}
-
-	const grammar = Prism.languages[lang];
-	let html = code;
-	if (grammar) {
-		html = Prism.highlight(code, grammar, lang);
-	}
-
-	return { classLanguage, html };
-}
-
 type MaybeString = string | null | undefined;
 
 /**  */
@@ -52,7 +11,7 @@ function transformer(className: MaybeString) {
 			let { lang, value } = node;
 			node.type = 'html';
 
-			let { html, classLanguage } = runHighlighter(lang, value);
+			let { html, classLanguage } = runHighlighterWithAstro(lang, value);
 			let classes = [classLanguage];
 			if (className) {
 				classes.push(className);
diff --git a/packages/webapi/mod.d.ts b/packages/webapi/mod.d.ts
index 7150edbe716e..b385e82a5e29 100644
--- a/packages/webapi/mod.d.ts
+++ b/packages/webapi/mod.d.ts
@@ -1,5 +1,5 @@
 export { pathToPosix } from './lib/utils';
-export { AbortController, AbortSignal, alert, atob, Blob, btoa, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, CanvasRenderingContext2D, CharacterData, clearTimeout, Comment, CountQueuingStrategy, CSSStyleSheet, CustomElementRegistry, CustomEvent, Document, DocumentFragment, DOMException, Element, Event, EventTarget, fetch, File, FormData, Headers, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLDocument, HTMLElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLSpanElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image, ImageData, IntersectionObserver, MediaQueryList, MutationObserver, Node, NodeFilter, NodeIterator, OffscreenCanvas, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, requestAnimationFrame, requestIdleCallback, ResizeObserver, Response, setTimeout, ShadowRoot, structuredClone, StyleSheet, Text, TransformStream, TreeWalker, URLPattern, Window, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter } from './mod.js';
+export { AbortController, AbortSignal, alert, atob, Blob, btoa, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, CanvasRenderingContext2D, CharacterData, clearTimeout, Comment, CountQueuingStrategy, CSSStyleSheet, CustomElementRegistry, CustomEvent, Document, DocumentFragment, DOMException, Element, Event, EventTarget, fetch, File, FormData, Headers, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLDocument, HTMLElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLSpanElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image, ImageData, IntersectionObserver, MediaQueryList, MutationObserver, Node, NodeFilter, NodeIterator, OffscreenCanvas, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, requestAnimationFrame, requestIdleCallback, ResizeObserver, Response, setTimeout, ShadowRoot, structuredClone, StyleSheet, Text, TransformStream, TreeWalker, URLPattern, Window, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter, } from './mod.js';
 export declare const polyfill: {
     (target: any, options?: PolyfillOptions): any;
     internals(target: any, name: string): any;
@@ -9,4 +9,4 @@ interface PolyfillOptions {
     override?: Record;
-}
+}
\ No newline at end of file
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index caaae4eb0a89..d00e2ade4175 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -503,7 +503,6 @@ importers:
       postcss: ^8.4.14
       postcss-load-config: ^3.1.4
       preferred-pm: ^3.0.3
-      prismjs: ^1.28.0
       prompts: ^2.4.2
       recast: ^0.20.5
       rehype: ^12.0.1
@@ -566,7 +565,6 @@ importers:
       postcss: 8.4.14
       postcss-load-config: 3.1.4_postcss@8.4.14
       preferred-pm: 3.0.3
-      prismjs: 1.28.0
       prompts: 2.4.2
       recast: 0.20.5
       rehype: 12.0.1
@@ -617,9 +615,14 @@ importers:
 
   packages/astro-prism:
     specifiers:
+      '@types/prismjs': 1.26.0
+      astro-scripts: workspace:*
       prismjs: ^1.28.0
-    devDependencies:
+    dependencies:
       prismjs: 1.28.0
+    devDependencies:
+      '@types/prismjs': 1.26.0
+      astro-scripts: link:../../scripts
 
   packages/astro-rss:
     specifiers:
@@ -2185,7 +2188,6 @@ importers:
       gray-matter: ^4.0.3
       linkedom: ^0.14.12
       mocha: ^9.2.2
-      prismjs: ^1.28.0
       rehype-raw: ^6.1.1
       remark-frontmatter: ^4.0.1
       remark-gfm: ^3.0.1
@@ -2201,7 +2203,6 @@ importers:
       '@mdx-js/rollup': 2.1.2
       es-module-lexer: 0.10.5
       gray-matter: 4.0.3
-      prismjs: 1.28.0
       rehype-raw: 6.1.1
       remark-frontmatter: 4.0.1
       remark-gfm: 3.0.1
@@ -2595,7 +2596,6 @@ importers:
       '@types/hast': ^2.3.4
       '@types/mdast': ^3.0.10
       '@types/mocha': ^9.1.1
-      '@types/prismjs': ^1.26.0
       '@types/unist': ^2.0.6
       acorn: ^8.7.1
       acorn-jsx: ^5.3.2
@@ -2611,7 +2611,6 @@ importers:
       micromark-util-combine-extensions: ^1.0.0
       micromark-util-types: ^1.0.2
       mocha: ^9.2.2
-      prismjs: ^1.28.0
       rehype-raw: ^6.1.1
       rehype-stringify: ^9.0.3
       remark-gfm: ^3.0.1
@@ -2636,7 +2635,6 @@ importers:
       micromark-extension-mdx-expression: 1.0.3
       micromark-extension-mdx-md: 1.0.0
       micromark-util-combine-extensions: 1.0.0
-      prismjs: 1.28.0
       rehype-raw: 6.1.1
       rehype-stringify: 9.0.3
       remark-gfm: 3.0.1
@@ -2654,7 +2652,6 @@ importers:
       '@types/hast': 2.3.4
       '@types/mdast': 3.0.10
       '@types/mocha': 9.1.1
-      '@types/prismjs': 1.26.0
       '@types/unist': 2.0.6
       astro-scripts: link:../../../scripts
       chai: 4.3.6
@@ -14507,6 +14504,7 @@ packages:
   /prismjs/1.28.0:
     resolution: {integrity: sha512-8aaXdYvl1F7iC7Xm1spqSaY/OJBpYW3v+KJ+F17iYxvdc8sfjW194COK5wVhMZX45tGteiBQgdvD/nhxcRwylw==}
     engines: {node: '>=6'}
+    dev: false
 
   /process-nextick-args/2.0.1:
     resolution: {integrity: sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==}

From 002e9f9cbac8b67b93e6d9bbbd09a2b1efdbf23f Mon Sep 17 00:00:00 2001
From: Princesseuh 
Date: Mon, 1 Aug 2022 16:32:08 -0400
Subject: [PATCH 2/7] Remove `@astrojs/prism` as a dependency of `astro`

---
 packages/astro/package.json | 1 -
 1 file changed, 1 deletion(-)

diff --git a/packages/astro/package.json b/packages/astro/package.json
index a13095705f49..19e707edb5ae 100644
--- a/packages/astro/package.json
+++ b/packages/astro/package.json
@@ -87,7 +87,6 @@
     "@astrojs/compiler": "^0.22.1",
     "@astrojs/language-server": "^0.20.0",
     "@astrojs/markdown-remark": "^0.13.0",
-    "@astrojs/prism": "0.6.1",
     "@astrojs/telemetry": "^0.4.1",
     "@astrojs/webapi": "^0.12.0",
     "@babel/core": "^7.18.2",

From 5cef99684ab0878d552f182e3bb91531abe0eec1 Mon Sep 17 00:00:00 2001
From: Princesseuh 
Date: Mon, 1 Aug 2022 16:34:28 -0400
Subject: [PATCH 3/7] Update lock file

---
 pnpm-lock.yaml | 2 --
 1 file changed, 2 deletions(-)

diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d00e2ade4175..d62691992a6a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -441,7 +441,6 @@ importers:
       '@astrojs/compiler': ^0.22.1
       '@astrojs/language-server': ^0.20.0
       '@astrojs/markdown-remark': ^0.13.0
-      '@astrojs/prism': 0.6.1
       '@astrojs/telemetry': ^0.4.1
       '@astrojs/webapi': ^0.12.0
       '@babel/core': ^7.18.2
@@ -528,7 +527,6 @@ importers:
       '@astrojs/compiler': 0.22.1
       '@astrojs/language-server': 0.20.3
       '@astrojs/markdown-remark': link:../markdown/remark
-      '@astrojs/prism': link:../astro-prism
       '@astrojs/telemetry': link:../telemetry
       '@astrojs/webapi': link:../webapi
       '@babel/core': 7.18.9

From 9046cecedc482ff8bf7aa24a2ab11606b4579b96 Mon Sep 17 00:00:00 2001
From: Princesseuh 
Date: Mon, 1 Aug 2022 17:31:19 -0400
Subject: [PATCH 4/7] Refactor to multiple files

---
 .changeset/slow-spiders-fly.md                |  8 ++++
 packages/astro-prism/Prism.astro              |  2 +-
 packages/astro-prism/README.md                |  4 +-
 packages/astro-prism/package.json             |  1 -
 packages/astro-prism/src/highlighter.ts       | 42 ++++++++++++++++++
 packages/astro-prism/src/index.ts             |  1 +
 .../src/{internal.ts => plugin.ts}            | 44 +------------------
 packages/integrations/mdx/src/remark-prism.ts |  2 +-
 packages/markdown/remark/src/remark-prism.ts  |  2 +-
 9 files changed, 57 insertions(+), 49 deletions(-)
 create mode 100644 .changeset/slow-spiders-fly.md
 create mode 100644 packages/astro-prism/src/highlighter.ts
 rename packages/astro-prism/src/{internal.ts => plugin.ts} (83%)

diff --git a/.changeset/slow-spiders-fly.md b/.changeset/slow-spiders-fly.md
new file mode 100644
index 000000000000..f78ca197f153
--- /dev/null
+++ b/.changeset/slow-spiders-fly.md
@@ -0,0 +1,8 @@
+---
+'astro': patch
+'@astrojs/prism': patch
+'@astrojs/mdx': patch
+'@astrojs/markdown-remark': patch
+---
+
+Refactor `@astrojs/mdx` and `@astrojs/markdown-remark` to use `@astrojs/prism` instead of duplicating the code
diff --git a/packages/astro-prism/Prism.astro b/packages/astro-prism/Prism.astro
index a92d50a83b94..7e83db7ae2b6 100644
--- a/packages/astro-prism/Prism.astro
+++ b/packages/astro-prism/Prism.astro
@@ -1,5 +1,5 @@
 ---
-import { runHighlighterWithAstro } from './dist/internal';
+import { runHighlighterWithAstro } from './dist/index';
 
 export interface Props {
 	class?: string;
diff --git a/packages/astro-prism/README.md b/packages/astro-prism/README.md
index e9bcbdbcde72..1e8e92fa21a4 100644
--- a/packages/astro-prism/README.md
+++ b/packages/astro-prism/README.md
@@ -16,10 +16,10 @@ import { Prism } from "@astrojs/prism"
 
 ## Internal
 
-This package exports a `runHighlighterWithAstro` function inside `internal.ts` to make sure the Astro language is loaded when highlighting code
+This package exports a `runHighlighterWithAstro` function to highlight while making sure the Astro language is loaded beforehand
 
 ```typescript
-import { runHighlighterWithAstro } from '@astrojs/prism/dist/internal';
+import { runHighlighterWithAstro } from '@astrojs/prism';
 
 runHighlighterWithAstro(`
   ---
diff --git a/packages/astro-prism/package.json b/packages/astro-prism/package.json
index 121ee3cbfff0..1aeaef356a14 100644
--- a/packages/astro-prism/package.json
+++ b/packages/astro-prism/package.json
@@ -20,7 +20,6 @@
   },
   "exports": {
     ".": "./dist/index.js",
-    "./dist/internal": "./dist/internal.js",
     "./Prism.astro": "./Prism.astro"
   },
   "keywords": [
diff --git a/packages/astro-prism/src/highlighter.ts b/packages/astro-prism/src/highlighter.ts
new file mode 100644
index 000000000000..3e651ceb2486
--- /dev/null
+++ b/packages/astro-prism/src/highlighter.ts
@@ -0,0 +1,42 @@
+import Prism from 'prismjs';
+import loadLanguages from 'prismjs/components/index.js';
+import { addAstro } from './plugin';
+
+const languageMap = new Map([['ts', 'typescript']]);
+
+export function runHighlighterWithAstro(lang: string | undefined, code: string) {
+	let classLanguage = `language-${lang}`;
+
+	if (!lang) {
+		lang = 'plaintext';
+	}
+
+	const ensureLoaded = (language: string) => {
+		if (language && !Prism.languages[language]) {
+			loadLanguages([language]);
+		}
+	};
+
+	if (languageMap.has(lang)) {
+		ensureLoaded(languageMap.get(lang)!);
+	} else if (lang === 'astro') {
+		ensureLoaded('typescript');
+		addAstro(Prism);
+	} else {
+		ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
+		ensureLoaded(lang);
+	}
+
+	if (lang && !Prism.languages[lang]) {
+		// eslint-disable-next-line no-console
+		console.warn(`Unable to load the language: ${lang}`);
+	}
+
+	const grammar = Prism.languages[lang];
+	let html = code;
+	if (grammar) {
+		html = Prism.highlight(code, grammar, lang);
+	}
+
+	return { classLanguage, html };
+}
diff --git a/packages/astro-prism/src/index.ts b/packages/astro-prism/src/index.ts
index f1dc0d801e2b..751304d27922 100644
--- a/packages/astro-prism/src/index.ts
+++ b/packages/astro-prism/src/index.ts
@@ -1,2 +1,3 @@
 // @ts-expect-error
 export { default as Prism } from '../Prism.astro';
+export { runHighlighterWithAstro } from './highlighter';
diff --git a/packages/astro-prism/src/internal.ts b/packages/astro-prism/src/plugin.ts
similarity index 83%
rename from packages/astro-prism/src/internal.ts
rename to packages/astro-prism/src/plugin.ts
index 93cdf66401c5..cbee66c3379a 100644
--- a/packages/astro-prism/src/internal.ts
+++ b/packages/astro-prism/src/plugin.ts
@@ -1,7 +1,4 @@
-import Prism from 'prismjs';
-import loadLanguages from 'prismjs/components/index.js';
-
-function addAstro() {
+export function addAstro(Prism: typeof import('prismjs')) {
 	if (Prism.languages.astro) {
 		return;
 	}
@@ -188,42 +185,3 @@ function addAstro() {
 		walkTokens(env.tokens);
 	});
 }
-
-const languageMap = new Map([['ts', 'typescript']]);
-
-export function runHighlighterWithAstro(lang: string | undefined, code: string) {
-	let classLanguage = `language-${lang}`;
-
-	if (!lang) {
-		lang = 'plaintext';
-	}
-
-	const ensureLoaded = (language: string) => {
-		if (language && !Prism.languages[language]) {
-			loadLanguages([language]);
-		}
-	};
-
-	if (languageMap.has(lang)) {
-		ensureLoaded(languageMap.get(lang)!);
-	} else if (lang === 'astro') {
-		ensureLoaded('typescript');
-		addAstro();
-	} else {
-		ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
-		ensureLoaded(lang);
-	}
-
-	if (lang && !Prism.languages[lang]) {
-		// eslint-disable-next-line no-console
-		console.warn(`Unable to load the language: ${lang}`);
-	}
-
-	const grammar = Prism.languages[lang];
-	let html = code;
-	if (grammar) {
-		html = Prism.highlight(code, grammar, lang);
-	}
-
-	return { classLanguage, html };
-}
diff --git a/packages/integrations/mdx/src/remark-prism.ts b/packages/integrations/mdx/src/remark-prism.ts
index b3f695a276bb..8b147ac1ace9 100644
--- a/packages/integrations/mdx/src/remark-prism.ts
+++ b/packages/integrations/mdx/src/remark-prism.ts
@@ -1,4 +1,4 @@
-import { runHighlighterWithAstro } from '@astrojs/prism/dist/internal';
+import { runHighlighterWithAstro } from '@astrojs/prism';
 import { visit } from 'unist-util-visit';
 
 /**  */
diff --git a/packages/markdown/remark/src/remark-prism.ts b/packages/markdown/remark/src/remark-prism.ts
index 6defc30fbed9..bfec8c84c293 100644
--- a/packages/markdown/remark/src/remark-prism.ts
+++ b/packages/markdown/remark/src/remark-prism.ts
@@ -1,4 +1,4 @@
-import { runHighlighterWithAstro } from '@astrojs/prism/dist/internal';
+import { runHighlighterWithAstro } from '@astrojs/prism';
 import { visit } from 'unist-util-visit';
 const noVisit = new Set(['root', 'html', 'text']);
 

From 066538cabfaac2bbf1ba3575f68a46b1b8c616d3 Mon Sep 17 00:00:00 2001
From: Princesseuh 
Date: Mon, 1 Aug 2022 17:41:49 -0400
Subject: [PATCH 5/7] Oops, can't have astro imports run inside node

---
 packages/astro-prism/Prism.astro              | 2 +-
 packages/astro-prism/package.json             | 3 ++-
 packages/astro-prism/src/highlighter.ts       | 2 +-
 packages/astro-prism/src/index.ts             | 1 -
 packages/integrations/mdx/src/remark-prism.ts | 2 +-
 packages/markdown/remark/src/remark-prism.ts  | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/packages/astro-prism/Prism.astro b/packages/astro-prism/Prism.astro
index 7e83db7ae2b6..81bd6cfd0001 100644
--- a/packages/astro-prism/Prism.astro
+++ b/packages/astro-prism/Prism.astro
@@ -1,5 +1,5 @@
 ---
-import { runHighlighterWithAstro } from './dist/index';
+import { runHighlighterWithAstro } from './dist/highlighter';
 
 export interface Props {
 	class?: string;
diff --git a/packages/astro-prism/package.json b/packages/astro-prism/package.json
index 1aeaef356a14..719776311457 100644
--- a/packages/astro-prism/package.json
+++ b/packages/astro-prism/package.json
@@ -20,7 +20,8 @@
   },
   "exports": {
     ".": "./dist/index.js",
-    "./Prism.astro": "./Prism.astro"
+    "./Prism.astro": "./Prism.astro",
+    "./dist/highlighter": "./dist/highlighter.js"
   },
   "keywords": [
     "astro",
diff --git a/packages/astro-prism/src/highlighter.ts b/packages/astro-prism/src/highlighter.ts
index 3e651ceb2486..3dffefae9582 100644
--- a/packages/astro-prism/src/highlighter.ts
+++ b/packages/astro-prism/src/highlighter.ts
@@ -1,6 +1,6 @@
 import Prism from 'prismjs';
 import loadLanguages from 'prismjs/components/index.js';
-import { addAstro } from './plugin';
+import { addAstro } from './plugin.js';
 
 const languageMap = new Map([['ts', 'typescript']]);
 
diff --git a/packages/astro-prism/src/index.ts b/packages/astro-prism/src/index.ts
index 751304d27922..f1dc0d801e2b 100644
--- a/packages/astro-prism/src/index.ts
+++ b/packages/astro-prism/src/index.ts
@@ -1,3 +1,2 @@
 // @ts-expect-error
 export { default as Prism } from '../Prism.astro';
-export { runHighlighterWithAstro } from './highlighter';
diff --git a/packages/integrations/mdx/src/remark-prism.ts b/packages/integrations/mdx/src/remark-prism.ts
index 8b147ac1ace9..7dc05f358421 100644
--- a/packages/integrations/mdx/src/remark-prism.ts
+++ b/packages/integrations/mdx/src/remark-prism.ts
@@ -1,4 +1,4 @@
-import { runHighlighterWithAstro } from '@astrojs/prism';
+import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter';
 import { visit } from 'unist-util-visit';
 
 /**  */
diff --git a/packages/markdown/remark/src/remark-prism.ts b/packages/markdown/remark/src/remark-prism.ts
index bfec8c84c293..80037a3e3737 100644
--- a/packages/markdown/remark/src/remark-prism.ts
+++ b/packages/markdown/remark/src/remark-prism.ts
@@ -1,4 +1,4 @@
-import { runHighlighterWithAstro } from '@astrojs/prism';
+import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter';
 import { visit } from 'unist-util-visit';
 const noVisit = new Set(['root', 'html', 'text']);
 

From e11582435d35dfa3d3e04c550fe71bd2947693bf Mon Sep 17 00:00:00 2001
From: Princesseuh 
Date: Tue, 2 Aug 2022 15:40:41 -0400
Subject: [PATCH 6/7] Follow Nate's suggestion on being minors instead of
 patchs

---
 .changeset/slow-spiders-fly.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/.changeset/slow-spiders-fly.md b/.changeset/slow-spiders-fly.md
index f78ca197f153..1bd6cfa73822 100644
--- a/.changeset/slow-spiders-fly.md
+++ b/.changeset/slow-spiders-fly.md
@@ -1,8 +1,8 @@
 ---
 'astro': patch
-'@astrojs/prism': patch
-'@astrojs/mdx': patch
-'@astrojs/markdown-remark': patch
+'@astrojs/prism': minor
+'@astrojs/mdx': minor
+'@astrojs/markdown-remark': minor
 ---
 
 Refactor `@astrojs/mdx` and `@astrojs/markdown-remark` to use `@astrojs/prism` instead of duplicating the code

From ac6d609f12da77544592627f90f504335990ad6f Mon Sep 17 00:00:00 2001
From: Princesseuh 
Date: Tue, 2 Aug 2022 15:45:23 -0400
Subject: [PATCH 7/7] Update lockfile

---
 pnpm-lock.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0504f098489d..94fd2d432833 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2187,9 +2187,9 @@ importers:
       github-slugger: ^1.4.0
       gray-matter: ^4.0.3
       linkedom: ^0.14.12
-      mdast-util-mdx: ^2.0.0
       mdast-util-to-string: ^3.1.0
       mocha: ^9.2.2
+      reading-time: ^1.5.0
       rehype-raw: ^6.1.1
       remark-frontmatter: ^4.0.1
       remark-gfm: ^3.0.1