@lexical/code-shiki 0.48.0 → 0.48.1-nightly.20260720.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -63,26 +63,26 @@ function isCodeLanguageLoaded(language) {
|
|
|
63
63
|
function loadCodeLanguage(language, editor, codeNodeKey) {
|
|
64
64
|
const diffedLanguage = getDiffedLanguage(language);
|
|
65
65
|
const langId = diffedLanguage ? diffedLanguage : language;
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
});
|
|
66
|
+
if (isCodeLanguageLoaded(langId)) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const languageInfo = langs.bundledLanguagesInfo.find(desc => desc.id === langId || desc.aliases && desc.aliases.includes(langId));
|
|
70
|
+
if (!languageInfo) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
return shiki.loadLanguage(languageInfo.import()).then(() => {
|
|
74
|
+
if (editor && codeNodeKey) {
|
|
75
|
+
editor.update(() => {
|
|
76
|
+
const codeNode = lexical.$getNodeByKey(codeNodeKey);
|
|
77
|
+
if (codeCore.$isCodeNode(codeNode) && codeNode.getLanguage() === language) {
|
|
78
|
+
codeNode.setIsSyntaxHighlightSupported(true);
|
|
79
|
+
codeNode.markDirty();
|
|
82
80
|
}
|
|
81
|
+
}, {
|
|
82
|
+
tag: lexical.HISTORY_MERGE_TAG
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
|
-
}
|
|
85
|
+
});
|
|
86
86
|
}
|
|
87
87
|
function isCodeThemeLoaded(theme) {
|
|
88
88
|
const themeId = theme;
|
|
@@ -94,21 +94,25 @@ function isCodeThemeLoaded(theme) {
|
|
|
94
94
|
return shiki.getLoadedThemes().includes(themeId);
|
|
95
95
|
}
|
|
96
96
|
function loadCodeTheme(theme, editor, codeNodeKey) {
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
97
|
+
if (isCodeThemeLoaded(theme)) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const themeInfo = themes.bundledThemesInfo.find(info => info.id === theme);
|
|
101
|
+
if (!themeInfo) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
return shiki.loadTheme(themeInfo.import()).then(() => {
|
|
105
|
+
if (editor && codeNodeKey) {
|
|
106
|
+
editor.update(() => {
|
|
107
|
+
const codeNode = lexical.$getNodeByKey(codeNodeKey);
|
|
108
|
+
if (codeCore.$isCodeNode(codeNode)) {
|
|
109
|
+
codeNode.markDirty();
|
|
108
110
|
}
|
|
111
|
+
}, {
|
|
112
|
+
tag: lexical.HISTORY_MERGE_TAG
|
|
109
113
|
});
|
|
110
114
|
}
|
|
111
|
-
}
|
|
115
|
+
});
|
|
112
116
|
}
|
|
113
117
|
function getCodeLanguageOptions() {
|
|
114
118
|
return langs.bundledLanguagesInfo.map(i => [i.id, i.name]);
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import { $isCodeNode, $createCodeHighlightNode, DEFAULT_CODE_LANGUAGE, CodeExtension, CodeIndentExtension, $plainifyCodeContent, CodeNode, CodeHighlightNode, registerCodeIndentation, $isCodeHighlightNode } from '@lexical/code-core';
|
|
10
10
|
import { effect, namedSignals } from '@lexical/extension';
|
|
11
|
-
import { $getNodeByKey, $createLineBreakNode, tokenizeRawText, $createTabNode, defineExtension, safeCast, mergeRegister, TextNode, $isLineBreakNode, $onUpdate, $createTextNode, $getSelection, $isRangeSelection, $isTextNode, $isTabNode } from 'lexical';
|
|
11
|
+
import { $getNodeByKey, HISTORY_MERGE_TAG, $createLineBreakNode, tokenizeRawText, $createTabNode, defineExtension, safeCast, mergeRegister, TextNode, $isLineBreakNode, $onUpdate, $createTextNode, $getSelection, $isRangeSelection, $isTextNode, $isTabNode } from 'lexical';
|
|
12
12
|
import { createHighlighterCoreSync, isSpecialLang, isSpecialTheme, stringifyTokenStyle, getTokenStyleObject } from '@shikijs/core';
|
|
13
13
|
import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript';
|
|
14
14
|
import { bundledLanguagesInfo } from 'shiki/langs';
|
|
@@ -61,26 +61,26 @@ function isCodeLanguageLoaded(language) {
|
|
|
61
61
|
function loadCodeLanguage(language, editor, codeNodeKey) {
|
|
62
62
|
const diffedLanguage = getDiffedLanguage(language);
|
|
63
63
|
const langId = diffedLanguage ? diffedLanguage : language;
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
});
|
|
64
|
+
if (isCodeLanguageLoaded(langId)) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const languageInfo = bundledLanguagesInfo.find(desc => desc.id === langId || desc.aliases && desc.aliases.includes(langId));
|
|
68
|
+
if (!languageInfo) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return shiki.loadLanguage(languageInfo.import()).then(() => {
|
|
72
|
+
if (editor && codeNodeKey) {
|
|
73
|
+
editor.update(() => {
|
|
74
|
+
const codeNode = $getNodeByKey(codeNodeKey);
|
|
75
|
+
if ($isCodeNode(codeNode) && codeNode.getLanguage() === language) {
|
|
76
|
+
codeNode.setIsSyntaxHighlightSupported(true);
|
|
77
|
+
codeNode.markDirty();
|
|
80
78
|
}
|
|
79
|
+
}, {
|
|
80
|
+
tag: HISTORY_MERGE_TAG
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
|
-
}
|
|
83
|
+
});
|
|
84
84
|
}
|
|
85
85
|
function isCodeThemeLoaded(theme) {
|
|
86
86
|
const themeId = theme;
|
|
@@ -92,21 +92,25 @@ function isCodeThemeLoaded(theme) {
|
|
|
92
92
|
return shiki.getLoadedThemes().includes(themeId);
|
|
93
93
|
}
|
|
94
94
|
function loadCodeTheme(theme, editor, codeNodeKey) {
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
95
|
+
if (isCodeThemeLoaded(theme)) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const themeInfo = bundledThemesInfo.find(info => info.id === theme);
|
|
99
|
+
if (!themeInfo) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
return shiki.loadTheme(themeInfo.import()).then(() => {
|
|
103
|
+
if (editor && codeNodeKey) {
|
|
104
|
+
editor.update(() => {
|
|
105
|
+
const codeNode = $getNodeByKey(codeNodeKey);
|
|
106
|
+
if ($isCodeNode(codeNode)) {
|
|
107
|
+
codeNode.markDirty();
|
|
106
108
|
}
|
|
109
|
+
}, {
|
|
110
|
+
tag: HISTORY_MERGE_TAG
|
|
107
111
|
});
|
|
108
112
|
}
|
|
109
|
-
}
|
|
113
|
+
});
|
|
110
114
|
}
|
|
111
115
|
function getCodeLanguageOptions() {
|
|
112
116
|
return bundledLanguagesInfo.map(i => [i.id, i.name]);
|
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
"use strict";var e=require("@lexical/code-core"),t=require("@lexical/extension"),n=require("lexical"),i=require("@shikijs/core"),o=require("@shikijs/engine-javascript"),r=require("shiki/langs"),s=require("shiki/themes");const d=i.createHighlighterCoreSync({engine:o.createJavaScriptRegexEngine(),langs:[],themes:[]});function g(e){const t=/^diff-([\w-]+)/i.exec(e);return t?t[1]:null}function a(e){const t=g(e)||e;return!!i.isSpecialLang(t)||d.getLoadedLanguages().includes(t)}function l(t,i,o){const s=g(t),l=s||t;if(
|
|
9
|
+
"use strict";var e=require("@lexical/code-core"),t=require("@lexical/extension"),n=require("lexical"),i=require("@shikijs/core"),o=require("@shikijs/engine-javascript"),r=require("shiki/langs"),s=require("shiki/themes");const d=i.createHighlighterCoreSync({engine:o.createJavaScriptRegexEngine(),langs:[],themes:[]});function g(e){const t=/^diff-([\w-]+)/i.exec(e);return t?t[1]:null}function a(e){const t=g(e)||e;return!!i.isSpecialLang(t)||d.getLoadedLanguages().includes(t)}function l(t,i,o){const s=g(t),l=s||t;if(a(l))return;const u=r.bundledLanguagesInfo.find(e=>e.id===l||e.aliases&&e.aliases.includes(l));return u?d.loadLanguage(u.import()).then(()=>{i&&o&&i.update(()=>{const i=n.$getNodeByKey(o);e.$isCodeNode(i)&&i.getLanguage()===t&&(i.setIsSyntaxHighlightSupported(!0),i.markDirty())},{tag:n.HISTORY_MERGE_TAG})}):void 0}function u(e){const t=e;return!!i.isSpecialTheme(t)||d.getLoadedThemes().includes(t)}function c(t,i,o){if(u(t))return;const r=s.bundledThemesInfo.find(e=>e.id===t);return r?d.loadTheme(r.import()).then(()=>{i&&o&&i.update(()=>{const t=n.$getNodeByKey(o);e.$isCodeNode(t)&&t.markDirty()},{tag:n.HISTORY_MERGE_TAG})}):void 0}function h(t,o){const r=/^diff-([\w-]+)/i.exec(o),s=t.getTextContent(),g=d.codeToTokens(s,{lang:r?r[1]:o,theme:t.getTheme()||"poimandres"}),{tokens:a,bg:l,fg:u}=g;let c="";return l&&(c+=`background-color: ${l};`),u&&(c+=`color: ${u};`),t.getStyle()!==c&&t.setStyle(c),function(t,o){const r=[];return t.forEach((t,s)=>{s&&r.push(n.$createLineBreakNode()),t.forEach((t,s)=>{let d=t.content;if(o&&0===s&&d.length>0){const t=["+","-",">","<"," "],n=["inserted","deleted","inserted","deleted","unchanged"],i=t.indexOf(d[0]);-1!==i&&(r.push(e.$createCodeHighlightNode(t[i],n[i])),d=d.slice(1))}const g=i.stringifyTokenStyle(t.htmlStyle||i.getTokenStyleObject(t));n.tokenizeRawText(d,{linebreak:()=>r.push(n.$createLineBreakNode()),tab:()=>r.push(n.$createTabNode()),text:t=>{const n=e.$createCodeHighlightNode(t);n.setStyle(g),r.push(n)}})})}),r}(a,!!r)}const f={$tokenize(t,n){const i=n||this.defaultLanguage;return null===i?e.$plainifyCodeContent(t.getTextContent()):h(t,i)},defaultLanguage:e.DEFAULT_CODE_LANGUAGE,defaultTheme:"one-light"};function p(t,i,o,r){const s=r.getParent();e.$isCodeNode(s)?m(t,i,o,s):e.$isCodeHighlightNode(r)&&r.replace(n.$createTextNode(r.__text))}function C(e,t){const i=t.getElementByKey(e.getKey());if(null===i)return;const o=e.getChildren(),r=o.length;if(r===i.__cachedChildrenLength)return;i.__cachedChildrenLength=r;let s="1",d=1;for(let e=0;e<r;e++)n.$isLineBreakNode(o[e])&&(s+="\n"+ ++d);i.setAttribute("data-gutter",s)}function m(t,i,o,r){const s=r.getKey(),{nodesCurrentlyHighlighting:d}=o;let g=r.getLanguage();g||null===i.defaultLanguage||(g=i.defaultLanguage,r.setLanguage(g));let h=r.getTheme();h||(h=i.defaultTheme,r.setTheme(h));let f=!1;if(u(h)||(c(h,t,s),g&&(f=!0)),g)if(a(g))r.getIsSyntaxHighlightSupported()||r.setIsSyntaxHighlightSupported(!0);else{!l(g,t,s)&&r.getIsSyntaxHighlightSupported()&&r.setIsSyntaxHighlightSupported(!1),f=!0}else r.getIsSyntaxHighlightSupported()&&r.setIsSyntaxHighlightSupported(!1);f||d.has(s)||(d.add(s),o.didTransform||(o.didTransform=!0,n.$onUpdate(()=>{o.didTransform=!1,d.clear()})),function(t,i){const o=n.$getNodeByKey(t);if(!e.$isCodeNode(o)||!o.isAttached())return;const r=n.$getSelection();if(!n.$isRangeSelection(r))return void i();const s=r.anchor,d=s.offset,g="element"===s.type&&n.$isLineBreakNode(o.getChildAtIndex(s.offset-1));let a=0;if(!g){const e=s.getNode();a=d+e.getPreviousSiblings().reduce((e,t)=>e+t.getTextContentSize(),0)}if(!i())return;if(g)return void s.getNode().select(d,d);o.getChildren().some(e=>{const t=n.$isTextNode(e);if(t||n.$isLineBreakNode(e)){const n=e.getTextContentSize();if(t&&n>=a)return e.select(a,a),!0;a-=n}return!1})}(s,()=>{const t=n.$getNodeByKey(s);if(!e.$isCodeNode(t)||!t.isAttached())return!1;const o=t.getLanguage()||i.defaultLanguage,d=i.$tokenize(t,o??void 0),g=function(e,t){let n=0;for(;n<e.length&&x(e[n],t[n]);)n++;const i=e.length,o=t.length,r=Math.min(i,o)-n;let s=0;for(;s<r;)if(s++,!x(e[i-s],t[o-s])){s--;break}const d=n,g=i-s,a=t.slice(n,o-s);return{from:d,nodesForReplacement:a,to:g}}(t.getChildren(),d),{from:a,to:l,nodesForReplacement:u}=g;return!(a===l&&!u.length)&&(r.splice(a,l-a,u),!0)}))}function x(t,i){return e.$isCodeHighlightNode(t)&&e.$isCodeHighlightNode(i)&&t.__text===i.__text&&t.__highlightType===i.__highlightType&&t.__style===i.__style||n.$isTabNode(t)&&n.$isTabNode(i)||n.$isLineBreakNode(t)&&n.$isLineBreakNode(i)}function y(t,i){const o=[];!0!==t._headless&&o.push(t.registerMutationListener(e.CodeNode,e=>{t.read("latest",()=>{for(const[i,o]of e)if("destroyed"!==o){const e=n.$getNodeByKey(i);null!==e&&C(e,t)}})},{skipInitialization:!1}));const r={didTransform:!1,nodesCurrentlyHighlighting:new Set};return o.push(t.registerNodeTransform(e.CodeNode,m.bind(null,t,i,r)),t.registerNodeTransform(n.TextNode,p.bind(null,t,i,r)),t.registerNodeTransform(e.CodeHighlightNode,p.bind(null,t,i,r))),n.mergeRegister(...o)}const N=/* @__PURE__ */n.defineExtension({build:(e,n)=>t.namedSignals(n),config:/* @__PURE__ */n.safeCast({disabled:!1,tokenizer:f}),dependencies:[e.CodeExtension,e.CodeIndentExtension],name:"@lexical/code-shiki",register:(e,n,i)=>{const o=i.getOutput();return t.effect(()=>{if(!o.disabled.value)return y(e,o.tokenizer.value)})}}),T=/* @__PURE__ */n.defineExtension({config:/* @__PURE__ */n.safeCast(f),dependencies:[N],init:(e,t,n)=>{n.getDependency(N).config.tokenizer=t},name:"@lexical/code-shiki/legacy"});exports.CodeHighlighterShikiExtension=T,exports.CodeShikiExtension=N,exports.ShikiTokenizer=f,exports.getCodeLanguageOptions=function(){return r.bundledLanguagesInfo.map(e=>[e.id,e.name])},exports.getCodeThemeOptions=function(){return s.bundledThemesInfo.map(e=>[e.id,e.displayName])},exports.isCodeLanguageLoaded=a,exports.loadCodeLanguage=l,exports.loadCodeTheme=c,exports.normalizeCodeLanguage=function(e){const t=e,n=r.bundledLanguagesInfo.find(e=>e.id===t||e.aliases&&e.aliases.includes(t));return n?n.id:e},exports.registerCodeHighlighting=function(t,i=f){if(!t.hasNodes([e.CodeNode,e.CodeHighlightNode]))throw new Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return n.mergeRegister(y(t,i),e.registerCodeIndentation(t))};
|
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import{$isCodeNode as e,$createCodeHighlightNode as t,DEFAULT_CODE_LANGUAGE as n,CodeExtension as i,CodeIndentExtension as o,$plainifyCodeContent as r,CodeNode as s,CodeHighlightNode as l,registerCodeIndentation as d,$isCodeHighlightNode as a}from"@lexical/code-core";import{effect as g,namedSignals as u}from"@lexical/extension";import{$getNodeByKey as c,$createLineBreakNode as
|
|
9
|
+
import{$isCodeNode as e,$createCodeHighlightNode as t,DEFAULT_CODE_LANGUAGE as n,CodeExtension as i,CodeIndentExtension as o,$plainifyCodeContent as r,CodeNode as s,CodeHighlightNode as l,registerCodeIndentation as d,$isCodeHighlightNode as a}from"@lexical/code-core";import{effect as g,namedSignals as u}from"@lexical/extension";import{$getNodeByKey as c,HISTORY_MERGE_TAG as f,$createLineBreakNode as h,tokenizeRawText as m,$createTabNode as p,defineExtension as y,safeCast as x,mergeRegister as S,TextNode as T,$isLineBreakNode as k,$onUpdate as _,$createTextNode as L,$getSelection as C,$isRangeSelection as b,$isTextNode as H,$isTabNode as v}from"lexical";import{createHighlighterCoreSync as I,isSpecialLang as N,isSpecialTheme as z,stringifyTokenStyle as w,getTokenStyleObject as A}from"@shikijs/core";import{createJavaScriptRegexEngine as E}from"@shikijs/engine-javascript";import{bundledLanguagesInfo as $}from"shiki/langs";import{bundledThemesInfo as j}from"shiki/themes";const D=I({engine:E(),langs:[],themes:[]});function K(e){const t=/^diff-([\w-]+)/i.exec(e);return t?t[1]:null}function P(e){const t=K(e)||e;return!!N(t)||D.getLoadedLanguages().includes(t)}function F(t,n,i){const o=K(t),r=o||t;if(P(r))return;const s=$.find(e=>e.id===r||e.aliases&&e.aliases.includes(r));return s?D.loadLanguage(s.import()).then(()=>{n&&i&&n.update(()=>{const n=c(i);e(n)&&n.getLanguage()===t&&(n.setIsSyntaxHighlightSupported(!0),n.markDirty())},{tag:f})}):void 0}function M(e){const t=e;return!!z(t)||D.getLoadedThemes().includes(t)}function O(t,n,i){if(M(t))return;const o=j.find(e=>e.id===t);return o?D.loadTheme(o.import()).then(()=>{n&&i&&n.update(()=>{const t=c(i);e(t)&&t.markDirty()},{tag:f})}):void 0}function R(){return $.map(e=>[e.id,e.name])}function B(){return j.map(e=>[e.id,e.displayName])}function q(e){const t=e,n=$.find(e=>e.id===t||e.aliases&&e.aliases.includes(t));return n?n.id:e}function G(e,n){const i=/^diff-([\w-]+)/i.exec(n),o=e.getTextContent(),r=D.codeToTokens(o,{lang:i?i[1]:n,theme:e.getTheme()||"poimandres"}),{tokens:s,bg:l,fg:d}=r;let a="";return l&&(a+=`background-color: ${l};`),d&&(a+=`color: ${d};`),e.getStyle()!==a&&e.setStyle(a),function(e,n){const i=[];return e.forEach((e,o)=>{o&&i.push(h()),e.forEach((e,o)=>{let r=e.content;if(n&&0===o&&r.length>0){const e=["+","-",">","<"," "],n=["inserted","deleted","inserted","deleted","unchanged"],o=e.indexOf(r[0]);-1!==o&&(i.push(t(e[o],n[o])),r=r.slice(1))}const s=w(e.htmlStyle||A(e));m(r,{linebreak:()=>i.push(h()),tab:()=>i.push(p()),text:e=>{const n=t(e);n.setStyle(s),i.push(n)}})})}),i}(s,!!i)}const J={$tokenize(e,t){const n=t||this.defaultLanguage;return null===n?r(e.getTextContent()):G(e,n)},defaultLanguage:n,defaultTheme:"one-light"};function Q(t,n,i,o){const r=o.getParent();e(r)?V(t,n,i,r):a(o)&&o.replace(L(o.__text))}function U(e,t){const n=t.getElementByKey(e.getKey());if(null===n)return;const i=e.getChildren(),o=i.length;if(o===n.__cachedChildrenLength)return;n.__cachedChildrenLength=o;let r="1",s=1;for(let e=0;e<o;e++)k(i[e])&&(r+="\n"+ ++s);n.setAttribute("data-gutter",r)}function V(t,n,i,o){const r=o.getKey(),{nodesCurrentlyHighlighting:s}=i;let l=o.getLanguage();l||null===n.defaultLanguage||(l=n.defaultLanguage,o.setLanguage(l));let d=o.getTheme();d||(d=n.defaultTheme,o.setTheme(d));let a=!1;if(M(d)||(O(d,t,r),l&&(a=!0)),l)if(P(l))o.getIsSyntaxHighlightSupported()||o.setIsSyntaxHighlightSupported(!0);else{!F(l,t,r)&&o.getIsSyntaxHighlightSupported()&&o.setIsSyntaxHighlightSupported(!1),a=!0}else o.getIsSyntaxHighlightSupported()&&o.setIsSyntaxHighlightSupported(!1);a||s.has(r)||(s.add(r),i.didTransform||(i.didTransform=!0,_(()=>{i.didTransform=!1,s.clear()})),function(t,n){const i=c(t);if(!e(i)||!i.isAttached())return;const o=C();if(!b(o))return void n();const r=o.anchor,s=r.offset,l="element"===r.type&&k(i.getChildAtIndex(r.offset-1));let d=0;if(!l){const e=r.getNode();d=s+e.getPreviousSiblings().reduce((e,t)=>e+t.getTextContentSize(),0)}if(!n())return;if(l)return void r.getNode().select(s,s);i.getChildren().some(e=>{const t=H(e);if(t||k(e)){const n=e.getTextContentSize();if(t&&n>=d)return e.select(d,d),!0;d-=n}return!1})}(r,()=>{const t=c(r);if(!e(t)||!t.isAttached())return!1;const i=t.getLanguage()||n.defaultLanguage,s=n.$tokenize(t,i??void 0),l=function(e,t){let n=0;for(;n<e.length&&W(e[n],t[n]);)n++;const i=e.length,o=t.length,r=Math.min(i,o)-n;let s=0;for(;s<r;)if(s++,!W(e[i-s],t[o-s])){s--;break}const l=n,d=i-s,a=t.slice(n,o-s);return{from:l,nodesForReplacement:a,to:d}}(t.getChildren(),s),{from:d,to:a,nodesForReplacement:g}=l;return!(d===a&&!g.length)&&(o.splice(d,a-d,g),!0)}))}function W(e,t){return a(e)&&a(t)&&e.__text===t.__text&&e.__highlightType===t.__highlightType&&e.__style===t.__style||v(e)&&v(t)||k(e)&&k(t)}function X(e,t){const n=[];!0!==e._headless&&n.push(e.registerMutationListener(s,t=>{e.read("latest",()=>{for(const[n,i]of t)if("destroyed"!==i){const t=c(n);null!==t&&U(t,e)}})},{skipInitialization:!1}));const i={didTransform:!1,nodesCurrentlyHighlighting:new Set};return n.push(e.registerNodeTransform(s,V.bind(null,e,t,i)),e.registerNodeTransform(T,Q.bind(null,e,t,i)),e.registerNodeTransform(l,Q.bind(null,e,t,i))),S(...n)}function Y(e,t=J){if(!e.hasNodes([s,l]))throw new Error("CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor");return S(X(e,t),d(e))}const Z=/* @__PURE__ */y({build:(e,t)=>u(t),config:/* @__PURE__ */x({disabled:!1,tokenizer:J}),dependencies:[i,o],name:"@lexical/code-shiki",register:(e,t,n)=>{const i=n.getOutput();return g(()=>{if(!i.disabled.value)return X(e,i.tokenizer.value)})}}),ee=/* @__PURE__ */y({config:/* @__PURE__ */x(J),dependencies:[Z],init:(e,t,n)=>{n.getDependency(Z).config.tokenizer=t},name:"@lexical/code-shiki/legacy"});export{ee as CodeHighlighterShikiExtension,Z as CodeShikiExtension,J as ShikiTokenizer,R as getCodeLanguageOptions,B as getCodeThemeOptions,P as isCodeLanguageLoaded,F as loadCodeLanguage,O as loadCodeTheme,q as normalizeCodeLanguage,Y as registerCodeHighlighting};
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"code"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"version": "0.48.0",
|
|
11
|
+
"version": "0.48.1-nightly.20260720.0",
|
|
12
12
|
"main": "./dist/LexicalCodeShiki.js",
|
|
13
13
|
"types": "./dist/typescript-too-old.d.ts",
|
|
14
14
|
"dependencies": {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"@shikijs/langs": "^4.2.0",
|
|
18
18
|
"@shikijs/themes": "^4.2.0",
|
|
19
19
|
"shiki": "^4.2.0",
|
|
20
|
-
"
|
|
21
|
-
"lexical": "0.48.0",
|
|
22
|
-
"@lexical/extension": "0.48.0"
|
|
20
|
+
"lexical": "0.48.1-nightly.20260720.0",
|
|
21
|
+
"@lexical/code-core": "0.48.1-nightly.20260720.0",
|
|
22
|
+
"@lexical/extension": "0.48.1-nightly.20260720.0"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
package/src/FacadeShiki.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
$createLineBreakNode,
|
|
26
26
|
$createTabNode,
|
|
27
27
|
$getNodeByKey,
|
|
28
|
+
HISTORY_MERGE_TAG,
|
|
28
29
|
type LexicalEditor,
|
|
29
30
|
type LexicalNode,
|
|
30
31
|
type NodeKey,
|
|
@@ -78,29 +79,32 @@ export function loadCodeLanguage(
|
|
|
78
79
|
) {
|
|
79
80
|
const diffedLanguage = getDiffedLanguage(language);
|
|
80
81
|
const langId = diffedLanguage ? diffedLanguage : language;
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// make sure the code is highlighed with the correct language
|
|
92
|
-
if (editor && codeNodeKey) {
|
|
93
|
-
editor.update(() => {
|
|
94
|
-
const codeNode = $getNodeByKey(codeNodeKey);
|
|
95
|
-
if ($isCodeNode(codeNode) && codeNode.getLanguage() === language) {
|
|
96
|
-
codeNode.setIsSyntaxHighlightSupported(true);
|
|
97
|
-
codeNode.markDirty();
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
}
|
|
82
|
+
if (isCodeLanguageLoaded(langId)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const languageInfo = bundledLanguagesInfo.find(
|
|
87
|
+
desc =>
|
|
88
|
+
desc.id === langId || (desc.aliases && desc.aliases.includes(langId)),
|
|
89
|
+
);
|
|
90
|
+
if (!languageInfo) {
|
|
91
|
+
return undefined;
|
|
103
92
|
}
|
|
93
|
+
|
|
94
|
+
return shiki.loadLanguage(languageInfo.import()).then(() => {
|
|
95
|
+
if (editor && codeNodeKey) {
|
|
96
|
+
editor.update(
|
|
97
|
+
() => {
|
|
98
|
+
const codeNode = $getNodeByKey(codeNodeKey);
|
|
99
|
+
if ($isCodeNode(codeNode) && codeNode.getLanguage() === language) {
|
|
100
|
+
codeNode.setIsSyntaxHighlightSupported(true);
|
|
101
|
+
codeNode.markDirty();
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{tag: HISTORY_MERGE_TAG},
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
export function isCodeThemeLoaded(theme: string) {
|
|
@@ -119,21 +123,28 @@ export function loadCodeTheme(
|
|
|
119
123
|
editor?: LexicalEditor,
|
|
120
124
|
codeNodeKey?: NodeKey,
|
|
121
125
|
) {
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if ($isCodeNode(codeNode)) {
|
|
130
|
-
codeNode.markDirty();
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
126
|
+
if (isCodeThemeLoaded(theme)) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const themeInfo = bundledThemesInfo.find(info => info.id === theme);
|
|
131
|
+
if (!themeInfo) {
|
|
132
|
+
return undefined;
|
|
136
133
|
}
|
|
134
|
+
|
|
135
|
+
return shiki.loadTheme(themeInfo.import()).then(() => {
|
|
136
|
+
if (editor && codeNodeKey) {
|
|
137
|
+
editor.update(
|
|
138
|
+
() => {
|
|
139
|
+
const codeNode = $getNodeByKey(codeNodeKey);
|
|
140
|
+
if ($isCodeNode(codeNode)) {
|
|
141
|
+
codeNode.markDirty();
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
{tag: HISTORY_MERGE_TAG},
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
export function getCodeLanguageOptions(): [string, string][] {
|