@lexical/markdown
Version:
This package contains Markdown helpers and functionality for Lexical.
10 lines (8 loc) • 15.9 kB
JavaScript
/**
* 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,COLLABORATION_TAG as a,HISTORIC_TAG as u,$isRangeSelection as g,$isRootOrShadowRoot as p,$createRangeSelection as d,$setSelection as m}from"lexical";import{$isListNode as h,$isListItemNode as x,ListNode as T,ListItemNode as C,$createListItemNode as E,$createListNode as y}from"@lexical/list";import{$isQuoteNode as v,HeadingNode as S,$isHeadingNode as $,QuoteNode as b,$createQuoteNode as F,$createHeadingNode as I}from"@lexical/rich-text";import{$findMatchingParent as N}from"@lexical/utils";import{$isCodeNode as w,CodeNode as k,$createCodeNode as L}from"@lexical/code";import{LinkNode as P,$isLinkNode as R,$createLinkNode as _}from"@lexical/link";function B(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 M(t){const e=B(t,(t=>t.type));return{element:e.element||[],multilineElement:e["multiline-element"]||[],textFormat:e["text-format"]||[],textMatch:e["text-match"]||[]}}const j=/[!-/:-@[-`{-~\s]/,A=/^\s{0,3}$/;function z(n){if(!t(n))return!1;const o=n.getFirstChild();return null==o||1===n.getChildrenSize()&&e(o)&&A.test(o.getTextContent())}function U(t,e,n,i){for(const o of e){if(!o.export)continue;const e=o.export(t,(t=>W(t,n,i)));if(null!=e)return e}return o(t)?W(t,n,i):r(t)?t.getTextContent():null}function W(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=>W(t,n,s,l,[...c,...l])),((t,e)=>D(t,e,n,l,c)));if(null!=o){f.push(o);continue t}}i(t)?f.push("\n"):e(t)?f.push(D(t,t.getTextContent(),n,l,c)):o(t)?f.push(W(t,n,s,l,c)):r(t)&&f.push(t.getTextContent())}return f.join("")}function D(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=O(t,!0),a=O(t,!1),u=new Set;for(const e of n){const n=e.format[0],r=e.tag;q(t,n)&&!u.has(n)&&(u.add(n),q(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=q(t,o[e].format),i=q(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 O(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 q(t,n){return e(t)&&t.hasFormat(n)}function G(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||j.test(n))&&(!o||j.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 H(t){return e(t)&&!t.hasFormat("code")}function J(t,e,n){let o=G(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);H(r.nodeAfter)&&J(r.nodeAfter,e,n),H(r.nodeBefore)&&J(r.nodeBefore,e,n),H(r.transformedNode)&&J(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;H(o.nodeAfter)&&J(o.nodeAfter,e,n),H(o.nodeBefore)&&J(o.nodeBefore,e,n),H(o.transformedNode)&&J(o.transformedNode,e,n)}const i=t.getTextContent().replace(/\\([*_`~\\])/g,"$1").replace(/&#(\d+);/g,((t,e)=>String.fromCodePoint(e)));t.setTextContent(i)}function K(t,e=!1){const o=M(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]=Q(l,t,o.multilineElement,f);i?t=s:V(n,f,o.element,r,o.textMatch,e)}const a=f.getChildren();for(const t of a)!e&&z(t)&&f.getChildrenSize()>1&&t.remove();null!==s()&&f.selectStart()}}function Q(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 V(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(J(a,r,i),u.isAttached()&&e.length>0){const e=u.getPreviousSibling();if(!s&&(t(e)||v(e)||h(e))){let t=e;if(h(e)){const n=e.getLastDescendant();t=null==n?null:N(n,x)}null!=t&&t.getTextContentSize()>0&&(t.splice(t.getChildrenSize(),0,[f(),...u.getChildren()]),u.remove())}}}function X(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 Y(t,e,n){const o=n.length;for(let r=e;r>=o;r--){const e=r-o;if(Z(t,e,n,0,o)&&" "!==t[e+o])return e}return-1}function Z(t,e,n,o,r){for(let i=0;i<r;i++)if(t[e+i]!==n[o+i])return!1;return!0}function tt(t,n=_t){const o=M(n),r=B(o.textFormat,(({tag:t})=>t[t.length-1])),l=B(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)||X(173,e.getType())}}const c=(t,n,c)=>{(function(t,e,n,o){const r=t.getParent();if(!p(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(!p(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,a=l-f+1;if(f>1&&!Z(r,a,o,0,f))continue;if(" "===r[a-1])continue;const u=r[l+1];if(!1===n.intraword&&u&&!j.test(u))continue;const p=t;let h=p,x=Y(r,a,o),T=h;for(;x<0&&(T=T.getPreviousSibling())&&!i(T);)if(e(T)){if(T.hasFormat("code"))continue;const t=T.getTextContent();h=T,x=Y(t,t.length,o)}if(x<0)continue;if(h===p&&x+f===a)continue;const C=h.getTextContent();if(x>0&&C[x-1]===c)continue;const E=C[x-1];if(!1===n.intraword&&E&&!j.test(E))continue;const y=p.getTextContent(),v=y.slice(0,a)+y.slice(l+1);p.setTextContent(v);const S=h===p?v:C;h.setTextContent(S.slice(0,x)+S.slice(x+f));const $=s(),b=d();m(b);const F=l-f*(h===p?2:1)+1;b.anchor.set(h.__key,x,"text"),b.focus.set(p.__key,F,"text");for(const t of n.format)b.hasFormat(t)||b.formatText(t);b.anchor.set(b.focus.key,b.focus.offset,b.focus.type);for(const t of n.format)b.hasFormat(t)&&b.toggleFormat(t);return g($)&&(b.format=$.format),!0}}(n,c,r)};return t.registerUpdateListener((({tags:n,dirtyLeaves:o,editorState:r,prevEditorState:i})=>{if(n.has(a)||n.has(u))return;if(t.isComposing())return;const l=r.read(s),f=i.read(s);if(!g(f)||!g(l)||!l.isCollapsed()||l.is(f))return;const p=l.anchor.key,d=l.anchor.offset,m=r._nodeMap.get(p);!e(m)||!o.has(p)||1!==d&&d>f.anchor.offset+1||t.update((()=>{if(!H(m))return;const t=m.getParent();null===t||w(t)||c(t,m,l.anchor.offset)}))}))}const et=/^(\s*)(\d{1,})\.\s/,nt=/^(\s*)[-*+]\s/,ot=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,rt=/^(#{1,6})\s/,it=/^>\s/,st=/^[ \t]*```(\w+)?/,lt=/[ \t]*```$/,ct=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,ft=/^(?:\|)(.+)(?:\|)\s?$/,at=/^(\| ?:?-*:? ?)+\|\s?$/,ut=t=>(e,n,o)=>{const r=t(o);r.append(...n),e.replace(r),r.select(0,0)};const gt=t=>(e,n,o)=>{const r=e.getPreviousSibling(),i=e.getNextSibling(),s=E("check"===t?"x"===o[3]:void 0);if(h(i)&&i.getListType()===t){const t=i.getFirstChild();null!==t?t.insertBefore(s):i.append(s),e.remove()}else if(h(r)&&r.getListType()===t)r.append(s),e.remove();else{const n=y(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)},pt=(t,e,n)=>{const o=[],r=t.getChildren();let i=0;for(const s of r)if(x(s)){if(1===s.getChildrenSize()){const t=s.getFirstChild();if(h(t)){o.push(pt(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")},dt={dependencies:[S],export:(t,e)=>{if(!$(t))return null;const n=Number(t.getTag().slice(1));return"#".repeat(n)+" "+e(t)},regExp:rt,replace:ut((t=>{const e="h"+t[1].length;return I(e)})),type:"element"},mt={dependencies:[b],export:(t,e)=>{if(!v(t))return null;const n=e(t).split("\n"),o=[];for(const t of n)o.push("> "+t);return o.join("\n")},regExp:it,replace:(t,e,n,o)=>{if(o){const n=t.getPreviousSibling();if(v(n))return n.splice(n.getChildrenSize(),0,[f(),...e]),n.select(0,0),void t.remove()}const r=F();r.append(...e),t.replace(r),r.select(0,0)},type:"element"},ht={dependencies:[k],export:t=>{if(!w(t))return null;const e=t.getTextContent();return"```"+(t.getLanguage()||"")+(e?"\n"+e:"")+"\n```"},regExpEnd:{optional:!0,regExp:lt},regExpStart:st,replace:(t,e,n,o,r,i)=>{let s,c;if(!e&&r){if(1===r.length)o?(s=L(),c=n[1]+r[0]):(s=L(n[1]),c=r[0].startsWith(" ")?r[0].slice(1):r[0]);else{if(s=L(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&&ut((t=>L(t?t[1]:void 0)))(t,e,n,i)},type:"multiline-element"},xt={dependencies:[T,C],export:(t,e)=>h(t)?pt(t,e,0):null,regExp:nt,replace:gt("bullet"),type:"element"},Tt={dependencies:[T,C],export:(t,e)=>h(t)?pt(t,e,0):null,regExp:ot,replace:gt("check"),type:"element"},Ct={dependencies:[T,C],export:(t,e)=>h(t)?pt(t,e,0):null,regExp:et,replace:gt("number"),type:"element"},Et={format:["code"],tag:"`",type:"text-format"},yt={format:["highlight"],tag:"==",type:"text-format"},vt={format:["bold","italic"],tag:"***",type:"text-format"},St={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},$t={format:["bold"],tag:"**",type:"text-format"},bt={format:["bold"],intraword:!1,tag:"__",type:"text-format"},Ft={format:["strikethrough"],tag:"~~",type:"text-format"},It={format:["italic"],tag:"*",type:"text-format"},Nt={format:["italic"],intraword:!1,tag:"_",type:"text-format"},wt={dependencies:[P],export:(t,e,n)=>{if(!R(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=_(o,{title:r}),s=l(n);return s.setFormat(t.getFormat()),i.append(s),t.replace(i),s},trigger:")",type:"text-match"};const kt=[dt,mt,xt,Ct],Lt=[ht],Pt=[Et,vt,St,$t,bt,yt,It,Nt,Ft],Rt=[wt],_t=[...kt,...Lt,...Pt,...Rt];function Bt(t,e=_t,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];ct.test(i)?r.push(i):st.test(i)||lt.test(i)?(o=!o,r.push(i)):o||""===i||""===s||!s||rt.test(s)||rt.test(i)||it.test(i)||et.test(i)||nt.test(i)||ot.test(i)||ft.test(i)||at.test(i)||!e?r.push(i):r[r.length-1]=s+i}return r.join("\n")}(t,r);return K(e,o)(i,n)}function Mt(t=_t,e,o=!1){const r=function(t,e=!1){const o=M(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=U(n,r,s,o.textMatch);null!=c&&e.push(i&&t>0&&!z(n)&&!z(l[t-1])?"\n".concat(c):c)}return e.join("\n")}}(t,o);return r(e)}export{Bt as $convertFromMarkdownString,Mt as $convertToMarkdownString,vt as BOLD_ITALIC_STAR,St as BOLD_ITALIC_UNDERSCORE,$t as BOLD_STAR,bt as BOLD_UNDERSCORE,Tt as CHECK_LIST,ht as CODE,kt as ELEMENT_TRANSFORMERS,dt as HEADING,yt as HIGHLIGHT,Et as INLINE_CODE,It as ITALIC_STAR,Nt as ITALIC_UNDERSCORE,wt as LINK,Lt as MULTILINE_ELEMENT_TRANSFORMERS,Ct as ORDERED_LIST,mt as QUOTE,Ft as STRIKETHROUGH,Pt as TEXT_FORMAT_TRANSFORMERS,Rt as TEXT_MATCH_TRANSFORMERS,_t as TRANSFORMERS,xt as UNORDERED_LIST,tt as registerMarkdownShortcuts};