From 64432bcb873efd0e4297c00fc9583a1fe516dfe7 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 2 Aug 2022 15:53:18 -0400 Subject: [PATCH] Refactor @astrojs/prism, fix Prism component import not working (#4114) * Upgrade @astrojs/prism to a real package, fix component import not working * Remove `@astrojs/prism` as a dependency of `astro` * Update lock file * Refactor to multiple files * Oops, can't have astro imports run inside node * Follow Nate's suggestion on being minors instead of patchs * Update lockfile --- .changeset/slow-spiders-fly.md | 8 ++++ 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 | 28 +++++++---- packages/astro-prism/src/highlighter.ts | 42 +++++++++++++++++ packages/astro-prism/src/index.ts | 2 + .../{internal.mjs => src/plugin.ts} | 37 ++++++++------- packages/astro-prism/tsconfig.json | 10 ++++ packages/astro/package.json | 2 - packages/integrations/mdx/package.json | 2 - 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 | 20 +++----- 17 files changed, 149 insertions(+), 175 deletions(-) create mode 100644 .changeset/slow-spiders-fly.md 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/highlighter.ts create mode 100644 packages/astro-prism/src/index.ts rename packages/astro-prism/{internal.mjs => src/plugin.ts} (83%) create mode 100644 packages/astro-prism/tsconfig.json diff --git a/.changeset/slow-spiders-fly.md b/.changeset/slow-spiders-fly.md new file mode 100644 index 000000000..1bd6cfa73 --- /dev/null +++ b/.changeset/slow-spiders-fly.md @@ -0,0 +1,8 @@ +--- +'astro': 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 diff --git a/packages/astro-prism/Prism.astro b/packages/astro-prism/Prism.astro index 2af5f64ab..81bd6cfd0 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/highlighter'; 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 000000000..1e8e92fa2
--- /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"
+---
+
+ {
- 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 7150edbe7..b385e82a5 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 8dc64c9fe..94fd2d432 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
@@ -503,7 +502,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
@@ -529,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
@@ -566,7 +563,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 +613,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:
@@ -2186,10 +2187,8 @@ 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
- prismjs: ^1.28.0
reading-time: ^1.5.0
rehype-raw: ^6.1.1
remark-frontmatter: ^4.0.1
@@ -2208,8 +2207,6 @@ importers:
es-module-lexer: 0.10.5
github-slugger: 1.4.0
gray-matter: 4.0.3
- mdast-util-mdx: 2.0.0
- prismjs: 1.28.0
rehype-raw: 6.1.1
remark-frontmatter: 4.0.1
remark-gfm: 3.0.1
@@ -2613,7 +2610,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
@@ -2629,7 +2625,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
@@ -2654,7 +2649,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
@@ -2672,7 +2666,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
@@ -14525,6 +14518,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==}