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 3 commits
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
43 changes: 5 additions & 38 deletions packages/astro-prism/Prism.astro
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
---

<pre class={[className, classLanguage].join(' ')}><code class={classLanguage}><Fragment set:html={html} /></code></pre>
<pre class={[className, classLanguage].join(' ')}>
<code class={classLanguage}><Fragment set:html={html} /></code>
</pre>
31 changes: 31 additions & 0 deletions packages/astro-prism/README.md
Original file line number Diff line number Diff line change
@@ -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"
---

<Prism lang="js" code={`const foo = 'bar';`} />
```

## 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!';
---

<div>{helloAstro}</div>
`, 'astro');
```
1 change: 0 additions & 1 deletion packages/astro-prism/index.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/astro-prism/internal.d.ts

This file was deleted.

26 changes: 19 additions & 7 deletions packages/astro-prism/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/astro-prism/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-expect-error
export { default as Prism } from '../Prism.astro';
Original file line number Diff line number Diff line change
@@ -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.'
);
Expand All @@ -19,11 +23,7 @@ export function addAstro(Prism) {
let braces = /(?:\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])*\})/.source;
let spread = /(?:\{<S>*\.{3}(?:[^{}]|<BRACES>)*\})/.source;

/**
* @param {string} source
* @param {string} [flags]
*/
function re(source, flags) {
function re(source: string, flags?: string) {
source = source
.replace(/<S>/g, function () {
return space;
Expand All @@ -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.:-]+(?:<S>+(?:[\w.:$-]+(?:=(?:"(?:\\[^]|[^\\"])*"|'(?:\\[^]|[^\\'])*'|[^\s{'"/>=]+|<BRACES>))?|<SPREAD>))*<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',
Expand All @@ -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(
Expand All @@ -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 '';
}
Expand All @@ -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];

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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']]);
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved

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 };
}
10 changes: 10 additions & 0 deletions packages/astro-prism/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src"],
"compilerOptions": {
"allowJs": true,
"target": "ES2020",
"module": "ES2020",
"outDir": "./dist"
}
}
2 changes: 0 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -124,7 +123,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",
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 2 additions & 44 deletions packages/integrations/mdx/src/remark-prism.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,14 @@
// 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) =>
visit(tree, 'code', (node: any) => {
let { lang, value } = node;
node.type = 'html';

let { html, classLanguage } = runHighlighter(lang, value);
let { html, classLanguage } = runHighlighterWithAstro(lang, value);
let classes = [classLanguage];
node.value = `<pre class="${classes.join(
' '
Expand Down
2 changes: 0 additions & 2 deletions packages/markdown/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"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",
Expand All @@ -55,7 +54,6 @@
"@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": "workspace:*",
"chai": "^4.3.6",
Expand Down
Loading