@bettercms-ai/richtext 0.8.2 → 0.8.3

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/package.json +1 -1
  2. package/src/ingest.ts +9 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettercms-ai/richtext",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "The one Portable Text contract for BetterCMS: schema, converters, and rendering. Shared by the backend, the dashboard and every delivery SDK.",
package/src/ingest.ts CHANGED
@@ -187,13 +187,20 @@ function tableRule(key: KeyGenerator, ingestCell: (html: string) => PortableText
187
187
  };
188
188
  }
189
189
 
190
- /** A code block's text, line breaks included: Lexical writes them as `<br>`, not newlines. */
190
+ /** A highlighter's line box (Expressive Code): a `<div>` directly under the `<code>` or the `<pre>`. */
191
+ const isCodeLine = (node: Node): boolean =>
192
+ node.nodeName.toLowerCase() === "div" && /^(code|pre)$/i.test(node.parentNode?.nodeName ?? "");
193
+
194
+ /**
195
+ * A code block's text, line breaks included: Lexical writes them as `<br>`, a highlighter that
196
+ * boxes each line writes none at all, and read without one two commands became one word.
197
+ */
191
198
  const codeText = (node: Node): string =>
192
199
  node.nodeName.toLowerCase() === "br"
193
200
  ? "\n"
194
201
  : node.nodeType === 3
195
202
  ? (node.textContent ?? "")
196
- : [...node.childNodes].map(codeText).join("");
203
+ : [...node.childNodes].map(codeText).join("") + (isCodeLine(node) ? "\n" : "");
197
204
 
198
205
  /** `language-x` on the `<code>` (Markdown, our renderer) or `data-language` on the `<pre>` (Lexical, Shiki). */
199
206
  const codeLanguage = (pre: Element): string | undefined =>