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

Refactor @astrojs/prism, fix Prism component import not working #4114

Merged
merged 8 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/slow-spiders-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'astro': patch
'@astrojs/prism': patch
'@astrojs/mdx': patch
'@astrojs/markdown-remark': patch
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
---

Refactor `@astrojs/mdx` and `@astrojs/markdown-remark` to use `@astrojs/prism` instead of duplicating the code
2 changes: 1 addition & 1 deletion packages/astro-prism/Prism.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { runHighlighterWithAstro } from './dist/internal';
import { runHighlighterWithAstro } from './dist/index';

export interface Props {
class?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-prism/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
---
Expand Down
1 change: 0 additions & 1 deletion packages/astro-prism/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
},
"exports": {
".": "./dist/index.js",
"./dist/internal": "./dist/internal.js",
"./Prism.astro": "./Prism.astro"
},
"keywords": [
Expand Down
42 changes: 42 additions & 0 deletions packages/astro-prism/src/highlighter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Prism from 'prismjs';
import loadLanguages from 'prismjs/components/index.js';
import { addAstro } from './plugin';
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved

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 };
}
1 change: 1 addition & 0 deletions packages/astro-prism/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// @ts-expect-error
export { default as Prism } from '../Prism.astro';
export { runHighlighterWithAstro } from './highlighter';
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down Expand Up @@ -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 };
}
2 changes: 1 addition & 1 deletion packages/integrations/mdx/src/remark-prism.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runHighlighterWithAstro } from '@astrojs/prism/dist/internal';
import { runHighlighterWithAstro } from '@astrojs/prism';
import { visit } from 'unist-util-visit';

/** */
Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/remark/src/remark-prism.ts
Original file line number Diff line number Diff line change
@@ -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']);

Expand Down