@lofcz/platejs-markdown 53.3.5 → 53.4.10

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.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as platejs2 from "platejs";
2
2
  import { Descendant, NodeKey, Nullable, OmitFirst, PluginConfig, SlateEditor, TElement, TListElement, TNodeMap, TText, Value } from "platejs";
3
3
  import { MdxJsxFlowElement, MdxJsxFlowElement as MdMdxJsxFlowElement, MdxJsxTextElement, MdxJsxTextElement as MdMdxJsxTextElement } from "mdast-util-mdx";
4
+ import { Token } from "marked";
4
5
  import { Plugin } from "unified";
5
6
  import { Options } from "remark-stringify";
6
- import { Token } from "marked";
7
7
  import baseRemarkMdx from "remark-mdx";
8
8
  import { Blockquote as MdBlockquote, Break as MdBreak, Code as MdCode, Content as MdContent, Definition as MdDefinition, Delete as MdDelete, Emphasis as MdEmphasis, FootnoteDefinition as MdFootnoteDefinition, FootnoteReference as MdFootnoteReference, Heading as MdHeading, Html as MdHtml, Image as MdImage, ImageReference as MdImageReference, InlineCode as MdInlineCode, Link as MdLink, LinkReference as MdLinkReference, List as MdList, ListItem as MdListItem, Paragraph as MdParagraph, PhrasingContent as MDPhrasingContent, Root, Root as MdRoot, RootContent as MdRootContent, Strong as MdStrong, Table as MdTable, TableCell as MdTableCell, TableRow as MdTableRow, Text as MdText, ThematicBreak as MdThematicBreak, Yaml as MdYaml } from "mdast";
9
9
  import { InlineMath as MdInlineMath, Math as MdMath } from "mdast-util-math";
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import { ElementApi, KEYS, TextApi, bindFirst, createTSlatePlugin, getPluginKey, getPluginType, isUrl } from "platejs";
2
2
  import "mdast-util-mdx";
3
3
  import { normalizeDateValue } from "@platejs/date";
4
+ import { Lexer, marked } from "marked";
4
5
  import kebabCase from "lodash/kebabCase.js";
5
6
  import { defaultHandlers, toMarkdown } from "mdast-util-to-markdown";
6
7
  import { unified } from "unified";
7
8
  import remarkStringify from "remark-stringify";
8
9
  import remarkParse from "remark-parse";
9
- import { marked } from "marked";
10
10
  import baseRemarkMdx from "remark-mdx";
11
11
  import { visit } from "unist-util-visit";
12
12
 
@@ -1456,7 +1456,7 @@ const slateToMdast = ({ children, options }) => {
1456
1456
 
1457
1457
  //#endregion
1458
1458
  //#region src/lib/rules/defaultRules.ts
1459
- const BARE_AUTOLINK_PROTOCOL_REGEX = /^https?:\/\//i;
1459
+ const BARE_AUTOLINK_REGEX = /^https?:\/\/[^\s<>\\]+$/i;
1460
1460
  const LEADING_NEWLINE_REGEX = /^\n/;
1461
1461
  function isBoolean(value) {
1462
1462
  return value === true || value === false || !!value && typeof value === "object" && Object.prototype.toString.call(value) === "[object Boolean]";
@@ -1527,10 +1527,14 @@ const defaultRules = {
1527
1527
  }),
1528
1528
  serialize: (node, options) => {
1529
1529
  const children = convertNodesSerialize(node.children, options);
1530
- if (children.length === 1 && children[0]?.type === "text" && children[0].value === node.url && options.remarkStringifyOptions?.resourceLink !== true && BARE_AUTOLINK_PROTOCOL_REGEX.test(node.url ?? "")) return {
1531
- type: "html",
1532
- value: node.url
1533
- };
1530
+ if (children.length === 1 && children[0]?.type === "text" && children[0].value === node.url && options.remarkStringifyOptions?.resourceLink !== true && BARE_AUTOLINK_REGEX.test(node.url ?? "")) {
1531
+ const tokens = Lexer.lexInline(node.url, { gfm: true });
1532
+ const token = tokens[0];
1533
+ if (tokens.length === 1 && token?.type === "link" && token.href === node.url && token.text === node.url) return {
1534
+ type: "html",
1535
+ value: node.url
1536
+ };
1537
+ }
1534
1538
  return {
1535
1539
  children,
1536
1540
  type: "link",
@@ -2407,16 +2411,36 @@ const BOOL_ATTR_REGEXES = Array.from(BOOL_ATTRS.entries()).map(([htmlAttr, jsxAt
2407
2411
  reg: new RegExp(`(\\s|^)${htmlAttr}(\\s|/?>|$)`, "gi")
2408
2412
  }));
2409
2413
  const ATTR_RENAMES = [[/(\s)class=/g, "$1className="], [/(\s)for=/g, "$1htmlFor="]];
2414
+ /**
2415
+ * True HTML void usage (`<br>`, `<img src>`, `<source src>` with no body)
2416
+ * should become self-closing JSX. A matching close tag means the name is
2417
+ * being used as an MDX/JSX component with children — e.g. a custom
2418
+ * `<source>…</source>` excerpt. Forcing `/>` there leaves a stray
2419
+ * `</source>` that remark-mdx throws on; `deserializeMd` then swallows
2420
+ * the error and dumps the entire document as raw paragraphs.
2421
+ *
2422
+ * When another open of the same name appears before the next close, this
2423
+ * open is treated as void (HTML `<source>` inside `<video>` sitting
2424
+ * beside a later paired component).
2425
+ */
2426
+ const shouldForceVoidClose = (source, tagName, afterOpenIndex) => {
2427
+ const rest = source.slice(afterOpenIndex);
2428
+ const nextOpen = rest.search(new RegExp(`<${tagName}\\b`, "i"));
2429
+ const nextClose = rest.search(new RegExp(`<\\/${tagName}\\s*>`, "i"));
2430
+ if (nextClose === -1) return true;
2431
+ if (nextOpen === -1) return false;
2432
+ return nextOpen < nextClose;
2433
+ };
2410
2434
  const htmlToJsx = (html) => {
2411
2435
  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) => {
2436
+ return html.replace(/<!--([\s\S]*?)-->/g, "{/*$1*/}").replace(/<([a-zA-Z0-9]+)\b([^>]*?)(\/?)>/gi, (match, tagName, attrs, selfClosing, offset, source) => {
2413
2437
  let a = attrs;
2414
2438
  ATTR_RENAMES.forEach(([pattern, replacement]) => {
2415
2439
  a = a.replace(pattern, replacement);
2416
2440
  });
2417
2441
  a = a.replace(/(^|\s)([a-zA-Z0-9_-]+)=([^{ \t\n\r"'>]+?)(?=\s|\/?>|$)/g, "$1$2=\"$3\"");
2418
2442
  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;
2443
+ const closing = VOID_ELEMENTS.has(tagName.toLowerCase()) && (selfClosing === "/" || shouldForceVoidClose(source, tagName, offset + match.length)) ? " /" : selfClosing;
2420
2444
  return `<${tagName}${a.trimEnd()}${closing}>`;
2421
2445
  });
2422
2446
  };
@@ -2505,6 +2529,7 @@ const remarkToSlate = function(options = {}) {
2505
2529
 
2506
2530
  //#endregion
2507
2531
  //#region src/lib/deserializer/utils/splitIncompleteMdx.ts
2532
+ const HTTP_AUTOLINK_REGEX = /^<https?:\/\/[^\s<>]*>/i;
2508
2533
  /** Check if character is valid for tag name: A-Z / a-z / 0-9 / $ - . _ : */
2509
2534
  const isNameChar = (c) => c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122 || c === 36 || c === 45 || c === 46 || c === 95 || c === 58;
2510
2535
  const isNameBoundary = (c) => c === 47 || c === 62 || c === 9 || c === 10 || c === 12 || c === 13 || c === 32;
@@ -2519,6 +2544,11 @@ const splitIncompleteMdx = (data) => {
2519
2544
  continue;
2520
2545
  }
2521
2546
  const tagStart = i;
2547
+ const autolink = HTTP_AUTOLINK_REGEX.exec(data.slice(i));
2548
+ if (autolink) {
2549
+ i += autolink[0].length;
2550
+ continue;
2551
+ }
2522
2552
  i++;
2523
2553
  if (i >= len) {
2524
2554
  cutPos = tagStart;
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.10",
4
4
  "description": "Markdown serializer plugin for Plate",
5
5
  "keywords": [
6
6
  "markdown",
@@ -54,7 +54,7 @@
54
54
  "@plate/scripts": "1.0.0",
55
55
  "@platejs/footnote": "npm:@lofcz/platejs-footnote@53.0.0",
56
56
  "@platejs/table": "npm:@lofcz/platejs-table@53.1.8",
57
- "platejs": "npm:@lofcz/platejs@53.3.4"
57
+ "platejs": "npm:@lofcz/platejs@53.4.9"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "platejs": ">=53.3.3",