@lexical/markdown
Advanced tools
Comparing version 0.21.1-nightly.20241213.0 to 0.21.1-nightly.20241216.0
@@ -118,2 +118,4 @@ /** | ||
const children = node.getChildren(); | ||
// keep track of unclosed tags from the very beginning | ||
const unclosedTags = []; | ||
mainLoop: for (const child of children) { | ||
@@ -124,3 +126,3 @@ for (const transformer of textMatchTransformers) { | ||
} | ||
const result = transformer.export(child, parentNode => exportChildren(parentNode, textTransformersIndex, textMatchTransformers), (textNode, textContent) => exportTextFormat(textNode, textContent, textTransformersIndex)); | ||
const result = transformer.export(child, parentNode => exportChildren(parentNode, textTransformersIndex, textMatchTransformers), (textNode, textContent) => exportTextFormat(textNode, textContent, textTransformersIndex, unclosedTags)); | ||
if (result != null) { | ||
@@ -134,3 +136,3 @@ output.push(result); | ||
} else if (lexical.$isTextNode(child)) { | ||
output.push(exportTextFormat(child, child.getTextContent(), textTransformersIndex)); | ||
output.push(exportTextFormat(child, child.getTextContent(), textTransformersIndex, unclosedTags)); | ||
} else if (lexical.$isElementNode(child)) { | ||
@@ -145,3 +147,5 @@ // empty paragraph returns "" | ||
} | ||
function exportTextFormat(node, textContent, textTransformers) { | ||
function exportTextFormat(node, textContent, textTransformers, | ||
// unclosed tags include the markdown tags that haven't been closed yet, and their associated formats | ||
unclosedTags) { | ||
// This function handles the case of a string looking like this: " foo " | ||
@@ -153,2 +157,8 @@ // Where it would be invalid markdown to generate: "** foo **" | ||
let output = frozenString; | ||
// the opening tags to be added to the result | ||
let openingTags = ''; | ||
// the closing tags to be added to the result | ||
let closingTags = ''; | ||
const prevNode = getTextSibling(node, true); | ||
const nextNode = getTextSibling(node, false); | ||
const applied = new Set(); | ||
@@ -158,19 +168,35 @@ for (const transformer of textTransformers) { | ||
const tag = transformer.tag; | ||
// dedup applied formats | ||
if (hasFormat(node, format) && !applied.has(format)) { | ||
// Multiple tags might be used for the same format (*, _) | ||
applied.add(format); | ||
// Prevent adding opening tag is already opened by the previous sibling | ||
const previousNode = getTextSibling(node, true); | ||
if (!hasFormat(previousNode, format)) { | ||
output = tag + output; | ||
// append the tag to openningTags, if it's not applied to the previous nodes, | ||
// or the nodes before that (which would result in an unclosed tag) | ||
if (!hasFormat(prevNode, format) || !unclosedTags.find(element => element.tag === tag)) { | ||
unclosedTags.push({ | ||
format, | ||
tag | ||
}); | ||
openingTags += tag; | ||
} | ||
} | ||
} | ||
// Prevent adding closing tag if next sibling will do it | ||
const nextNode = getTextSibling(node, false); | ||
if (!hasFormat(nextNode, format)) { | ||
output += tag; | ||
// close any tags in the same order they were applied, if necessary | ||
for (let i = 0; i < unclosedTags.length; i++) { | ||
// prevent adding closing tag if next sibling will do it | ||
if (hasFormat(nextNode, unclosedTags[i].format)) { | ||
continue; | ||
} | ||
while (unclosedTags.length > i) { | ||
const unclosedTag = unclosedTags.pop(); | ||
if (unclosedTag && typeof unclosedTag.tag === 'string') { | ||
closingTags += unclosedTag.tag; | ||
} | ||
} | ||
break; | ||
} | ||
output = openingTags + output + closingTags; | ||
// Replace trimmed version of textContent ensuring surrounding whitespace is not modified | ||
@@ -177,0 +203,0 @@ return textContent.replace(frozenString, () => output); |
@@ -9,35 +9,35 @@ /** | ||
'use strict';var p=require("lexical"),z=require("@lexical/list"),E=require("@lexical/rich-text"),aa=require("@lexical/utils"),F=require("@lexical/code"),G=require("@lexical/link");function I(a,c){let b={};for(let f of a)(a=c(f))&&(b[a]?b[a].push(f):b[a]=[f]);return b}function J(a){a=I(a,c=>c.type);return{element:a.element||[],multilineElement:a["multiline-element"]||[],textFormat:a["text-format"]||[],textMatch:a["text-match"]||[]}}let K=/[!-/:-@[-`{-~\s]/,ba=/^\s{0,3}$/; | ||
function L(a){if(!p.$isParagraphNode(a))return!1;let c=a.getFirstChild();return null==c||1===a.getChildrenSize()&&p.$isTextNode(c)&&ba.test(c.getTextContent())}function ca(a,c=!1){let b=J(a),f=[...b.multilineElement,...b.element],e=!c,d=b.textFormat.filter(g=>1===g.format.length);return g=>{let n=[];g=(g||p.$getRoot()).getChildren();for(let h=0;h<g.length;h++){let t=g[h],y=da(t,f,d,b.textMatch);null!=y&&n.push(e&&0<h&&!L(t)&&!L(g[h-1])?"\n".concat(y):y)}return n.join("\n")}} | ||
function da(a,c,b,f){for(let e of c)if(e.export&&(c=e.export(a,d=>M(d,b,f)),null!=c))return c;return p.$isElementNode(a)?M(a,b,f):p.$isDecoratorNode(a)?a.getTextContent():null} | ||
function M(a,c,b){let f=[];a=a.getChildren();a:for(let e of a){for(let d of b)if(d.export&&(a=d.export(e,g=>M(g,c,b),(g,n)=>N(g,n,c)),null!=a)){f.push(a);continue a}p.$isLineBreakNode(e)?f.push("\n"):p.$isTextNode(e)?f.push(N(e,e.getTextContent(),c)):p.$isElementNode(e)?f.push(M(e,c,b)):p.$isDecoratorNode(e)&&f.push(e.getTextContent())}return f.join("")} | ||
function N(a,c,b){let f=c.trim(),e=f,d=new Set;for(let n of b){b=n.format[0];let h=n.tag;if(O(a,b)&&!d.has(b)){d.add(b);var g=P(a,!0);O(g,b)||(e=h+e);g=P(a,!1);O(g,b)||(e+=h)}}return c.replace(f,()=>e)} | ||
function P(a,c){let b=c?a.getPreviousSibling():a.getNextSibling();b||(a=a.getParentOrThrow(),a.isInline()&&(b=c?a.getPreviousSibling():a.getNextSibling()));for(;b;){if(p.$isElementNode(b)){if(!b.isInline())break;a=c?b.getLastDescendant():b.getFirstDescendant();if(p.$isTextNode(a))return a;b=c?b.getPreviousSibling():b.getNextSibling()}if(p.$isTextNode(b))return b;if(!p.$isElementNode(b))break}return null}function O(a,c){return p.$isTextNode(a)&&a.hasFormat(c)} | ||
'use strict';var p=require("lexical"),z=require("@lexical/list"),E=require("@lexical/rich-text"),aa=require("@lexical/utils"),F=require("@lexical/code"),G=require("@lexical/link");function I(a,c){let b={};for(let e of a)(a=c(e))&&(b[a]?b[a].push(e):b[a]=[e]);return b}function J(a){a=I(a,c=>c.type);return{element:a.element||[],multilineElement:a["multiline-element"]||[],textFormat:a["text-format"]||[],textMatch:a["text-match"]||[]}}let K=/[!-/:-@[-`{-~\s]/,ba=/^\s{0,3}$/; | ||
function L(a){if(!p.$isParagraphNode(a))return!1;let c=a.getFirstChild();return null==c||1===a.getChildrenSize()&&p.$isTextNode(c)&&ba.test(c.getTextContent())}function ca(a,c=!1){let b=J(a),e=[...b.multilineElement,...b.element],f=!c,d=b.textFormat.filter(g=>1===g.format.length);return g=>{let n=[];g=(g||p.$getRoot()).getChildren();for(let h=0;h<g.length;h++){let r=g[h],x=da(r,e,d,b.textMatch);null!=x&&n.push(f&&0<h&&!L(r)&&!L(g[h-1])?"\n".concat(x):x)}return n.join("\n")}} | ||
function da(a,c,b,e){for(let f of c)if(f.export&&(c=f.export(a,d=>M(d,b,e)),null!=c))return c;return p.$isElementNode(a)?M(a,b,e):p.$isDecoratorNode(a)?a.getTextContent():null} | ||
function M(a,c,b){let e=[];a=a.getChildren();let f=[];a:for(let d of a){for(let g of b)if(g.export&&(a=g.export(d,n=>M(n,c,b),(n,h)=>N(n,h,c,f)),null!=a)){e.push(a);continue a}p.$isLineBreakNode(d)?e.push("\n"):p.$isTextNode(d)?e.push(N(d,d.getTextContent(),c,f)):p.$isElementNode(d)?e.push(M(d,c,b)):p.$isDecoratorNode(d)&&e.push(d.getTextContent())}return e.join("")} | ||
function N(a,c,b,e){let f=c.trim(),d=f,g="",n="",h=O(a,!0);var r=O(a,!1);let x=new Set;for(let w of b){b=w.format[0];let q=w.tag;P(a,b)&&!x.has(b)&&(x.add(b),P(h,b)&&e.find(l=>l.tag===q)||(e.push({format:b,tag:q}),g+=q))}for(a=0;a<e.length;a++)if(!P(r,e[a].format)){for(;e.length>a;)(r=e.pop())&&"string"===typeof r.tag&&(n+=r.tag);break}d=g+d+n;return c.replace(f,()=>d)} | ||
function O(a,c){let b=c?a.getPreviousSibling():a.getNextSibling();b||(a=a.getParentOrThrow(),a.isInline()&&(b=c?a.getPreviousSibling():a.getNextSibling()));for(;b;){if(p.$isElementNode(b)){if(!b.isInline())break;a=c?b.getLastDescendant():b.getFirstDescendant();if(p.$isTextNode(a))return a;b=c?b.getPreviousSibling():b.getNextSibling()}if(p.$isTextNode(b))return b;if(!p.$isElementNode(b))break}return null}function P(a,c){return p.$isTextNode(a)&&a.hasFormat(c)} | ||
let Q="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,ea=Q&&"documentMode"in document?document.documentMode:null;Q&&"InputEvent"in window&&!ea?"getTargetRanges"in new window.InputEvent("input"):!1; | ||
let R=Q&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),S=Q&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,fa=Q&&/^(?=.*Chrome).*/i.test(navigator.userAgent),T=Q&&/AppleWebKit\/[\d.]+/.test(navigator.userAgent)&&!fa; | ||
function ha(a,c=!1){let b=J(a),f=ia(b.textFormat);return(e,d)=>{e=e.split("\n");let g=e.length;d=d||p.$getRoot();d.clear();for(let u=0;u<g;u++){var n=e[u];a:{var h=e;var t=u,y=b.multilineElement,w=d;for(var q of y){let {handleImportAfterStartMatch:B,regExpEnd:x,regExpStart:A,replace:H}=q;if(y=h[t].match(A)){if(B){var m=B({lines:h,rootNode:w,startLineIndex:t,startMatch:y,transformer:q});if(null===m)continue;else if(m){h=m;break a}}var l="object"===typeof x&&"regExp"in x?x.regExp:x,k=x&&"object"=== | ||
typeof x&&"optional"in x?x.optional:!x;m=t;for(var r=h.length;m<r;){var v=l?h[m].match(l):null;if(v||k&&!(k&&m<r-1))if(v&&t===m&&v.index===y.index)m++;else{l=[];if(v&&t===m)l.push(h[t].slice(y[0].length,-v[0].length));else for(k=t;k<=m;k++)k===t?(r=h[k].slice(y[0].length),l.push(r)):k===m&&v?(r=h[k].slice(0,-v[0].length),l.push(r)):l.push(h[k]);if(!1!==H(w,null,y,v,l,!0)){h=[!0,m];break a}break}else m++}}}h=[!1,t]}let [D,C]=h;if(D)u=C;else{h=n;m=d;v=b.element;t=f;w=b.textMatch;y=p.$createTextNode(h); | ||
n=p.$createParagraphNode();n.append(y);m.append(n);for(let {regExp:B,replace:x}of v)if(m=h.match(B))if(y.setTextContent(h.slice(m[0].length)),!1!==x(n,[y],m,!0))break;U(y,t,w);n.isAttached()&&0<h.length&&(h=n.getPreviousSibling(),p.$isParagraphNode(h)||E.$isQuoteNode(h)||z.$isListNode(h))&&(t=h,z.$isListNode(h)&&(h=h.getLastDescendant(),t=null==h?null:aa.$findMatchingParent(h,z.$isListItemNode)),null!=t&&0<t.getTextContentSize()&&(t.splice(t.getChildrenSize(),0,[p.$createLineBreakNode(),...n.getChildren()]), | ||
n.remove()))}}q=d.getChildren();for(let u of q)!c&&L(u)&&1<d.getChildrenSize()&&u.remove();null!==p.$getSelection()&&d.selectStart()}}function U(a,c,b){var f=a.getTextContent();let e=ja(f,c);if(e){var d,g;if(e[0]===f)var n=a;else{f=e.index||0;let h=f+e[0].length;0===f?[n,d]=a.splitText(h):[g,n,d]=a.splitText(f,h)}n.setTextContent(e[2]);if(a=c.transformersByTag[e[1]])for(let h of a.format)n.hasFormat(h)||n.toggleFormat(h);n.hasFormat("code")||U(n,c,b);g&&U(g,c,b);d&&U(d,c,b)}else ka(a,b)} | ||
function ka(a,c){a:for(;a;){for(let b of c){if(!b.replace||!b.importRegExp)continue;let f=a.getTextContent().match(b.importRegExp);if(!f)continue;let e=f.index||0,d=b.getEndIndex?b.getEndIndex(a,f):e+f[0].length;if(!1===d)continue;let g,n;0===e?[g,a]=a.splitText(d):[,g,n]=a.splitText(e,d);n&&ka(n,c);b.replace(g,f);continue a}break}} | ||
function ja(a,c){var b=a.match(c.openTagsRegExp);if(null==b)return null;for(let d of b){var f=d.replace(/^\s/,"");b=c.fullMatchRegExpByTag[f];if(null!=b&&(b=a.match(b),f=c.transformersByTag[f],null!=b&&null!=f)){if(!1!==f.intraword)return b;var {index:e=0}=b;f=a[e-1];e=a[e+b[0].length];if(!(f&&!K.test(f)||e&&!K.test(e)))return b}}return null} | ||
function ia(a){let c={},b={},f=[];for(let e of a){({tag:a}=e);c[a]=e;let d=a.replace(/(\*|\^|\+)/g,"\\$1");f.push(d);b[a]=R||S||T?new RegExp(`(${d})(?![${d}\\s])(.*?[^${d}\\s])${d}(?!${d})`):new RegExp(`(?<![\\\\${d}])(${d})((\\\\${d})?.*?[^${d}\\s](\\\\${d})?)((?<!\\\\)|(?<=\\\\\\\\))(${d})(?![\\\\${d}])`)}return{fullMatchRegExpByTag:b,openTagsRegExp:new RegExp((R||S||T?"":"(?<![\\\\])")+"("+f.join("|")+")","g"),transformersByTag:c}}var la; | ||
function ha(a,c=!1){let b=J(a),e=ia(b.textFormat);return(f,d)=>{f=f.split("\n");let g=f.length;d=d||p.$getRoot();d.clear();for(let u=0;u<g;u++){var n=f[u];a:{var h=f;var r=u,x=b.multilineElement,w=d;for(var q of x){let {handleImportAfterStartMatch:B,regExpEnd:y,regExpStart:A,replace:H}=q;if(x=h[r].match(A)){if(B){var l=B({lines:h,rootNode:w,startLineIndex:r,startMatch:x,transformer:q});if(null===l)continue;else if(l){h=l;break a}}var m="object"===typeof y&&"regExp"in y?y.regExp:y,k=y&&"object"=== | ||
typeof y&&"optional"in y?y.optional:!y;l=r;for(var t=h.length;l<t;){var v=m?h[l].match(m):null;if(v||k&&!(k&&l<t-1))if(v&&r===l&&v.index===x.index)l++;else{m=[];if(v&&r===l)m.push(h[r].slice(x[0].length,-v[0].length));else for(k=r;k<=l;k++)k===r?(t=h[k].slice(x[0].length),m.push(t)):k===l&&v?(t=h[k].slice(0,-v[0].length),m.push(t)):m.push(h[k]);if(!1!==H(w,null,x,v,m,!0)){h=[!0,l];break a}break}else l++}}}h=[!1,r]}let [D,C]=h;if(D)u=C;else{h=n;l=d;v=b.element;r=e;w=b.textMatch;x=p.$createTextNode(h); | ||
n=p.$createParagraphNode();n.append(x);l.append(n);for(let {regExp:B,replace:y}of v)if(l=h.match(B))if(x.setTextContent(h.slice(l[0].length)),!1!==y(n,[x],l,!0))break;U(x,r,w);n.isAttached()&&0<h.length&&(h=n.getPreviousSibling(),p.$isParagraphNode(h)||E.$isQuoteNode(h)||z.$isListNode(h))&&(r=h,z.$isListNode(h)&&(h=h.getLastDescendant(),r=null==h?null:aa.$findMatchingParent(h,z.$isListItemNode)),null!=r&&0<r.getTextContentSize()&&(r.splice(r.getChildrenSize(),0,[p.$createLineBreakNode(),...n.getChildren()]), | ||
n.remove()))}}q=d.getChildren();for(let u of q)!c&&L(u)&&1<d.getChildrenSize()&&u.remove();null!==p.$getSelection()&&d.selectStart()}}function U(a,c,b){var e=a.getTextContent();let f=ja(e,c);if(f){var d,g;if(f[0]===e)var n=a;else{e=f.index||0;let h=e+f[0].length;0===e?[n,d]=a.splitText(h):[g,n,d]=a.splitText(e,h)}n.setTextContent(f[2]);if(a=c.transformersByTag[f[1]])for(let h of a.format)n.hasFormat(h)||n.toggleFormat(h);n.hasFormat("code")||U(n,c,b);g&&U(g,c,b);d&&U(d,c,b)}else ka(a,b)} | ||
function ka(a,c){a:for(;a;){for(let b of c){if(!b.replace||!b.importRegExp)continue;let e=a.getTextContent().match(b.importRegExp);if(!e)continue;let f=e.index||0,d=b.getEndIndex?b.getEndIndex(a,e):f+e[0].length;if(!1===d)continue;let g,n;0===f?[g,a]=a.splitText(d):[,g,n]=a.splitText(f,d);n&&ka(n,c);b.replace(g,e);continue a}break}} | ||
function ja(a,c){var b=a.match(c.openTagsRegExp);if(null==b)return null;for(let d of b){var e=d.replace(/^\s/,"");b=c.fullMatchRegExpByTag[e];if(null!=b&&(b=a.match(b),e=c.transformersByTag[e],null!=b&&null!=e)){if(!1!==e.intraword)return b;var {index:f=0}=b;e=a[f-1];f=a[f+b[0].length];if(!(e&&!K.test(e)||f&&!K.test(f)))return b}}return null} | ||
function ia(a){let c={},b={},e=[];for(let f of a){({tag:a}=f);c[a]=f;let d=a.replace(/(\*|\^|\+)/g,"\\$1");e.push(d);b[a]=R||S||T?new RegExp(`(${d})(?![${d}\\s])(.*?[^${d}\\s])${d}(?!${d})`):new RegExp(`(?<![\\\\${d}])(${d})((\\\\${d})?.*?[^${d}\\s](\\\\${d})?)((?<!\\\\)|(?<=\\\\\\\\))(${d})(?![\\\\${d}])`)}return{fullMatchRegExpByTag:b,openTagsRegExp:new RegExp((R||S||T?"":"(?<![\\\\])")+"("+e.join("|")+")","g"),transformersByTag:c}}var la; | ||
function V(a){let c=new URLSearchParams;c.append("code",a);for(let b=1;b<arguments.length;b++)c.append("v",arguments[b]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${c} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}la=V&&V.__esModule&&Object.prototype.hasOwnProperty.call(V,"default")?V["default"]:V; | ||
function ma(a,c,b){let f=b.length;for(;c>=f;c--){let e=c-f;if(na(a,e,b,0,f)&&" "!==a[e+f])return e}return-1}function na(a,c,b,f,e){for(let d=0;d<e;d++)if(a[c+d]!==b[f+d])return!1;return!0} | ||
let oa=/^(\s*)(\d{1,})\.\s/,pa=/^(\s*)[-*+]\s/,qa=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,W=/^(#{1,6})\s/,ra=/^>\s/,sa=/^[ \t]*```(\w+)?/,ta=/[ \t]*```$/,ua=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,va=/^(?:\|)(.+)(?:\|)\s?$/,wa=/^(\| ?:?-*:? ?)+\|\s?$/,xa=a=>(c,b,f)=>{f=a(f);f.append(...b);c.replace(f);f.select(0,0)},X=a=>(c,b,f)=>{var e=c.getPreviousSibling(),d=c.getNextSibling();const g=z.$createListItemNode("check"===a?"x"===f[3]:void 0);z.$isListNode(d)&&d.getListType()===a?(e=d.getFirstChild(), | ||
null!==e?e.insertBefore(g):d.append(g),c.remove()):z.$isListNode(e)&&e.getListType()===a?(e.append(g),c.remove()):(d=z.$createListNode(a,"number"===a?Number(f[2]):void 0),d.append(g),c.replace(d));g.append(...b);g.select(0,0);b=f[1];c=b.match(/\t/g);b=b.match(/ /g);f=0;c&&(f+=c.length);b&&(f+=Math.floor(b.length/4));(c=f)&&g.setIndent(c)},Y=(a,c,b)=>{const f=[];var e=a.getChildren();let d=0;for(const n of e)if(z.$isListItemNode(n)){if(1===n.getChildrenSize()&&(e=n.getFirstChild(),z.$isListNode(e))){f.push(Y(e, | ||
c,b+1));continue}e=" ".repeat(4*b);var g=a.getListType();g="number"===g?`${a.getStart()+d}. `:"check"===g?`- [${n.getChecked()?"x":" "}] `:"- ";f.push(e+g+c(n));d++}return f.join("\n")},ya={dependencies:[E.HeadingNode],export:(a,c)=>{if(!E.$isHeadingNode(a))return null;const b=Number(a.getTag().slice(1));return"#".repeat(b)+" "+c(a)},regExp:W,replace:xa(a=>E.$createHeadingNode("h"+a[1].length)),type:"element"},za={dependencies:[E.QuoteNode],export:(a,c)=>{if(!E.$isQuoteNode(a))return null;a=c(a).split("\n"); | ||
c=[];for(const b of a)c.push("> "+b);return c.join("\n")},regExp:ra,replace:(a,c,b,f)=>{if(f&&(b=a.getPreviousSibling(),E.$isQuoteNode(b))){b.splice(b.getChildrenSize(),0,[p.$createLineBreakNode(),...c]);b.select(0,0);a.remove();return}b=E.$createQuoteNode();b.append(...c);a.replace(b);b.select(0,0)},type:"element"},Aa={dependencies:[F.CodeNode],export:a=>{if(!F.$isCodeNode(a))return null;const c=a.getTextContent();return"```"+(a.getLanguage()||"")+(c?"\n"+c:"")+"\n```"},regExpEnd:{optional:!0,regExp:ta}, | ||
regExpStart:sa,replace:(a,c,b,f,e,d)=>{if(!c&&e){if(1===e.length)f?(c=F.$createCodeNode(),b=b[1]+e[0]):(c=F.$createCodeNode(b[1]),b=e[0].startsWith(" ")?e[0].slice(1):e[0]);else{c=F.$createCodeNode(b[1]);if(0===e[0].trim().length)for(;0<e.length&&!e[0].length;)e.shift();else e[0]=e[0].startsWith(" ")?e[0].slice(1):e[0];for(;0<e.length&&!e[e.length-1].length;)e.pop();b=e.join("\n")}b=p.$createTextNode(b);c.append(b);a.append(c)}else c&&xa(g=>F.$createCodeNode(g?g[1]:void 0))(a,c,b,d)},type:"multiline-element"}, | ||
function ma(a,c,b){let e=b.length;for(;c>=e;c--){let f=c-e;if(na(a,f,b,0,e)&&" "!==a[f+e])return f}return-1}function na(a,c,b,e,f){for(let d=0;d<f;d++)if(a[c+d]!==b[e+d])return!1;return!0} | ||
let oa=/^(\s*)(\d{1,})\.\s/,pa=/^(\s*)[-*+]\s/,qa=/^(\s*)(?:-\s)?\s?(\[(\s|x)?\])\s/i,W=/^(#{1,6})\s/,ra=/^>\s/,sa=/^[ \t]*```(\w+)?/,ta=/[ \t]*```$/,ua=/^[ \t]*```[^`]+(?:(?:`{1,2}|`{4,})[^`]+)*```(?:[^`]|$)/,va=/^(?:\|)(.+)(?:\|)\s?$/,wa=/^(\| ?:?-*:? ?)+\|\s?$/,xa=a=>(c,b,e)=>{e=a(e);e.append(...b);c.replace(e);e.select(0,0)},X=a=>(c,b,e)=>{var f=c.getPreviousSibling(),d=c.getNextSibling();const g=z.$createListItemNode("check"===a?"x"===e[3]:void 0);z.$isListNode(d)&&d.getListType()===a?(f=d.getFirstChild(), | ||
null!==f?f.insertBefore(g):d.append(g),c.remove()):z.$isListNode(f)&&f.getListType()===a?(f.append(g),c.remove()):(d=z.$createListNode(a,"number"===a?Number(e[2]):void 0),d.append(g),c.replace(d));g.append(...b);g.select(0,0);b=e[1];c=b.match(/\t/g);b=b.match(/ /g);e=0;c&&(e+=c.length);b&&(e+=Math.floor(b.length/4));(c=e)&&g.setIndent(c)},Y=(a,c,b)=>{const e=[];var f=a.getChildren();let d=0;for(const n of f)if(z.$isListItemNode(n)){if(1===n.getChildrenSize()&&(f=n.getFirstChild(),z.$isListNode(f))){e.push(Y(f, | ||
c,b+1));continue}f=" ".repeat(4*b);var g=a.getListType();g="number"===g?`${a.getStart()+d}. `:"check"===g?`- [${n.getChecked()?"x":" "}] `:"- ";e.push(f+g+c(n));d++}return e.join("\n")},ya={dependencies:[E.HeadingNode],export:(a,c)=>{if(!E.$isHeadingNode(a))return null;const b=Number(a.getTag().slice(1));return"#".repeat(b)+" "+c(a)},regExp:W,replace:xa(a=>E.$createHeadingNode("h"+a[1].length)),type:"element"},za={dependencies:[E.QuoteNode],export:(a,c)=>{if(!E.$isQuoteNode(a))return null;a=c(a).split("\n"); | ||
c=[];for(const b of a)c.push("> "+b);return c.join("\n")},regExp:ra,replace:(a,c,b,e)=>{if(e&&(b=a.getPreviousSibling(),E.$isQuoteNode(b))){b.splice(b.getChildrenSize(),0,[p.$createLineBreakNode(),...c]);b.select(0,0);a.remove();return}b=E.$createQuoteNode();b.append(...c);a.replace(b);b.select(0,0)},type:"element"},Aa={dependencies:[F.CodeNode],export:a=>{if(!F.$isCodeNode(a))return null;const c=a.getTextContent();return"```"+(a.getLanguage()||"")+(c?"\n"+c:"")+"\n```"},regExpEnd:{optional:!0,regExp:ta}, | ||
regExpStart:sa,replace:(a,c,b,e,f,d)=>{if(!c&&f){if(1===f.length)e?(c=F.$createCodeNode(),b=b[1]+f[0]):(c=F.$createCodeNode(b[1]),b=f[0].startsWith(" ")?f[0].slice(1):f[0]);else{c=F.$createCodeNode(b[1]);if(0===f[0].trim().length)for(;0<f.length&&!f[0].length;)f.shift();else f[0]=f[0].startsWith(" ")?f[0].slice(1):f[0];for(;0<f.length&&!f[f.length-1].length;)f.pop();b=f.join("\n")}b=p.$createTextNode(b);c.append(b);a.append(c)}else c&&xa(g=>F.$createCodeNode(g?g[1]:void 0))(a,c,b,d)},type:"multiline-element"}, | ||
Ba={dependencies:[z.ListNode,z.ListItemNode],export:(a,c)=>z.$isListNode(a)?Y(a,c,0):null,regExp:pa,replace:X("bullet"),type:"element"},Ca={dependencies:[z.ListNode,z.ListItemNode],export:(a,c)=>z.$isListNode(a)?Y(a,c,0):null,regExp:qa,replace:X("check"),type:"element"},Da={dependencies:[z.ListNode,z.ListItemNode],export:(a,c)=>z.$isListNode(a)?Y(a,c,0):null,regExp:oa,replace:X("number"),type:"element"},Ea={format:["code"],tag:"`",type:"text-format"},Ga={format:["highlight"],tag:"==",type:"text-format"}, | ||
Ha={format:["bold","italic"],tag:"***",type:"text-format"},Ia={format:["bold","italic"],intraword:!1,tag:"___",type:"text-format"},Ja={format:["bold"],tag:"**",type:"text-format"},Ka={format:["bold"],intraword:!1,tag:"__",type:"text-format"},La={format:["strikethrough"],tag:"~~",type:"text-format"},Ma={format:["italic"],tag:"*",type:"text-format"},Na={format:["italic"],intraword:!1,tag:"_",type:"text-format"},Oa={dependencies:[G.LinkNode],export:(a,c,b)=>{if(!G.$isLinkNode(a))return null;c=(c=a.getTitle())? | ||
`[${a.getTextContent()}](${a.getURL()} "${c}")`:`[${a.getTextContent()}](${a.getURL()})`;const f=a.getFirstChild();return 1===a.getChildrenSize()&&p.$isTextNode(f)?b(f,c):c},importRegExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(a,c)=>{const [,b,f,e]=c;c=G.$createLinkNode(f,{title:e});const d=p.$createTextNode(b);d.setFormat(a.getFormat());c.append(d);a.replace(c)},trigger:")",type:"text-match"}; | ||
function Pa(a,c=!1){a=a.split("\n");let b=!1,f=[];for(let e=0;e<a.length;e++){let d=a[e],g=f[f.length-1];ua.test(d)?f.push(d):sa.test(d)||ta.test(d)?(b=!b,f.push(d)):b?f.push(d):""===d||""===g||!g||W.test(g)||W.test(d)||ra.test(d)||oa.test(d)||pa.test(d)||qa.test(d)||va.test(d)||wa.test(d)||!c?f.push(d):f[f.length-1]=g+d}return f.join("\n")}let Qa=[ya,za,Ba,Da],Ra=[Aa],Sa=[Ea,Ha,Ia,Ja,Ka,Ga,Ma,Na,La],Ta=[Oa],Z=[...Qa,...Ra,...Sa,...Ta]; | ||
exports.$convertFromMarkdownString=function(a,c=Z,b,f=!1,e=!1){a=f?a:Pa(a,e);return ha(c,f)(a,b)};exports.$convertToMarkdownString=function(a=Z,c,b=!1){return ca(a,b)(c)};exports.BOLD_ITALIC_STAR=Ha;exports.BOLD_ITALIC_UNDERSCORE=Ia;exports.BOLD_STAR=Ja;exports.BOLD_UNDERSCORE=Ka;exports.CHECK_LIST=Ca;exports.CODE=Aa;exports.ELEMENT_TRANSFORMERS=Qa;exports.HEADING=ya;exports.HIGHLIGHT=Ga;exports.INLINE_CODE=Ea;exports.ITALIC_STAR=Ma;exports.ITALIC_UNDERSCORE=Na;exports.LINK=Oa; | ||
`[${a.getTextContent()}](${a.getURL()} "${c}")`:`[${a.getTextContent()}](${a.getURL()})`;const e=a.getFirstChild();return 1===a.getChildrenSize()&&p.$isTextNode(e)?b(e,c):c},importRegExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))/,regExp:/(?:\[([^[]+)\])(?:\((?:([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?)\))$/,replace:(a,c)=>{const [,b,e,f]=c;c=G.$createLinkNode(e,{title:f});const d=p.$createTextNode(b);d.setFormat(a.getFormat());c.append(d);a.replace(c)},trigger:")",type:"text-match"}; | ||
function Pa(a,c=!1){a=a.split("\n");let b=!1,e=[];for(let f=0;f<a.length;f++){let d=a[f],g=e[e.length-1];ua.test(d)?e.push(d):sa.test(d)||ta.test(d)?(b=!b,e.push(d)):b?e.push(d):""===d||""===g||!g||W.test(g)||W.test(d)||ra.test(d)||oa.test(d)||pa.test(d)||qa.test(d)||va.test(d)||wa.test(d)||!c?e.push(d):e[e.length-1]=g+d}return e.join("\n")}let Qa=[ya,za,Ba,Da],Ra=[Aa],Sa=[Ea,Ha,Ia,Ja,Ka,Ga,Ma,Na,La],Ta=[Oa],Z=[...Qa,...Ra,...Sa,...Ta]; | ||
exports.$convertFromMarkdownString=function(a,c=Z,b,e=!1,f=!1){a=e?a:Pa(a,f);return ha(c,e)(a,b)};exports.$convertToMarkdownString=function(a=Z,c,b=!1){return ca(a,b)(c)};exports.BOLD_ITALIC_STAR=Ha;exports.BOLD_ITALIC_UNDERSCORE=Ia;exports.BOLD_STAR=Ja;exports.BOLD_UNDERSCORE=Ka;exports.CHECK_LIST=Ca;exports.CODE=Aa;exports.ELEMENT_TRANSFORMERS=Qa;exports.HEADING=ya;exports.HIGHLIGHT=Ga;exports.INLINE_CODE=Ea;exports.ITALIC_STAR=Ma;exports.ITALIC_UNDERSCORE=Na;exports.LINK=Oa; | ||
exports.MULTILINE_ELEMENT_TRANSFORMERS=Ra;exports.ORDERED_LIST=Da;exports.QUOTE=za;exports.STRIKETHROUGH=La;exports.TEXT_FORMAT_TRANSFORMERS=Sa;exports.TEXT_MATCH_TRANSFORMERS=Ta;exports.TRANSFORMERS=Z;exports.UNORDERED_LIST=Ba; | ||
exports.registerMarkdownShortcuts=function(a,c=Z){let b=J(c),f=I(b.textFormat,({tag:d})=>d[d.length-1]),e=I(b.textMatch,({trigger:d})=>d);for(let d of c)if(c=d.type,"element"===c||"text-match"===c||"multiline-element"===c){c=d.dependencies;for(let g of c)a.hasNode(g)||la(173,g.getType())}return a.registerUpdateListener(({tags:d,dirtyLeaves:g,editorState:n,prevEditorState:h})=>{if(!d.has("collaboration")&&!d.has("historic")&&!a.isComposing()){var t=n.read(p.$getSelection);d=h.read(p.$getSelection); | ||
if(p.$isRangeSelection(d)&&p.$isRangeSelection(t)&&t.isCollapsed()&&!t.is(d)){h=t.anchor.key;var y=t.anchor.offset,w=n._nodeMap.get(h);!p.$isTextNode(w)||!g.has(h)||1!==y&&y>d.anchor.offset+1||a.update(()=>{if(!w.hasFormat("code")){var q=w.getParent();if(null!==q&&!F.$isCodeNode(q)){var m=t.anchor.offset;b:{var l=b.element;var k=q.getParent();if(p.$isRootOrShadowRoot(k)&&q.getFirstChild()===w&&(k=w.getTextContent()," "===k[m-1]))for(let {regExp:B,replace:x}of l)if((l=k.match(B))&&l[0].length===(l[0].endsWith(" ")? | ||
m:m-1)){var r=w.getNextSiblings();let [A,H]=w.splitText(m);A.remove();r=H?[H,...r]:r;if(!1!==x(q,r,l,!1)){k=!0;break b}}k=!1}if(!k){b:{l=b.multilineElement;k=q.getParent();if(p.$isRootOrShadowRoot(k)&&q.getFirstChild()===w&&(k=w.getTextContent()," "===k[m-1]))for(let {regExpStart:B,replace:x,regExpEnd:A}of l)if(!(A&&!("optional"in A)||A&&"optional"in A&&!A.optional)&&(l=k.match(B))&&l[0].length===(l[0].endsWith(" ")?m:m-1)){r=w.getNextSiblings();let [H,Fa]=w.splitText(m);H.remove();r=Fa?[Fa,...r]: | ||
r;if(!1!==x(q,r,l,null,null,!1)){q=!0;break b}}q=!1}if(!q){b:{k=w.getTextContent();q=e[k[m-1]];if(null!=q){m<k.length&&(k=k.slice(0,m));for(u of q)if(u.replace&&u.regExp&&(q=k.match(u.regExp),null!==q)){k=q.index||0;l=k+q[0].length;var v=void 0;0===k?[v]=w.splitText(l):[,v]=w.splitText(k,l);v.selectNext(0,0);u.replace(v,q);var u=!0;break b}}u=!1}if(!u)b:if(l=w.getTextContent(),--m,r=l[m],u=f[r])for(let B of u){var {tag:D}=B;u=D.length;let x=m-u+1;if(!(1<u&&!na(l,x,D,0,u)||" "===l[x-1])&&(v=l[m+1], | ||
!1!==B.intraword||!v||K.test(v))){q=v=w;k=ma(l,x,D);for(var C=q;0>k&&(C=C.getPreviousSibling())&&!p.$isLineBreakNode(C);)p.$isTextNode(C)&&(k=C.getTextContent(),q=C,k=ma(k,k.length,D));if(!(0>k||q===v&&k+u===x||(D=q.getTextContent(),0<k&&D[k-1]===r||(C=D[k-1],!1===B.intraword&&C&&!K.test(C))))){l=v.getTextContent();l=l.slice(0,x)+l.slice(m+1);v.setTextContent(l);l=q===v?l:D;q.setTextContent(l.slice(0,k)+l.slice(k+u));l=p.$getSelection();r=p.$createRangeSelection();p.$setSelection(r);m=m-u*(q===v? | ||
2:1)+1;r.anchor.set(q.__key,k,"text");r.focus.set(v.__key,m,"text");for(let A of B.format)r.hasFormat(A)||r.formatText(A);r.anchor.set(r.focus.key,r.focus.offset,r.focus.type);for(let A of B.format)r.hasFormat(A)&&r.toggleFormat(A);p.$isRangeSelection(l)&&(r.format=l.format);break b}}}}}}}})}}})} | ||
exports.registerMarkdownShortcuts=function(a,c=Z){let b=J(c),e=I(b.textFormat,({tag:d})=>d[d.length-1]),f=I(b.textMatch,({trigger:d})=>d);for(let d of c)if(c=d.type,"element"===c||"text-match"===c||"multiline-element"===c){c=d.dependencies;for(let g of c)a.hasNode(g)||la(173,g.getType())}return a.registerUpdateListener(({tags:d,dirtyLeaves:g,editorState:n,prevEditorState:h})=>{if(!d.has("collaboration")&&!d.has("historic")&&!a.isComposing()){var r=n.read(p.$getSelection);d=h.read(p.$getSelection); | ||
if(p.$isRangeSelection(d)&&p.$isRangeSelection(r)&&r.isCollapsed()&&!r.is(d)){h=r.anchor.key;var x=r.anchor.offset,w=n._nodeMap.get(h);!p.$isTextNode(w)||!g.has(h)||1!==x&&x>d.anchor.offset+1||a.update(()=>{if(!w.hasFormat("code")){var q=w.getParent();if(null!==q&&!F.$isCodeNode(q)){var l=r.anchor.offset;b:{var m=b.element;var k=q.getParent();if(p.$isRootOrShadowRoot(k)&&q.getFirstChild()===w&&(k=w.getTextContent()," "===k[l-1]))for(let {regExp:B,replace:y}of m)if((m=k.match(B))&&m[0].length===(m[0].endsWith(" ")? | ||
l:l-1)){var t=w.getNextSiblings();let [A,H]=w.splitText(l);A.remove();t=H?[H,...t]:t;if(!1!==y(q,t,m,!1)){k=!0;break b}}k=!1}if(!k){b:{m=b.multilineElement;k=q.getParent();if(p.$isRootOrShadowRoot(k)&&q.getFirstChild()===w&&(k=w.getTextContent()," "===k[l-1]))for(let {regExpStart:B,replace:y,regExpEnd:A}of m)if(!(A&&!("optional"in A)||A&&"optional"in A&&!A.optional)&&(m=k.match(B))&&m[0].length===(m[0].endsWith(" ")?l:l-1)){t=w.getNextSiblings();let [H,Fa]=w.splitText(l);H.remove();t=Fa?[Fa,...t]: | ||
t;if(!1!==y(q,t,m,null,null,!1)){q=!0;break b}}q=!1}if(!q){b:{k=w.getTextContent();q=f[k[l-1]];if(null!=q){l<k.length&&(k=k.slice(0,l));for(u of q)if(u.replace&&u.regExp&&(q=k.match(u.regExp),null!==q)){k=q.index||0;m=k+q[0].length;var v=void 0;0===k?[v]=w.splitText(m):[,v]=w.splitText(k,m);v.selectNext(0,0);u.replace(v,q);var u=!0;break b}}u=!1}if(!u)b:if(m=w.getTextContent(),--l,t=m[l],u=e[t])for(let B of u){var {tag:D}=B;u=D.length;let y=l-u+1;if(!(1<u&&!na(m,y,D,0,u)||" "===m[y-1])&&(v=m[l+1], | ||
!1!==B.intraword||!v||K.test(v))){q=v=w;k=ma(m,y,D);for(var C=q;0>k&&(C=C.getPreviousSibling())&&!p.$isLineBreakNode(C);)p.$isTextNode(C)&&(k=C.getTextContent(),q=C,k=ma(k,k.length,D));if(!(0>k||q===v&&k+u===y||(D=q.getTextContent(),0<k&&D[k-1]===t||(C=D[k-1],!1===B.intraword&&C&&!K.test(C))))){m=v.getTextContent();m=m.slice(0,y)+m.slice(l+1);v.setTextContent(m);m=q===v?m:D;q.setTextContent(m.slice(0,k)+m.slice(k+u));m=p.$getSelection();t=p.$createRangeSelection();p.$setSelection(t);l=l-u*(q===v? | ||
2:1)+1;t.anchor.set(q.__key,k,"text");t.focus.set(v.__key,l,"text");for(let A of B.format)t.hasFormat(A)||t.formatText(A);t.anchor.set(t.focus.key,t.focus.offset,t.focus.type);for(let A of B.format)t.hasFormat(A)&&t.toggleFormat(A);p.$isRangeSelection(m)&&(t.format=m.format);break b}}}}}}}})}}})} |
@@ -11,13 +11,13 @@ { | ||
"license": "MIT", | ||
"version": "0.21.1-nightly.20241213.0", | ||
"version": "0.21.1-nightly.20241216.0", | ||
"main": "LexicalMarkdown.js", | ||
"types": "index.d.ts", | ||
"dependencies": { | ||
"@lexical/code": "0.21.1-nightly.20241213.0", | ||
"@lexical/link": "0.21.1-nightly.20241213.0", | ||
"@lexical/list": "0.21.1-nightly.20241213.0", | ||
"@lexical/rich-text": "0.21.1-nightly.20241213.0", | ||
"@lexical/text": "0.21.1-nightly.20241213.0", | ||
"@lexical/utils": "0.21.1-nightly.20241213.0", | ||
"lexical": "0.21.1-nightly.20241213.0" | ||
"@lexical/code": "0.21.1-nightly.20241216.0", | ||
"@lexical/link": "0.21.1-nightly.20241216.0", | ||
"@lexical/list": "0.21.1-nightly.20241216.0", | ||
"@lexical/rich-text": "0.21.1-nightly.20241216.0", | ||
"@lexical/text": "0.21.1-nightly.20241216.0", | ||
"@lexical/utils": "0.21.1-nightly.20241216.0", | ||
"lexical": "0.21.1-nightly.20241216.0" | ||
}, | ||
@@ -24,0 +24,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155988
2938
+ Added@lexical/clipboard@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/code@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/html@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/link@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/list@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/rich-text@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/selection@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/table@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/text@0.21.1-nightly.20241216.0(transitive)
+ Added@lexical/utils@0.21.1-nightly.20241216.0(transitive)
+ Addedlexical@0.21.1-nightly.20241216.0(transitive)
- Removed@lexical/clipboard@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/code@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/html@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/link@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/list@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/rich-text@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/selection@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/table@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/text@0.21.1-nightly.20241213.0(transitive)
- Removed@lexical/utils@0.21.1-nightly.20241213.0(transitive)
- Removedlexical@0.21.1-nightly.20241213.0(transitive)