Skip to content

Commit

Permalink
Merge branch 'main' into fix-md-c
Browse files Browse the repository at this point in the history
  • Loading branch information
potatowagon authored Sep 20, 2024
2 parents 2588ed5 + 4cf0a53 commit dc08b44
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 335 deletions.
2 changes: 1 addition & 1 deletion packages/lexical-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@types/prismjs": "^1.26.0"
},
"module": "LexicalCode.mjs",
"sideEffects": false,
"sideEffects": true,
"exports": {
".": {
"import": {
Expand Down
7 changes: 3 additions & 4 deletions packages/lexical-code/src/CodeHighlightNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import type {
TabNode,
} from 'lexical';

import './CodeHighlighterPrism';

import {
addClassNamesToElement,
removeClassNamesFromElement,
Expand All @@ -30,6 +28,7 @@ import {
TextNode,
} from 'lexical';

import {Prism} from './CodeHighlighterPrism';
import {$createCodeNode} from './CodeNode';

export const DEFAULT_CODE_LANGUAGE = 'javascript';
Expand Down Expand Up @@ -84,11 +83,11 @@ export function getLanguageFriendlyName(lang: string) {
export const getDefaultCodeLanguage = (): string => DEFAULT_CODE_LANGUAGE;

export const getCodeLanguages = (): Array<string> =>
Object.keys(window.Prism.languages)
Object.keys(Prism.languages)
.filter(
// Prism has several language helpers mixed into languages object
// so filtering them out here to get langs list
(language) => typeof window.Prism.languages[language] !== 'function',
(language) => typeof Prism.languages[language] !== 'function',
)
.sort();

Expand Down
8 changes: 3 additions & 5 deletions packages/lexical-code/src/CodeHighlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import type {
RangeSelection,
} from 'lexical';

import './CodeHighlighterPrism';

import {mergeRegister} from '@lexical/utils';
import {
$createLineBreakNode,
Expand All @@ -44,6 +42,7 @@ import {
} from 'lexical';
import invariant from 'shared/invariant';

import {Prism} from './CodeHighlighterPrism';
import {
$createCodeHighlightNode,
$isCodeHighlightNode,
Expand All @@ -69,10 +68,9 @@ export interface Tokenizer {
export const PrismTokenizer: Tokenizer = {
defaultLanguage: DEFAULT_CODE_LANGUAGE,
tokenize(code: string, language?: string): (string | Token)[] {
return window.Prism.tokenize(
return Prism.tokenize(
code,
window.Prism.languages[language || ''] ||
window.Prism.languages[this.defaultLanguage],
Prism.languages[language || ''] || Prism.languages[this.defaultLanguage],
);
},
};
Expand Down
11 changes: 1 addition & 10 deletions packages/lexical-code/src/CodeHighlighterPrism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,5 @@ import 'prismjs/components/prism-swift';
import 'prismjs/components/prism-typescript';
import 'prismjs/components/prism-java';
import 'prismjs/components/prism-cpp';
import {CAN_USE_DOM} from 'shared/canUseDOM';

declare global {
interface Window {
Prism: typeof import('prismjs');
}
}

export const Prism: typeof import('prismjs') = CAN_USE_DOM
? window.Prism
: (global as unknown as {Prism: typeof import('prismjs')}).Prism;
export const Prism: typeof import('prismjs') = globalThis.Prism || window.Prism;
Loading

0 comments on commit dc08b44

Please sign in to comment.