UNPKG

@lexical/markdown

Version:

This package contains Markdown helpers and functionality for Lexical.

10 lines (8 loc) 15.9 kB
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import{$isParagraphNode as t,$isTextNode as e,$getRoot as n,$isElementNode as o,$isDecoratorNode as r,$isLineBreakNode as i,$getSelection as s,$createTextNode as l,$createParagraphNode as c,$createLineBreakNode as f,$isRangeSelection as a,$isRootOrShadowRoot as u,$createRangeSelection as g,$setSelection as p}from"lexical";import{$isListNode as d,$isListItemNode as h,ListNode as m,ListItemNode as x,$createListItemNode as T,$createListNode as C}from"@lexical/list";import{$isQuoteNode as E,HeadingNode as y,$isHeadingNode as b,QuoteNode as v,$createQuoteNode as S,$createHeadingNode as $}from"@lexical/rich-text";import{$findMatchingParent as F}from"@lexical/utils";import{$isCodeNode as I,CodeNode as N,$createCodeNode as w}from"@lexical/code";import{LinkNode as k,$isLinkNode as L,$createLinkNode as P}from"@lexical/link";function R(t,e){const n={};for(const o of t){const t=e(o);t&&(n[t]?n[t].push(o):n[t]=[o])}return n}function _(t){const e=R(t,(t=>t.type));return{element:e.element||[],multilineElement:e["multiline-element"]||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const B=/[!-/:-@[-`{-~\s]/,M=/^\s{0,3}$/;function j(n){if(!t(n))return!1;const o=n.getFirstChild();return null==o||1===n.getChildrenSize()&&e(o)&&M.test(o.getTextContent())}function A(t,e,n,i){for(const o of e){if(!o.export)continue;const e=o.export(t,(t=>z(t,n,i)));if(null!=e)return e}return o(t)?z(t,n,i):r(t)?t.getTextContent():null}function z(t,n,s,l,c){const f=[],a=t.getChildren();l||(l=[]),c||(c=[]);t:for(const t of a){for(const e of s){if(!e.export)continue;const o=e.export(t,(t=>z(t,n,s,l,[...c,...l])),((t,e)=>U(t,e,n,l,c)));if(null!=o){f.push(o);continue t}}i(t)?f.push("\n"):e(t)?f.push(U(t,t.getTextContent(),n,l,c)):o(t)?f.push(z(t,n,s,l,c)):r(t)&&f.push(t.getTextContent())}return f.join("")}function U(t,e,n,o,r){let i=0===t.getFormat()?e:function(t){return t.replace(/^\s+|\s+$/g,(t=>[...t].map((t=>"&#"+t.codePointAt(0)+";")).join("")))}(e);t.hasFormat("code")||(i=i.replace(/([*_`~\\])/g,"\\$1"));let s="",l="",c="";const f=W(t,!0),a=W(t,!1),u=new Set;for(const e of n){const n=e.format[0],r=e.tag;D(t,n)&&!u.has(n)&&(u.add(n),D(f,n)&&o.find((t=>t.tag===r))||(o.push({format:n,tag:r}),s+=r))}for(let e=0;e<o.length;e++){const n=D(t,o[e].format),i=D(a,o[e].format);if(n&&i)continue;const s=[...o];for(;s.length>e;){const t=s.pop();r&&t&&r.find((e=>e.tag===t.tag))||(t&&"string"==typeof t.tag&&(n?i||(c+=t.tag):l+=t.tag),o.pop())}break}return i=s+i+c,l+i}function W(t,n){let r=n?t.getPreviousSibling():t.getNextSibling();if(!r){const e=t.getParentOrThrow();e.isInline()&&(r=n?e.getPreviousSibling():e.getNextSibling())}for(;r;){if(o(r)){if(!r.isInline())break;const t=n?r.getLastDescendant():r.getFirstDescendant();if(e(t))return t;r=n?r.getPreviousSibling():r.getNextSibling()}if(e(r))return r;if(!o(r))return null}return null}function D(t,n){return e(t)&&t.hasFormat(n)}function O(t,e){const n=function(t,e){const n=t.match(e.openTagsRegExp);if(null==n)return null;for(const o of n){const n=o.replace(/^\s/,""),r=e.fullMatchRegExpByTag[n];if(null==r)continue;const i=t.match(r),s=e.transformersByTag[n];if(null!=i&&null!=s){if(!1!==s.intraword)return i;const{index:e=0}=i,n=t[e-1],o=t[e+i[0].length];if((!n||B.test(n))&&(!o||B.test(o)))return i}}return null}(t.getTextContent(),e);if(!n)return null;const o=n.index||0;return{endIndex:o+n[0].length,match:n,startIndex:o,transformer:e.transformersByTag[n[1]]}}function q(t){return e(t)&&!t.hasFormat("code")}function G(t,e,n){let o=O(t,e),r=function(t,e){const n=t;let o,r,i,s;for(const t of e){if(!t.replace||!t.importRegExp)continue;const e=n.getTextContent().match(t.importRegExp);if(!e)continue;const l=e.index||0,c=t.getEndIndex?t.getEndIndex(n,e):l+e[0].length;!1!==c&&(void 0===o||void 0===r||l<o&&c>r)&&(o=l,r=c,i=t,s=e)}return void 0===o||void 0===r||void 0===i||void 0===s?null:{endIndex:r,match:s,startIndex:o,transformer:i}}(t,n);if(o&&r&&(o.startIndex<=r.startIndex&&o.endIndex>=r.endIndex?r=null:o=null),o){const r=function(t,e,n,o,r){const i=t.getTextContent();let s,l,c;if(r[0]===i?s=t:0===e?[s,l]=t.splitText(n):[c,s,l]=t.splitText(e,n),s.setTextContent(r[2]),o)for(const t of o.format)s.hasFormat(t)||s.toggleFormat(t);return{nodeAfter:l,nodeBefore:c,transformedNode:s}}(t,o.startIndex,o.endIndex,o.transformer,o.match);q(r.nodeAfter)&&G(r.nodeAfter,e,n),q(r.nodeBefore)&&G(r.nodeBefore,e,n),q(r.transformedNode)&&G(r.transformedNode,e,n)}else if(r){const o=function(t,e,n,o,r){let i,s,l;return 0===e?[i,s]=t.splitText(n):[l,i,s]=t.splitText(e,n),o.replace?{nodeAfter:s,nodeBefore:l,transformedNode:o.replace(i,r)||void 0}:null}(t,r.startIndex,r.endIndex,r.transformer,r.match);if(!o)return;q(o.nodeAfter)&&G(o.nodeAfter,e,n),q(o.nodeBefore)&&G(o.nodeBefore,e,n),q(o.transformedNode)&&G(o.transformedNode,e,n)}const i=t.getTextContent().replace(/\\([*_`~])/g,"$1").replace(/&#(\d+);/g,((t,e)=>String.fromCodePoint(e)));t.setTextContent(i)}function H(t,e=!1){const o=_(t),r=function(t){const e={},n={},o=[],r="(?<![\\\\])";for(const r of t){const{tag:t}=r;e[t]=r;const i=t.replace(/(\*|\^|\+)/g,"\\$1");o.push(i),1===t.length?n[t]=new RegExp(`(?<![\\\\${i}])(${i})((\\\\${i})?.*?[^${i}\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?![\\\\${i}])`):n[t]=new RegExp(`(?<!\\\\)(${i})((\\\\${i})?.*?[^\\s](\\\\${i})?)((?<!\\\\)|(?<=\\\\\\\\))(${i})(?!\\\\)`)}return{fullMatchRegExpByTag:n,openTagsRegExp:new RegExp(`${r}(${o.join("|")})`,"g"),transformersByTag:e}}(o.textFormat);return(t,i)=>{const l=t.split("\n"),c=l.length,f=i||n();f.clear();for(let t=0;t<c;t++){const n=l[t],[i,s]=J(l,t,o.multilineElement,f);i?t=s:K(n,f,o.element,r,o.textMatch,e)}const a=f.getChildren();for(const t of a)!e&&j(t)&&f.getChildrenSize()>1&&t.remove();null!==s()&&f.selectStart()}}function J(t,e,n,o){for(const r of n){const{handleImportAfterStartMatch:n,regExpEnd:i,regExpStart:s,replace:l}=r,c=t[e].match(s);if(!c)continue;if(n){const i=n({lines:t,rootNode:o,startLineIndex:e,startMatch:c,transformer:r});if(null===i)continue;if(i)return i}const f="object"==typeof i&&"regExp"in i?i.regExp:i,a=i&&"object"==typeof i&&"optional"in i?i.optional:!i;let u=e;const g=t.length;for(;u<g;){const n=f?t[u].match(f):null;if(!n&&(!a||a&&u<g-1)){u++;continue}if(n&&e===u&&n.index===c.index){u++;continue}const r=[];if(n&&e===u)r.push(t[e].slice(c[0].length,-n[0].length));else for(let o=e;o<=u;o++)if(o===e){const e=t[o].slice(c[0].length);r.push(e)}else if(o===u&&n){const e=t[o].slice(0,-n[0].length);r.push(e)}else r.push(t[o]);if(!1!==l(o,null,c,n,r,!0))return[!0,u];break}}return[!1,e]}function K(e,n,o,r,i,s){const a=l(e),u=c();u.append(a),n.append(u);for(const{regExp:t,replace:n}of o){const o=e.match(t);if(o&&(a.setTextContent(e.slice(o[0].length)),!1!==n(u,[a],o,!0)))break}if(G(a,r,i),u.isAttached()&&e.length>0){const e=u.getPreviousSibling();if(!s&&(t(e)||E(e)||d(e))){let t=e;if(d(e)){const n=e.getLastDescendant();t=null==n?null:F(n,h)}null!=t&&t.getTextContentSize()>0&&(t.splice(t.getChildrenSize(),0,[f(),...u.getChildren()]),u.remove())}}}function Q(t,...e){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",t);for(const t of e)o.append("v",t);throw n.search=o.toString(),Error(`Minified Lexical error #${t}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}function V(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(X(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function X(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function Y(t,n=Pt){const o=_(n),r=R(o.textFormat,(({tag:t})=>t[t.length-1])),l=R(o.textMatch,(({trigger:t})=>t));for(const e of n){const n=e.type;if("element"===n||"text-match"===n||"multiline-element"===n){const n=e.dependencies;for(const e of n)t.hasNode(e)||Q(173,e.getType())}}const c=(t,n,c)=>{(function(t,e,n,o){const r=t.getParent();if(!u(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExp:r,replace:s}of o){const o=i.match(r);if(o&&o[0].length===(o[0].endsWith(" ")?n:n-1)){const r=e.getNextSiblings(),[i,l]=e.splitText(n);if(i.remove(),!1!==s(t,l?[l,...r]:r,o,!1))return!0}}return!1})(t,n,c,o.element)||function(t,e,n,o){const r=t.getParent();if(!u(r)||t.getFirstChild()!==e)return!1;const i=e.getTextContent();if(" "!==i[n-1])return!1;for(const{regExpStart:r,replace:s,regExpEnd:l}of o){if(l&&!("optional"in l)||l&&"optional"in l&&!l.optional)continue;const o=i.match(r);if(o&&o[0].length===(o[0].endsWith(" ")?n:n-1)){const r=e.getNextSiblings(),[i,l]=e.splitText(n);if(i.remove(),!1!==s(t,l?[l,...r]:r,o,null,null,!1))return!0}}return!1}(t,n,c,o.multilineElement)||function(t,e,n){let o=t.getTextContent();const r=n[o[e-1]];if(null==r)return!1;e<o.length&&(o=o.slice(0,e));for(const e of r){if(!e.replace||!e.regExp)continue;const n=o.match(e.regExp);if(null===n)continue;const r=n.index||0,i=r+n[0].length;let s;return 0===r?[s]=t.splitText(i):[,s]=t.splitText(r,i),s.selectNext(0,0),e.replace(s,n),!0}return!1}(n,c,l)||function(t,n,o){const r=t.getTextContent(),l=n-1,c=r[l],f=o[c];if(!f)return!1;for(const n of f){const{tag:o}=n,f=o.length,u=l-f+1;if(f>1&&!X(r,u,o,0,f))continue;if(" "===r[u-1])continue;const d=r[l+1];if(!1===n.intraword&&d&&!B.test(d))continue;const h=t;let m=h,x=V(r,u,o),T=m;for(;x<0&&(T=T.getPreviousSibling())&&!i(T);)if(e(T)){if(T.hasFormat("code"))continue;const t=T.getTextContent();m=T,x=V(t,t.length,o)}if(x<0)continue;if(m===h&&x+f===u)continue;const C=m.getTextContent();if(x>0&&C[x-1]===c)continue;const E=C[x-1];if(!1===n.intraword&&E&&!B.test(E))continue;const y=h.getTextContent(),b=y.slice(0,u)+y.slice(l+1);h.setTextContent(b);const v=m===h?b:C;m.setTextContent(v.slice(0,x)+v.slice(x+f));const S=s(),$=g();p($);const F=l-f*(m===h?2:1)+1;$.anchor.set(m.__key,x,"text"),$.focus.set(h.__key,F,"text");for(const t of n.format)$.hasFormat(t)||$.formatText(t);$.anchor.set($.focus.key,$.focus.offset,$.focus.type);for(const t of n.format)$.hasFormat(t)&&$.toggleFormat(t);return a(S)&&($.format=S.format),!0}}(n,c,r)};return t.registerUpdateListener((({tags:n,dirtyLeaves:o,editorState:r,prevEditorState:i})=>{if(n.has("collaboration")||n.has("historic"))return;if(t.isComposing())return;const l=r.read(s),f=i.read(s);if(!a(f)||!a(l)||!l.isCollapsed()||l.is(f))return;const u=l.anchor.key,g=l.anchor.offset,p=r._nodeMap.get(u);!e(p)||!o.has(u)||1!==g&&g>f.anchor.offset+1||t.update((()=>{if(!q(p))return;const t=p.getParent();null===t||I(t)||c(t,p,l.anchor.offset)}))}))}const Z=/^(\s*)(\d{1,})\.\s/,tt=/^(\s*)[-*+]\s/,et=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,nt=/^(#{1,6})\s/,ot=/^>\s/,rt=/^[ \t]*```(\w+)?/,it=/[ \t]*```$/,st=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,lt=/^(?:\|)(.+)(?:\|)\s?$/,ct=/^(\| ?:?-*:? ?)+\|\s?$/,ft=t=>(e,n,o)=>{const r=t(o);r.append(...n),e.replace(r),r.select(0,0)};const at=t=>(e,n,o)=>{const r=e.getPreviousSibling(),i=e.getNextSibling(),s=T("check"===t?"x"===o[3]:void 0);if(d(i)&&i.getListType()===t){const t=i.getFirstChild();null!==t?t.insertBefore(s):i.append(s),e.remove()}else if(d(r)&&r.getListType()===t)r.append(s),e.remove();else{const n=C(t,"number"===t?Number(o[2]):void 0);n.append(s),e.replace(n)}s.append(...n),s.select(0,0);const l=function(t){const e=t.match(/\t/g),n=t.match(/ /g);let o=0;return e&&(o+=e.length),n&&(o+=Math.floor(n.length/4)),o}(o[1]);l&&s.setIndent(l)},ut=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(h(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(d(t)){o.push(ut(t,e,n+1));continue}}const r=" ".repeat(4*n),l=t.getListType(),c="number"===l?`${t.getStart()+i}. `:"check"===l?`- [${s.getChecked()?"x":" "}] `:"- ";o.push(r+c+e(s)),i++}return o.join("\n")},gt={dependencies:[y],export:(t,e)=>{if(!b(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:nt,replace:ft((t=>{const e="h"+t[1].length;return $(e)})),type:"element"},pt={dependencies:[v],export:(t,e)=>{if(!E(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:ot,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(E(n))return n.splice(n.getChildrenSize(),0,[f(),...e]),n.select(0,0),void t.remove()}const r=S();r.append(...e),t.replace(r),r.select(0,0)},type:"element"},dt={dependencies:[N],export:t=>{if(!I(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExpEnd:{optional:!0,regExp:it},regExpStart:rt,replace:(t,e,n,o,r,i)=>{let s,c;if(!e&&r){if(1===r.length)o?(s=w(),c=n[1]+r[0]):(s=w(n[1]),c=r[0].startsWith(" ")?r[0].slice(1):r[0]);else{if(s=w(n[1]),0===r[0].trim().length)for(;r.length>0&&!r[0].length;)r.shift();else r[0]=r[0].startsWith(" ")?r[0].slice(1):r[0];for(;r.length>0&&!r[r.length-1].length;)r.pop();c=r.join("\n")}const e=l(c);s.append(e),t.append(s)}else e&&ft((t=>w(t?t[1]:void 0)))(t,e,n,i)},type:"multiline-element"},ht={dependencies:[m,x],export:(t,e)=>d(t)?ut(t,e,0):null,regExp:tt,replace:at("bullet"),type:"element"},mt={dependencies:[m,x],export:(t,e)=>d(t)?ut(t,e,0):null,regExp:et,replace:at("check"),type:"element"},xt={dependencies:[m,x],export:(t,e)=>d(t)?ut(t,e,0):null,regExp:Z,replace:at("number"),type:"element"},Tt={format:["code"],tag:"`",type:"text-format"},Ct={format:["highlight"],tag:"==",type:"text-format"},Et={format:["bold","italic"],tag:"***",type:"text-format"},yt={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},bt={format:["bold"],tag:"**",type:"text-format"},vt={format:["bold"],intraword:!1,tag:"__",type:"text-format"},St={format:["strikethrough"],tag:"~~",type:"text-format"},$t={format:["italic"],tag:"*",type:"text-format"},Ft={format:["italic"],intraword:!1,tag:"_",type:"text-format"},It={dependencies:[k],export:(t,e,n)=>{if(!L(t))return null;const o=t.getTitle(),r=e(t);return o?`[${r}](${t.getURL()} "${o}")`:`[${r}](${t.getURL()})`},importRegExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(t,e)=>{const[,n,o,r]=e,i=P(o,{title:r}),s=l(n);return s.setFormat(t.getFormat()),i.append(s),t.replace(i),s},trigger:")",type:"text-match"};const Nt=[gt,pt,ht,xt],wt=[dt],kt=[Tt,Et,yt,bt,vt,Ct,$t,Ft,St],Lt=[It],Pt=[...Nt,...wt,...kt,...Lt];function Rt(t,e=Pt,n,o=!1,r=!1){const i=o?t:function(t,e=!1){const n=t.split("\n");let o=!1;const r=[];for(let t=0;t<n.length;t++){const i=n[t],s=r[r.length-1];st.test(i)?r.push(i):rt.test(i)||it.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||nt.test(s)||nt.test(i)||ot.test(i)||Z.test(i)||tt.test(i)||et.test(i)||lt.test(i)||ct.test(i)||!e?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(t,r);return H(e,o)(i,n)}function _t(t=Pt,e,o=!1){const r=function(t,e=!1){const o=_(t),r=[...o.multilineElement,...o.element],i=!e,s=o.textFormat.filter((t=>1===t.format.length)).sort(((t,e)=>Number(t.format.includes("code"))-Number(e.format.includes("code"))));return t=>{const e=[],l=(t||n()).getChildren();for(let t=0;t<l.length;t++){const n=l[t],c=A(n,r,s,o.textMatch);null!=c&&e.push(i&&t>0&&!j(n)&&!j(l[t-1])?"\n".concat(c):c)}return e.join("\n")}}(t,o);return r(e)}export{Rt as $convertFromMarkdownString,_t as $convertToMarkdownString,Et as BOLD_ITALIC_STAR,yt as BOLD_ITALIC_UNDERSCORE,bt as BOLD_STAR,vt as BOLD_UNDERSCORE,mt as CHECK_LIST,dt as CODE,Nt as ELEMENT_TRANSFORMERS,gt as HEADING,Ct as HIGHLIGHT,Tt as INLINE_CODE,$t as ITALIC_STAR,Ft as ITALIC_UNDERSCORE,It as LINK,wt as MULTILINE_ELEMENT_TRANSFORMERS,xt as ORDERED_LIST,pt as QUOTE,St as STRIKETHROUGH,kt as TEXT_FORMAT_TRANSFORMERS,Lt as TEXT_MATCH_TRANSFORMERS,Pt as TRANSFORMERS,ht as UNORDERED_LIST,Y as registerMarkdownShortcuts};