@lofcz/platejs-markdown 53.3.4 → 53.4.4

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.
Files changed (2) hide show
  1. package/dist/index.js +35 -6
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1561,17 +1561,26 @@ const defaultRules = {
1561
1561
  },
1562
1562
  callout: {
1563
1563
  deserialize: (mdastNode, deco, options) => {
1564
- const props = parseAttributes(mdastNode.attributes);
1564
+ const { type: calloutType, variant, ...rest } = parseAttributes(mdastNode.attributes);
1565
+ const normalize = (value) => {
1566
+ if (typeof value !== "string") return;
1567
+ const trimmed = value.trim().toLowerCase();
1568
+ return trimmed.length > 0 ? trimmed : void 0;
1569
+ };
1570
+ const resolvedVariant = normalize(calloutType) ?? normalize(variant);
1565
1571
  return {
1566
1572
  children: convertChildrenDeserialize(mdastNode.children, deco, options),
1567
1573
  type: getPluginType(options.editor, KEYS.callout),
1568
- ...props
1574
+ ...resolvedVariant ? { variant: resolvedVariant } : {},
1575
+ ...rest
1569
1576
  };
1570
1577
  },
1571
1578
  serialize(slateNode, options) {
1572
- const { children, type, ...rest } = slateNode;
1579
+ const { children, type: _type, variant, ...rest } = slateNode;
1580
+ const attrs = { ...rest };
1581
+ if (typeof variant === "string" && variant.length > 0) attrs.type = variant;
1573
1582
  return {
1574
- attributes: propsToAttributes(rest),
1583
+ attributes: propsToAttributes(attrs),
1575
1584
  children: convertNodesSerialize(children, options),
1576
1585
  name: "callout",
1577
1586
  type: "mdxJsxFlowElement"
@@ -2398,16 +2407,36 @@ const BOOL_ATTR_REGEXES = Array.from(BOOL_ATTRS.entries()).map(([htmlAttr, jsxAt
2398
2407
  reg: new RegExp(`(\\s|^)${htmlAttr}(\\s|/?>|$)`, "gi")
2399
2408
  }));
2400
2409
  const ATTR_RENAMES = [[/(\s)class=/g, "$1className="], [/(\s)for=/g, "$1htmlFor="]];
2410
+ /**
2411
+ * True HTML void usage (`<br>`, `<img src>`, `<source src>` with no body)
2412
+ * should become self-closing JSX. A matching close tag means the name is
2413
+ * being used as an MDX/JSX component with children — e.g. a custom
2414
+ * `<source>…</source>` excerpt. Forcing `/>` there leaves a stray
2415
+ * `</source>` that remark-mdx throws on; `deserializeMd` then swallows
2416
+ * the error and dumps the entire document as raw paragraphs.
2417
+ *
2418
+ * When another open of the same name appears before the next close, this
2419
+ * open is treated as void (HTML `<source>` inside `<video>` sitting
2420
+ * beside a later paired component).
2421
+ */
2422
+ const shouldForceVoidClose = (source, tagName, afterOpenIndex) => {
2423
+ const rest = source.slice(afterOpenIndex);
2424
+ const nextOpen = rest.search(new RegExp(`<${tagName}\\b`, "i"));
2425
+ const nextClose = rest.search(new RegExp(`<\\/${tagName}\\s*>`, "i"));
2426
+ if (nextClose === -1) return true;
2427
+ if (nextOpen === -1) return false;
2428
+ return nextOpen < nextClose;
2429
+ };
2401
2430
  const htmlToJsx = (html) => {
2402
2431
  if (!html || typeof html !== "string") return html;
2403
- return html.replace(/<!--([\s\S]*?)-->/g, "{/*$1*/}").replace(/<([a-zA-Z0-9]+)\b([^>]*?)(\/?)>/gi, (_match, tagName, attrs, selfClosing) => {
2432
+ return html.replace(/<!--([\s\S]*?)-->/g, "{/*$1*/}").replace(/<([a-zA-Z0-9]+)\b([^>]*?)(\/?)>/gi, (match, tagName, attrs, selfClosing, offset, source) => {
2404
2433
  let a = attrs;
2405
2434
  ATTR_RENAMES.forEach(([pattern, replacement]) => {
2406
2435
  a = a.replace(pattern, replacement);
2407
2436
  });
2408
2437
  a = a.replace(/(^|\s)([a-zA-Z0-9_-]+)=([^{ \t\n\r"'>]+?)(?=\s|\/?>|$)/g, "$1$2=\"$3\"");
2409
2438
  for (const { reg, jsxAttr } of BOOL_ATTR_REGEXES) a = a.replace(reg, `$1${jsxAttr}="true"$2`);
2410
- const closing = VOID_ELEMENTS.has(tagName.toLowerCase()) ? " /" : selfClosing;
2439
+ const closing = VOID_ELEMENTS.has(tagName.toLowerCase()) && (selfClosing === "/" || shouldForceVoidClose(source, tagName, offset + match.length)) ? " /" : selfClosing;
2411
2440
  return `<${tagName}${a.trimEnd()}${closing}>`;
2412
2441
  });
2413
2442
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lofcz/platejs-markdown",
3
- "version": "53.3.4",
3
+ "version": "53.4.4",
4
4
  "description": "Markdown serializer plugin for Plate",
5
5
  "keywords": [
6
6
  "markdown",
@@ -52,8 +52,8 @@
52
52
  "remark-gfm": "4.0.1",
53
53
  "remark-math": "6.0.0",
54
54
  "@plate/scripts": "1.0.0",
55
- "@platejs/footnote": "npm:@lofcz/platejs-footnote@53.0.0",
56
55
  "@platejs/table": "npm:@lofcz/platejs-table@53.1.8",
56
+ "@platejs/footnote": "npm:@lofcz/platejs-footnote@53.0.0",
57
57
  "platejs": "npm:@lofcz/platejs@53.3.4"
58
58
  },
59
59
  "peerDependencies": {