diff options
Diffstat (limited to '')
-rw-r--r-- | packages/astro-prism/src/plugin.ts (renamed from packages/astro-prism/internal.mjs) | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/packages/astro-prism/internal.mjs b/packages/astro-prism/src/plugin.ts index 22a5f9d48..cbee66c33 100644 --- a/packages/astro-prism/internal.mjs +++ b/packages/astro-prism/src/plugin.ts @@ -1,13 +1,14 @@ -export function addAstro(Prism) { +export function addAstro(Prism: typeof import('prismjs')) { 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 +20,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; @@ -40,16 +37,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', @@ -60,7 +59,7 @@ export function addAstro(Prism) { inside: Prism.languages.astro, }, }, - Prism.languages.astro.tag + (Prism.languages.astro as any).tag ); Prism.languages.insertBefore( @@ -80,11 +79,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 +96,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 +168,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,7 +178,7 @@ export function addAstro(Prism) { } }; - Prism.hooks.add('after-tokenize', function (env) { + Prism.hooks.add('after-tokenize', function (env: any) { if (env.language !== 'astro') { return; } |