@lofcz/platejs-markdown 53.3.5 → 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 +22 -2
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -2407,16 +2407,36 @@ const BOOL_ATTR_REGEXES = Array.from(BOOL_ATTRS.entries()).map(([htmlAttr, jsxAt
2407
2407
  reg: new RegExp(`(\\s|^)${htmlAttr}(\\s|/?>|$)`, "gi")
2408
2408
  }));
2409
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
+ };
2410
2430
  const htmlToJsx = (html) => {
2411
2431
  if (!html || typeof html !== "string") return html;
2412
- 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) => {
2413
2433
  let a = attrs;
2414
2434
  ATTR_RENAMES.forEach(([pattern, replacement]) => {
2415
2435
  a = a.replace(pattern, replacement);
2416
2436
  });
2417
2437
  a = a.replace(/(^|\s)([a-zA-Z0-9_-]+)=([^{ \t\n\r"'>]+?)(?=\s|\/?>|$)/g, "$1$2=\"$3\"");
2418
2438
  for (const { reg, jsxAttr } of BOOL_ATTR_REGEXES) a = a.replace(reg, `$1${jsxAttr}="true"$2`);
2419
- const closing = VOID_ELEMENTS.has(tagName.toLowerCase()) ? " /" : selfClosing;
2439
+ const closing = VOID_ELEMENTS.has(tagName.toLowerCase()) && (selfClosing === "/" || shouldForceVoidClose(source, tagName, offset + match.length)) ? " /" : selfClosing;
2420
2440
  return `<${tagName}${a.trimEnd()}${closing}>`;
2421
2441
  });
2422
2442
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lofcz/platejs-markdown",
3
- "version": "53.3.5",
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": {